3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.util.Point
17 * @extends Ext.util.Region
19 * Represents a 2D point with x and y properties, useful for comparison and instantiation
22 * var point = Ext.util.Point.fromEvent(e);
26 Ext.define('Ext.util.Point', {
28 /* Begin Definitions */
29 extend: 'Ext.util.Region',
34 * Returns a new instance of Ext.util.Point base on the pageX / pageY values of the given event
36 * @param {Event} e The event
37 * @returns Ext.util.Point
39 fromEvent: function(e) {
40 e = (e.changedTouches && e.changedTouches.length > 0) ? e.changedTouches[0] : e;
41 return new this(e.pageX, e.pageY);
47 constructor: function(x, y) {
48 this.callParent([y, x, y, x]);
52 * Returns a human-eye-friendly string that represents this point,
53 * useful for debugging
56 toString: function() {
57 return "Point[" + this.x + "," + this.y + "]";
61 * Compare this point and another point
62 * @param {Ext.util.Point/Object} The point to compare with, either an instance
63 * of Ext.util.Point or an object with left and top properties
64 * @return {Boolean} Returns whether they are equivalent
67 return (this.x == p.x && this.y == p.y);
71 * Whether the given point is not away from this point within the given threshold amount.
72 * TODO: Rename this isNear.
73 * @param {Ext.util.Point/Object} The point to check with, either an instance
74 * of Ext.util.Point or an object with left and top properties
75 * @param {Object/Number} threshold Can be either an object with x and y properties or a number
78 isWithin: function(p, threshold) {
79 if (!Ext.isObject(threshold)) {
86 return (this.x <= p.x + threshold.x && this.x >= p.x - threshold.x &&
87 this.y <= p.y + threshold.y && this.y >= p.y - threshold.y);
91 * Compare this point with another point when the x and y values of both points are rounded. E.g:
92 * [100.3,199.8] will equals to [100, 200]
93 * @param {Ext.util.Point/Object} The point to compare with, either an instance
94 * of Ext.util.Point or an object with x and y properties
97 roundedEquals: function(p) {
98 return (Math.round(this.x) == Math.round(p.x) && Math.round(this.y) == Math.round(p.y));
102 * Translate this region by the given offset amount. TODO: Either use translate or translateBy!
103 * @param {Ext.util.Offset/Object} offset Object containing the <code>x</code> and <code>y</code> properties.
104 * Or the x value is using the two argument form.
105 * @param {Number} The y value unless using an Offset object.
106 * @return {Ext.util.Region} this This Region
109 this.prototype.translate = Ext.util.Region.prototype.translateBy;