-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathclip.js
93 lines (81 loc) · 1.83 KB
/
clip.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
kaplay();
const r = new Rect(vec2(100, 100), 300, 200);
const c = new Circle(vec2(250, 200), 100, 100);
const res = new Line(vec2(), vec2());
const testLines = [
new Line(vec2(20, 40), vec2(500, 200)),
new Line(vec2(20, 80), vec2(60, 20)),
new Line(vec2(170, 200), vec2(260, 220)),
new Line(vec2(150, 40), vec2(300, 40)),
new Line(vec2(40, 100), vec2(40, 170)),
new Line(vec2(160, 140), vec2(240, 140)),
new Line(vec2(120, 150), vec2(120, 190)),
];
function drawRectClippedLine(r, l) {
drawLine({
p1: l.p1,
p2: l.p2,
color: WHITE,
});
if (clipLineToRect(r, l, res)) {
drawLine({
p1: res.p1,
p2: res.p2,
color: GREEN,
});
}
}
function drawCircleClippedLine(r, l) {
drawLine({
p1: l.p1,
p2: l.p2,
color: WHITE,
});
if (clipLineToCircle(c, l, res)) {
drawLine({
p1: res.p1,
p2: res.p2,
color: GREEN,
});
}
}
scene("rect", () => {
onDraw(() => {
drawRect({
pos: r.pos,
width: r.width,
height: r.height,
fill: false,
outline: {
color: RED,
width: 1,
},
});
for (line of testLines) {
drawRectClippedLine(r, line);
}
});
onKeyPress("c", () => {
go("circle");
});
});
scene("circle", () => {
onDraw(() => {
drawCircle({
pos: c.center,
radius: c.radius,
fill: false,
outline: {
color: RED,
width: 1,
},
});
for (line of testLines) {
drawCircleClippedLine(c, line);
}
});
onKeyPress("r", () => {
go("rect");
});
});
go("circle");