Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

Point.as

Name: Point Class [MX].  Simple point class combining an x and a y all in one tight little package 

Author: senocular: www.senocular.com
Date: 
1899-12-31T00:40:16.600
Documentation:
class Point: basic point class with manipulators for
adding and other operations of that sort - for both
numbers and other points, as well as other methods for
other ... things. More may be added over time.


Basic run-down:
- a Point is a collection of two values, _x and _y, usually refering to a screen coordinate.
- methods like add have two variations, addN is for a number to be added to both
the _x and _y of the Point (_x + num and _y + num) and addPt is to have another Point added
to the current Point (_x + _x and _y + _y).
- such methods have an optional trans argument.  This specifies whether or not the point using
the method is to be transformed into the result of that method.  Example:

a = new Point(1,1);
b = new Point(2,2);
trace(a.addPt(b)); // traces Point(3, 3)
trace(a); // traces Point(1, 1)
trace(a.addPt(b, true)); // traces Point(3, 3)
trace(a); // traces Point(3, 3)

- new methods and properties are added to the movieclip like _loc (_x and _y in a point),
_mouseLoc and _cursorLoc - the difference between mouse and cursor is that the mouseLoc
is a point containing that clip's _xmouse and _ymouse, whereas the _cursorLoc contains
the _parent's _xmouse and _ymouse.  The reason for the cursorLoc is for something like
following the mouse where you wouldnt use _loc = _mouseLoc, but rather...

myMovieClip.onMouseMove = function(){
	this._loc = this._cursorLoc;
}

- there are also methods for rotation and direction.  rotation is an actual angle and
direction represents a point or vector - something where the _x of the point would
represent the _x movement in along and angle and _y of that point, the _y movement.
- new drawing methods are also included for points

Notes:
- a point object will not work as an init object for attachMovie (nor will
setting something like _loc in an init object work)
- the trans options for methods wont work for properties like _loc which rely on 
getter/setter methods for optaining and setting those properties

Example:
 

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
	POINT CLASS DEFINITION
*********READ ALL COMMENTS! UPDATES INCLUDED***********
*/
Point = function(x,y){
	if (arguments.length) this.init.apply(this, arguments);
};
Point.prototype.toString = function(){
	return "Point("+this.x+", "+this.y+")";
};
Point.prototype.init = function(){
	this.setTo.apply(this, arguments);
	ASSetPropFlags(this,"x,y",3,1);
	return this;
};
Point.prototype.setTo = function(a, b){
	if (a instanceof Point){
		this.x = a.x;
		this.y = a.y;
	}else{
		this.x = a;
		this.y = b;
	};
	return this;
};
Point.prototype.valueOf = function(){
	return new Boolean(this.x || this.y);
}
Point.prototype.copy = function(){
	return new Point(this.x, this.y);
};
Point.prototype.equals = function(pt){
	return (this.x == pt.x && this.y == pt.y);
};
Point.prototype.addN = function(n, trans){
	if (trans){
		this.x += n;
		this.y += n;
		return this;
	};
	return new Point(this.x+n, this.y+n);
};
Point.prototype.addPt = function(pt, trans){
	if (trans){
		this.x += pt.x;
		this.y += pt.y;
		return this;
	};
	return new Point(this.x+pt.x, this.y+pt.y);
};
Point.prototype.subtractN = function(n, trans){
	if (trans){
		this.x -= n;
		this.y -= n;
		return this;
	};
	return new Point(this.x-n, this.y-n);
};
Point.prototype.subtractPt = function(pt, trans){
	if (trans){
		this.x -= pt.x;
		this.y -= pt.y;
		return this;
	};
	return new Point(this.x-pt.x, this.y-pt.y);
};
Point.prototype.multiplyN = function(n, trans){
	if (trans){
		this.x *= n;
		this.y *= n;
		return this;
	};
	return new Point(this.x*n, this.y*n);
};
Point.prototype.multiplyPt = function(pt, trans){
	if (trans){
		this.x *= pt.x;
		this.y *= pt.y;
		return this;
	};
	return new Point(this.x*pt.x, this.y*pt.y);
};
Point.prototype.divideN = function(n, trans){
	if (trans){
		this.x /= n;
		this.y /= n;
		return this;
	};
	return new Point(this.x/n, this.y/n);
};
Point.prototype.dividePt = function(pt, trans){
	if (trans){
		this.x /= pt.x;
		this.y /= pt.y;
		return this;};
	return new Point(this.x/pt.x, this.y/pt.y);
};
Point.prototype.round = function(trans){
	if (trans){
		this.x = Math.round(this.x);
		this.y = Math.round(this.y);
		return this;
	};
	return new Point(Math.round(this.x), Math.round(this.y));
};
Point.prototype.negate = function(trans){
	if (trans){
		this.x = -pt.x;
		this.y = -pt.y;
		return this;};
	return new Point(-this.x, -this.y);
};
Point.prototype.closerTo = function(p1,p2){
	var dx = this.x-p1.x, dy = this.y-p1.y;
	var d1 = dx*dx+dy*dy
	dx = this.x-p2.x;
	dy = this.y-p2.y;
	return (d1 < dx*dx+dy*dy) ? p1 : p2;
};
Point.prototype.furtherFrom = function(p1,p2){
	var dx = this.x-p1.x, dy = this.y-p1.y;
	var d1 = dx*dx+dy*dy
	dx = this.x-p2.x;
	dy = this.y-p2.y;
	return (d1 < dx*dx+dy*dy) ? p2 : p1;
};
Point.prototype.distanceFrom = function(pt){
	var x = pt.x-this.x;
	var y = pt.y-this.y;
	return Math.sqrt(x*x + y*y);
};
Point.prototype.rotationTo = function(pt){
	return Math.atan2(pt.y-this.y, pt.x-this.x) * 180/Math.PI;
};
Point.prototype.directionTo = function(pt){
	var theta = Math.atan2(pt.y-this.y, pt.x-this.x) ;
	return new Point(Math.cos(theta), Math.sin(theta));
};
Point.between = function(p1,p2, t){
	if (t == undefined) return new Point(p1.x+(p2.x-p1.x)/2, p1.y+(p2.y-p1.y)/2);
	if (t<0) t=0;
	else if (t>1) t=1;
	return p1.addPt( p2.subtractPt(p1).multiplyN(t) );
};
ASSetPropFlags(Point.prototype,null,7);
Point.prototype.addProperty("_x",
	function(){
		return this.x;
	},
	function(x){
		this.x = x;
	}
);
Point.prototype.addProperty("_y",
	function(){
		return this.y;
	},
	function(y){
		this.y = y;
	}
);
/*
	MOVIECLIP EXTENSIONS FOR POINTS
*/
MovieClip.prototype.addProperty("_loc",
	function(){
		return new Point(this._x, this._y);
	},
	function(pt){
		this._x = pt.x;
		this._y = pt.y;
	}
);
MovieClip.prototype.addProperty("_mouseLoc",
	function(){
		return new Point(this._xmouse, this._ymouse);
	},
	null
);
MovieClip.prototype.addProperty("_cursorLoc",
	function(){
		return new Point(this._parent._xmouse, this._parent._ymouse);
	},
	null
);
MovieClip.prototype.addProperty("_direction",
	function(){
		var r = this._rotation*Math.PI/180;
		return new Point(Math.cos(r), Math.sin(r));
	},
	function(d){
		this._rotation = new Point(0,0).rotationTo(d);
	}
);
MovieClip.prototype.moveToPt = function(pt){
	this.moveTo(pt.x, pt.y);
	return this;
};
MovieClip.prototype.lineToPt = function(pt){
	this.lineTo(pt.x, pt.y);
	return this;
};
MovieClip.prototype.lineToPts = function(pts){
	if (!(pts instanceof Array)) pts = arguments;
	var l = pts.length;
	for (var i=0; i<l; i++) this.lineToPt(pts<i>);
	return this;
};
MovieClip.prototype.curveToPt = function(p1, p2){
	this.curveTo(p1.x, p1.y, p2.x, p2.y);
	return this;
};
MovieClip.prototype.moveTowardsPoint = function(pt, speed){
	if (this._loc.equals(pt)) return this._loc;
	var x = pt.x-this._x;
	var y = pt.y-this._y;
	var dist = Math.sqrt(x*x + y*y);
	if (dist <= speed) return this._loc = pt;
	var theta = Math.atan2(y, x);
	x = Math.cos(theta)*speed;
	y = Math.sin(theta)*speed;
	trace(new Point(x, y));
	return this._loc = this._loc.addPt(new Point(x, y));
};
MovieClip.prototype.rotateTowardsPoint = function(pt, speed){
	if (speed){
		var angle = this._loc.rotationTo(pt);
		var diff = this._rotation - angle;
		if (Math.abs(diff) < speed) return this._rotation = angle;
		if (diff > 180) angle += 360;
		else if (diff < -180) angle -= 360;
		if (this._rotation < angle) return this._rotation += speed;
		return this._rotation -= speed;
	}
	return this._rotation = this._loc.rotationTo(pt);
};
MovieClip.prototype.moveInDirectionOfPoint = function(pt, speed){
	if (speed == undefined) return this._loc = this._loc.addPt(pt);
	var ratio = speed/pt.distanceFrom(new Point(0,0));
	return this._loc = this._loc.addPt(pt.multiplyN(ratio));
};
ASSetPropFlags(MovieClip.prototype,null,7);