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

 

senocular.com ActionScript Library

PathLineSegment.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
import flash.geom.Point;
import com.senocular.drawing.PathSegment;

class com.senocular.drawing.PathLineSegment extends PathSegment {
	
	// Constructor
	public function PathLineSegment(startPt:Point, endPt:Point){
		super(startPt, endPt);
		_end = endPt;
		_command = "lineTo";
	}
	
	// Public Properties
	public function get length():Number {
		if (isNaN(_length)){
			_length = lineLength();
		}
		return _length;
	}
	
	// Public Methods
	public function segment(t1:Number, t2:Number):PathSegment {
		if (t2 == 1){
			if (t1 == 0) return this;
			return trim(t1, true);
		}
		var seg:PathSegment = trim(t2);
		if (t1 != 0){
			seg = seg.trim(t1/t2, true);
		}
		return seg;
	}
	public function trim(t:Number, fromStart:Boolean):PathSegment {
		var startPt:Point;
		var endPt:Point;
		if (fromStart){
			endPt = _start;
			startPt = _end;
			t = 1 - t;
		}else{
			startPt = _start;
			endPt = _end;
		}
		var newstart:Point = startPt;
		var newend:Point = startPt;
		var dx:Number = endPt.x - startPt.x;
		var dy:Number = endPt.y - startPt.y;
		if (fromStart){
			newstart = new Point(startPt.x + dx*t, startPt.y + dy*t);
		}else{
			newend = new Point(startPt.x + dx*t, startPt.y + dy*t);
		}
		return new __constructor__(newstart, newend);
	}
	public function pointAt(t:Number, startPt:Point, endPt:Point):Point {
		if (!startPt) startPt = _start;
		if (!endPt) endPt = _end;
		var dx:Number = endPt.x - startPt.x;
		var dy:Number = endPt.y - startPt.y;
		return new Point(startPt.x + dx*t, startPt.y + dy*t);
	}
	public function angleAt(t:Number, startPt:Point, endPt:Point):Number {
		if (!startPt) startPt = _start;
		if (!endPt) endPt = _end;
		return Math.atan2(endPt.y - startPt.y, endPt.x - startPt.x);
	}
	public function lineLength(startPt:Point, endPt:Point):Number {
		if (!startPt) startPt = _start;
		if (!endPt) endPt = _end;
		var dx:Number = endPt.x - startPt.x;
		var dy:Number = endPt.y - startPt.y;
		return Math.sqrt(dx*dx + dy*dy);
	}
}