summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/print_preview/data/coordinate2d.js
blob: 4b10af63808508e18669be00dfaab9f0d1986602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('print_preview', function() {
  'use strict';

  /**
   * Immutable two dimensional point in space. The units of the dimensions are
   * undefined.
   * @param {number} x X-dimension of the point.
   * @param {number} y Y-dimension of the point.
   * @constructor
   */
  function Coordinate2d(x, y) {
    /**
     * X-dimension of the point.
     * @type {number}
     * @private
     */
    this.x_ = x;

    /**
     * Y-dimension of the point.
     * @type {number}
     * @private
     */
    this.y_ = y;
  };

  Coordinate2d.prototype = {
    /** @return {number} X-dimension of the point. */
    get x() {
      return this.x_;
    },

    /** @return {number} Y-dimension of the point. */
    get y() {
      return this.y_;
    },

    /**
     * @param {number} x Amount to translate in the X dimension.
     * @param {number} y Amount to translate in the Y dimension.
     * @return {!print_preview.Coordinate2d} A new two-dimensional point
     *     translated along the X and Y dimensions.
     */
    translate: function(x, y) {
      return new Coordinate2d(this.x_ + x, this.y_ + y);
    },

    /**
     * @param {number} factor Amount to scale the X and Y dimensions.
     * @return {!print_preview.Coordinate2d} A new two-dimensional point scaled
     *     by the given factor.
     */
    scale: function(factor) {
      return new Coordinate2d(this.x_ * factor, this.y_ * factor);
    },

    /**
     * @param {print_preview.Coordinate2d} other The point to compare against.
     * @return {boolean} Whether another point is equal to this one.
     */
    equals: function(other) {
      return other != null &&
          this.x_ == other.x_ &&
          this.y_ == other.y_;
    }
  };

  // Export
  return {
    Coordinate2d: Coordinate2d
  };
});