Point.js
3.23 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.
* @constructor
* @author Leandro Ferreira
*/
function Point (x, y) {
this.x = x || 0;
this.y = y || 0;
this.__defineGetter__("length", function(){return Math.sqrt(this.x * this.x + this.y * this.y);});
/**
* Adds the coordinates of another point to the coordinates of this point to create a new point.
* @param {Point} v The point to be added.
* @returns {Point} The new Point.
*/
this.add = function(v) {
return new Point(this.x + v.x, this.y + v.y);
}
/**
* Creates a copy of this Point object.
* @returns {Point} The new Point.
*/
this.clone = function() {
return new Point(this.x, this.y);
}
/**
* [static] Returns the distance between pt1 and pt2.
* @param {Point} pt1 The first point.
* @param {Point} pt2 The second point.
* @returns {Number} The distance between the first and second points.
*/
Point.distance = function(pt1, pt2) {
var dx = p2.x - p1.x;
var dy = p2.y - p1.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Determines whether two points are equal.
* @param {Point} toCompare The point to be compared.
* @returns {Boolean} True if the object is equal to this Point object; false if it is not equal.
*/
this.equals = function(toCompare) {
return this.x == toCompare.x && this.y == toCompare.y;
}
/**
* [static] Determines a point between two specified points.
* @param {Point} pt1 The first point.
* @param {Point} pt2 The second point.
* @param {Number} f The level of interpolation between the two points. Indicates where the new point will be, along the line between pt1 and pt2. If f=1, pt1 is returned; if f=0, pt2 is returned.
* @returns {Point} The new, interpolated point.
*/
Point.interpolate = function(pt1, pt2, f) {
var pt = new Point();
pt.x = p1.x + f * (p2.x - p1.x);
pt.y = p1.y + f * (p2.y - p1.y);
return pt;
}
/**
* Scales the line segment between (0,0) and the current point to a set length.
* @param {Number} thickness The scaling value. For example, if the current point is (0,5), and you normalize it to 1, the point returned is at (0,1).
*/
this.normalize = function(thickness) {
var ratio = thickness / this.length;
this.x *= ratio;
this.y *= ratio;
}
/**
* Offsets the Point object by the specified amount.
* @param {Number} dx The amount by which to offset the horizontal coordinate, x.
* @param {Number} dy The amount by which to offset the vertical coordinate, y.
*/
this.offset = function(dx, dy) {
this.x += dx;
this.y += dy;
}
/**
* [static] Converts a pair of polar coordinates to a Cartesian point coordinate.
* @param {Number} len The length coordinate of the polar pair.
* @param {Number} angle The angle, in radians, of the polar pair.
* @returns {Point} The Cartesian point.
*/
Point.polar = function(len, angle) {
return new Point(len * Math.cos(angle), len * Math.sin(angle));
}
/**
* Subtracts the coordinates of another point from the coordinates of this point to create a new point.
* @param {Point} v The point to be subtracted.
* @returns {Point} The new point.
*/
this.subtract = function(v) {
return new Point( this.x - v.x, this.y = v.y );
}
return this;
}