-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIntersection.as
111 lines (95 loc) · 4 KB
/
Intersection.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
package {
import flash.display.*;
import flash.geom.*;
public class Intersection extends Sprite {
// TODO: separate representation and drawing code once I finish experimenting
public var approaches:Array = [new Approach(), new Approach(),
new Approach(), new Approach()];
public var dir:Array = [new Point(+1, 0), new Point(0, +1),
new Point(-1, 0), new Point(0, -1)];
public function Intersection() {
redraw();
}
public function redraw():void {
graphics.clear();
var intersectionBoundary:Array = [];
for (var i:int = 0; i < approaches.length; i++) {
var p:Point, v:Point;
p = V.scale(dir[i], 40);
v = V.left(dir[i]);
graphics.beginFill(0x000000);
drawPath([p, p.add(V.scale(dir[i], 100)),
p.add(V.scale(dir[i], 100)).add(V.scale(v, 10*approaches[i].inLanes)),
p.add(V.scale(v, 10*approaches[i].inLanes))]);
graphics.endFill();
for (var lane:int = 0; lane < approaches[i].inLanes; lane++) {
if (lane != 0)
drawLane(p.add(V.scale(v, 10*lane)), dir[i], 0xffffff, true);
if (lane == 0)
drawLane(p.add(V.scale(v, 10*lane+1)), dir[i], 0xffff00, false);
if (lane == approaches[i].inLanes-1)
drawLane(p.add(V.scale(v, 10*lane+9)), dir[i], 0xffffff, false);
}
graphics.lineStyle(1, 0xffffff, 1.0, false, LineScaleMode.NORMAL, CapsStyle.NONE);
drawPath([p, p.add(V.scale(v, 10*approaches[i].inLanes - 1))]);
graphics.lineStyle();
intersectionBoundary.push(p.add(V.scale(v, 10*approaches[i].inLanes-1)));
p = V.scale(dir[i], 40);
v = V.right(dir[i]);
graphics.beginFill(0x000000);
drawPath([p, p.add(V.scale(dir[i], 100)),
p.add(V.scale(dir[i], 100)).add(V.scale(v, 10*approaches[i].outLanes)),
p.add(V.scale(v, 10*approaches[i].outLanes))]);
graphics.endFill();
for (lane = 0; lane < approaches[i].outLanes; lane++) {
if (lane != 0)
drawLane(p.add(V.scale(v, 10*lane)), dir[i], 0xffffff, true);
if (lane == 0)
drawLane(p.add(V.scale(v, 10*lane+1)), dir[i], 0xffff00, false);
if (lane == approaches[i].outLanes-1)
drawLane(p.add(V.scale(v, 10*lane+9)), dir[i], 0xffffff, false);
}
intersectionBoundary.push(p.add(V.scale(v, 10*approaches[i].outLanes-1)));
}
graphics.beginFill(0x000000);
graphics.moveTo(intersectionBoundary[approaches.length*2-1].x, intersectionBoundary[approaches.length*2-1].y);
for (i = 0; i < approaches.length; i++) {
graphics.lineStyle(1, 0xffffff);
graphics.lineTo(intersectionBoundary[2*i].x,
intersectionBoundary[2*i].y);
graphics.lineStyle();
graphics.lineTo(intersectionBoundary[2*i+1].x,
intersectionBoundary[2*i+1].y);
}
graphics.endFill();
graphics.lineStyle();
}
public function drawLane(p:Point, v:Point, color:int, striped:Boolean):void {
graphics.lineStyle(1, color);
if (striped) {
var matrix:Matrix = new Matrix();
matrix.translate(p.x, p.y);
matrix.scale(4, 1);
matrix.rotate(Math.atan2(v.y, v.x));
var stripeBitmap:BitmapData = new BitmapData(2, 1, true, 0x00000000);
stripeBitmap.setPixel32(0, 0, 0xff000000 | color);
graphics.lineBitmapStyle(stripeBitmap, matrix, true, false);
}
graphics.moveTo(p.x, p.y);
graphics.lineTo(p.x + v.x * 100, p.y + v.y * 100);
graphics.lineStyle();
}
public function drawPath(p:Array):void {
if (p.length > 0) {
graphics.moveTo(p[0].x, p[0].y);
for (var i:int = 1; i < p.length; i++) {
graphics.lineTo(p[i].x, p[i].y);
}
}
}
}
}
class Approach {
public var inLanes:int = 1;
public var outLanes:int = 1;
}