-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.js
136 lines (119 loc) · 3.59 KB
/
main.js
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
function DrawLine(x0, y0, x1, y1, col)
{
context.fillStyle = col;
context.strokeStyle = col;
context.beginPath();
context.moveTo(x0+canvas.width/2, canvas.height/2-y0);
context.lineTo(x1+canvas.width/2, canvas.height/2-y1);
context.stroke();
}
function DrawPoint(x, y, col)
{
context.fillStyle = col;
context.strokeStyle = col;
context.beginPath();
context.arc(x+canvas.width/2, canvas.height/2-y, 2, 0, 2.0*Math.PI, false);
context.fill();
context.stroke();
}
function DrawText(text, x, y, col)
{
context.fillStyle = col;
context.fillText(text, x+canvas.width/2, canvas.height/2-y);
}
function FillRect(x, y, w, h, col)
{
context.fillStyle = col;
context.fillRect(x+canvas.width/2, canvas.height/2-y, w, h);
}
function DrawPolygon(points, off, col)
{
context.strokeStyle = col;
context.fillStyle = col;
// draw vertices
var ymax = 0;
for (var i=0; i<points.length; i++)
{
var pt = points[i];
DrawPoint(pt.x+off.x, pt.y+off.y, col);
ymax = Math.max(ymax, pt.y);
}
// draw lines
for (var i=0; i<points.length; i++)
{
var pt0 = points[i];
var pt1 = points[(i+1)%points.length]
var midPt = pt0.midpoint(pt1);
DrawLine(pt0.x+off.x, pt0.y+off.y, pt1.x+off.x, pt1.y+off.y, col);
}
}
points =
[
new Vector(235, 774),
new Vector(245, 740),
new Vector(230, 710),
new Vector(240, 703),
new Vector(274, 733),
new Vector(306, 710),
new Vector(272, 690),
new Vector(277, 639),
new Vector(305, 645),
new Vector(347, 611),
new Vector(340, 639),
new Vector(298, 674),
new Vector(325, 702),
new Vector(335, 663),
new Vector(355, 645),
new Vector(350, 686),
new Vector(400, 710),
new Vector(360, 725),
new Vector(357, 755),
new Vector(328, 723),
new Vector(291, 741),
new Vector(289, 754),
new Vector(266, 757),
];
// points.length must be > 0
function CalcAabb(points)
{
var aabbMin = points[0].clone();
var aabbMax = points[0].clone();
for (var i=1; i<points.length; i++)
{
aabbMin.x = Math.min(aabbMin.x, points[i].x);
aabbMin.y = Math.min(aabbMin.y, points[i].y);
aabbMax.x = Math.max(aabbMax.x, points[i].x);
aabbMax.y = Math.max(aabbMax.y, points[i].y);
}
return [aabbMin, new Vector(aabbMin.x, aabbMax.y), aabbMax, new Vector(aabbMax.x, aabbMin.y)];
}
function UiCompute()
{
// clear canvas
FillRect(-canvas.width/2, canvas.height/2, canvas.width, canvas.height, "#eeeeee");
// transform points into view
for (var i=0; i<points.length; i++)
{
points[i].x *= 1.5;
points[i].y *= 1.5;
points[i].x -= 250;
points[i].y -= 1050;
}
// do computations
var convexHull = CalcConvexHull(points);
var oobb = CalcOmbb(convexHull); // draws OOBB candidates
var aabb = CalcAabb(points);
// paint infos
context.lineWidth = 2.0;
DrawPolygon(convexHull, new Vector(0, 0), "#00c800", false);
context.lineWidth = 1.0;
DrawText("Convex hull", -100, 200, "#00c800");
DrawText("Axis aligned bounding box", -100, 212, "#0000ff");
DrawText("Oriented bounding box", -100, 224, "#ff0000");
DrawText("OOBB candidates", -100, 236, "#336699");
DrawText("Input polygon", -100, 248, "#000000");
DrawPolygon(points, new Vector(-480, 0), "#000000", false);
DrawPolygon(convexHull, new Vector(-480, 0), "#00c800", false);
DrawPolygon(oobb, new Vector(-480, 0), "#ff0000", false);
DrawPolygon(aabb, new Vector(-480, 0), "#0000ff", false);
}