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

 

senocular.com ActionScript Library

Path.as

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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import flash.geom.Point;
import com.senocular.drawing.PathSegment;
import com.senocular.drawing.PathMoveSegment;
import com.senocular.drawing.PathLineSegment;
import com.senocular.drawing.PathCurveSegment;
import com.senocular.drawing.PathCircleSegment;

/**
* The Path class saves a collection of line drawing commands to represent a path.
* Using the path class you can draw all of or a segment of that path in a movie clip
* or get locations and orientations along the path to have objects follow it.
*
* Note: People using Flash MX 2004 will have to use a custom flash.geom.Point class
*
* @usage
* <pre><code>import com.senocular.drawing.Path;
* // create a path instance that draws in _root
* var myPath:Path = new Path();
* // draw a square in the path
* myPath.moveTo(50, 50);
* myPath.lineTo(100, 50);
* myPath.lineTo(100, 100);
* myPath.lineTo(50, 100);
* myPath.lineTo(50, 50);
* // draw the path in _root
* _root.lineStyle(0, 0, 100);
* myPath.draw(_root);
* </code></pre>
* @author Trevor McCauley, senocular.com
* @version 2.2.0
 */
class com.senocular.drawing.Path {
	private var _lengthValid:Boolean = true;
	private var _length:Number = 0;
	private var _moveToHasLength:Boolean = false;
	private var _position:Point;
	private var _segments:Array;

	// Constructor
	/**
	 * Constructor. Creates a new path instance
	 */
	function Path(){
		init();
	}
	
	// Public Properties
	/**
	 * The approximate length of the total path in pixels. 
	 * @see moveToHasLength
	 */
	public function get length():Number {
		if (!_lengthValid) {
			_lengthValid = true;
			_length = 0;
			var seg:PathSegment;
			var i:Number = _segments.length;
			while (i--){
				seg = _segments[i];
				if (_moveToHasLength || seg._command != "moveTo"){
					_length += seg.length;
				}
			}
		}
		return _length;
	}
	/**
	 * Determines whether or not moveTo commands are treated
	 * as line segments therefore adding to the total length of the path
	 * @see length
	 */
	public function get moveToHasLength():Boolean {
		return _moveToHasLength;
	}
	public function set moveToHasLength(b:Boolean):Void {
		_lengthValid = false;
		_moveToHasLength = b;
	}
	/**
	 * The current position of the drawing pen based on the last drawing command used.
	 * If you want to move the pen position without invoking a moveTo command, set
	 * this property to a new point at the desired location
	 * @see moveTo
	 * @see lineTo
	 * @see curveTo
	 * @see circleTo
	 */
	public function get position():Point {
		return _position.clone();
	}
	public function set position(p:Point):Void {
		_position = p.clone();
		_segments.push(new PathSegment(_position));
	}
	
	// Public Methods
	/**
	 * Moves the current drawing position to (x, y).
	 * @param x An integer indicating the horizontal location to move the drawing position
	 * @param y An integer indicating the vertical location to move the drawing position
	 * @see lineTo
	 * @see curveTo
	 * @see circleTo
	 * @return Nothing
	 */
	public function moveTo(x:Number, y:Number):Void {
		if (_moveToHasLength){
			_lengthValid = false;
		}
		var end:Point = new Point(x, y);
		_segments.push(new PathMoveSegment(_position, end));
		_position = end;
	}
	/**
	 * Creates a line in the path from the current drawing position
	 * to (x, y); the current drawing position is then set to (x, y).
	 * @param x An integer indicating the horizontal position of the end anchor point of the line
	 * @param y An integer indicating the vertical position of the end anchor point of the line
	 * @see moveTo
	 * @see curveTo
	 * @see circleTo
	 * @return Nothing
	 */
	public function lineTo(x:Number, y:Number):Void {
		_lengthValid = false;
		var end:Point = new Point(x, y);
		_segments.push(new PathLineSegment(_position, end));
		_position = end;
	}
	/**
	 * Creates a curve in the path from the current drawing position to
	 * (x, y) using the control point specified by (cx, cy). The current  drawing position is then set
	 * to (x, y).
	 * @param cx An integer that specifies the horizontal position of the control point of the curve
	 * @param cy An integer that specifies the vertical position of the control point of the curve
	 * @param x An integer that specifies the horizontal position of the end anchor point of the curve
	 * @param y An integer that specifies the vertical position of the end anchor point of the curve
	 * @see moveTo
	 * @see lineTo
	 * @see circleTo
	 * @return Nothing
	 */
	public function curveTo(cx:Number, cy:Number, x:Number, y:Number):Void {
		_lengthValid = false;
		var end:Point = new Point(x, y);
		_segments.push(new PathCurveSegment(_position, new Point(cx, cy), end));
		_position = end;
	}
	/**
	 * Creates a circle segment in the path from the current drawing position to
	 * (x, y) using a third intermediary point (cx, cy) which lies on the circular path
	 * between the current drawing position and (x, y). The current  drawing position is then set
	 * to (x, y).
	 * @param cx An integer that specifies the horizontal position of the control point of the circle segment
	 * @param cy An integer that specifies the vertical position of the control point of the circle segment
	 * @param x An integer that specifies the horizontal position of the end anchor point of the circle segment
	 * @param y An integer that specifies the vertical position of the end anchor point of the circle segment
	 * @see moveTo
	 * @see lineTo
	 * @see curveTo
	 * @return Nothing
	 */
	public function circleTo(cx:Number, cy:Number, x:Number, y:Number):Void {
		_lengthValid = false;
		var end:Point = new Point(x, y);
		_segments.push(new PathCircleSegment(_position, new Point(cx, cy), end));
		_position = end;
	}
	/**
	 * Clears all drawing commands in the path
	 * @see moveTo
	 * @see lineTo
	 * @see curveTo
	 * @see circleTo
	 * @return nothing
	 */
	public function clear(Void):Void {
		init();
	}
	/**
	 * Draws the saved path into the target object passed.
	 * @param target The object receiving a copy of the drawing commands saved to the path instance
	 * @param startt A float between 0 and 1 where 0 is the start of the path and 1 is the end of
	 * the path to start drawing in the target
	 * @param endt A float between 0 and 1 where 0 is the start of the path and 1 is the end of
	 * the path to stop drawing in the target
	 * @see moveTo
	 * @see lineTo
	 * @see curveTo
	 * @see circleTo
	 * @return Nothing
	 */
	public function draw(target:Object, startt:Number, endt:Number):Void {
		startt = cleant(startt, 0);
		endt = cleant(endt, 1);
		if (endt < startt){
			draw(target, startt, 1);
			draw(target, 0, endt);
			return;
		}
		var segments:Array = getSegmentsToDraw(startt, endt);
		if (segments.length){
			target.moveTo(segments[0].start.x, segments[0].start.y);
			var n:Number = segments.length;
			var i:Number;
			for (i=0; i<n; i++){
				segments[i].draw(target);
			}
		}
	}
	/**
	 * Gets the location of a point on the path at a specific location along the path
	 * @param t A float between 0 and 1 where 0 is the start of the path and 1 is the end of
	 * the path to find a point
	 * @return point along the path at the t location along the path
	 * @see angleAt
	 */
	public function pointAt(t:Number):Point {
		t = cleant(t);
		if (t == 0){
			return _segments[0].pointAt(t);
		}else if (t == 1){
			var last:Number = _segments.length - 1;
			return _segments[last].pointAt(t);
		}
		var tLength = t*length;
		var curLength:Number = 0;
		var lastLength:Number = 0;
		var seg:PathSegment;
		var n:Number = _segments.length;
		var i:Number;
		for (i=0; i<n; i++){
			seg = _segments[i];
			if ((_moveToHasLength || seg._command != "moveTo") && seg.length){
				curLength += seg.length;
			}else{
				continue;
			}
			if (tLength <= curLength){
				return seg.pointAt((tLength - lastLength)/seg.length);
			}
			lastLength = curLength;
		}
		return new Point(0, 0);
	}
	/**
	 * Gets the angle in radians of a point on the path at a specific location along the path
	 * @param t A float between 0 and 1 where 0 is the start of the path and 1 is the end of
	 * the path to find an angle
	 * @return angle along the path at the t location along the path
	 * @see pointAt
	 */
	public function angleAt(t:Number):Number {
		t = cleant(t);
		var tLength:Number = t*length;
		var curLength:Number = 0;
		var lastLength:Number = 0;
		var seg:PathSegment;
		var n:Number = _segments.length;
		var i:Number;
		for (i=0; i<n; i++){
			seg = _segments[i];
			if ((_moveToHasLength || seg._command != "moveTo") && seg.length){
				curLength += seg.length;
			}else{
				continue;
			}
			if (tLength <= curLength){
				return seg.angleAt((tLength - lastLength)/seg.length);
			}
			lastLength = curLength;
		}
		return 0;
	}
	
	// Private
	private function init(Void):Void {
		_lengthValid = false;
		_segments = new Array();
		_position = new Point(0,0);
	}
	private function cleant(t:Number, base:Number):Number {
			if (isNaN(t)) t = base;
			else if (t < 0 || t > 1){
				t %= 1;
				if (t == 0) t = base;
				else if (t < 0) t += 1;
			}
			return t;
	}
	private function getSegmentsToDraw(startt:Number, endt:Number):Array {
		var startLength:Number = startt*length;
		var endLength:Number = endt*length;
		var curLength:Number = 0;
		var lastLength:Number = 0;
		var starti:Number = -1;
		var endi:Number = -1;
		var segStartt:Number = 0;
		var segEndt:Number = 1;
		var newSegments:Array = new Array();
		var seg:PathSegment;
		var n:Number = _segments.length;
		var i:Number;
		for (i=0; i<n; i++){
			seg = _segments[i];
			if ((_moveToHasLength || seg._command != "moveTo") && seg.length){
				curLength += seg.length;
			}else{
				continue;
			}
			if (startLength < curLength && starti == -1){
				starti = i;
				segStartt = (startt == 0) ? 0 : (startLength - lastLength)/seg.length;
			}
			if (endLength <= curLength){
				endi = i;
				segEndt = (endt == 1) ? 1 : (endLength - lastLength)/seg.length;
				break;
			}
			lastLength = curLength;
		}
		if (starti < 0 || endi < 0){
			return newSegments;
		}
		newSegments = _segments.slice(starti, endi + 1);
		if (starti == endi){
			if (segStartt != 0 || segEndt != 1){
				newSegments[0] = newSegments[0].segment(segStartt, segEndt);
			}
		}else{
			if (segStartt != 0){
				newSegments[0] = newSegments[0].segment(segStartt, 1);
			}
			if (segEndt != 1){
				var last:Number = newSegments.length - 1;
				newSegments[last] = newSegments[last].segment(0, segEndt);
			}
		}
		return newSegments;
	}
}