-
Notifications
You must be signed in to change notification settings - Fork 0
/
skalaclip.js
134 lines (108 loc) · 2.49 KB
/
skalaclip.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
function Point(x, y) {
return { x, y };
}
function Vector(x, y, z) {
return { x, y, z };
}
function pointCross(a, b) {
return Vector(a.y - b.y, b.x - a.x, a.x * b.y - a.y * b.x);
}
function cross(u, v) {
const z = u.x * v.y - u.y * v.x;
return Point((u.y * v.z - u.z * v.y) / z, (u.z * v.x - u.x * v.z) / z);
}
function dot(u, v) {
return u.x * v.x + u.y * v.y + u.z * v.z;
}
const mask = [0, 1, 2, 2, 4, 0, 4, 4, 8, 1, 0, 2, 8, 1, 8, 0];
const tab1 = [4, 3, 0, 3, 1, 4, 0, 3, 2, 2, 4, 2, 1, 1, 0, 4];
const tab2 = [4, 0, 1, 1, 2, 4, 2, 2, 3, 0, 4, 1, 3, 0, 3, 4];
class Clipper {
constructor(a, b) {
if (a.x < b.x) {
this.x_min = a.x;
this.x_max = b.x;
} else {
this.x_min = b.x;
this.x_max = a.x;
}
if (a.y < b.y) {
this.y_min = a.y;
this.y_max = b.y;
} else {
this.y_min = b.y;
this.y_max = a.y;
}
this.x = [
Vector(this.x_min, this.y_min, 1),
Vector(this.x_max, this.y_min, 1),
Vector(this.x_max, this.y_max, 1),
Vector(this.x_min, this.y_max, 1),
];
this.e = [
Vector(0, 1, -this.y_min),
Vector(1, 0, -this.x_max),
Vector(0, 1, -this.y_max),
Vector(1, 0, -this.x_min),
];
}
code(pt) {
let c = 0;
if (pt.x < this.x_min) {
c = 8;
} else if (pt.x > this.x_max) {
c = 2;
}
if (pt.y < this.y_min) {
c |= 1;
} else if (pt.y > this.y_max) {
c |= 4;
}
return c;
};
clipLine(xA, xB) {
const cA = this.code(xA);
const cB = this.code(xB);
if ((cA | cB) === 0) {
return [0, xA, xB];
}
if ((cA & cB) !== 0) {
return [-1, xA, xB];
}
const p = pointCross(xA, xB);
let c = 0;
for (let k = 0; k < 4; k += 1) {
if (dot(p, this.x[k]) <= 0) {
c |= (1 << k);
}
}
if (c === 0 || c === 15) {
return [-1, xA, xB];
}
const i = tab1[c];
const j = tab2[c];
if (cA !== 0 && cB !== 0) {
const newXA = cross(p, this.e[i]);
const newXB = cross(p, this.e[j]);
return [3, newXA, newXB];
}
if (cA === 0) {
let newXB = null;
if ((cB & mask[c]) === 0) {
newXB = cross(p, this.e[i]);
} else {
newXB = cross(p, this.e[j]);
}
return [2, xA, newXB];
}
if (cB === 0) {
let newXA = null;
if ((cA & mask[c]) === 0) {
newXA = cross(p, this.e[i]);
} else {
newXA = cross(p, this.e[j]);
}
return [1, newXA, xB];
}
}
}