-
Notifications
You must be signed in to change notification settings - Fork 3
/
line.html
358 lines (309 loc) · 9.29 KB
/
line.html
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!doctype html>
<html>
<head>
<title>Line Rasterization</title>
<script>
"use strict";
/*
* rasterize_line divides lines into fragments.
* va: first vertex. Access coordinates as va.x, va.y.
* vb: second vertex. Access coordinates as vb.x, vb.y.
* emit_fragment: output function. Call as emit_fragment(cx,cy)
* with pixel center of all pixels exited by line.
*
* Consider pixel (x,y) to be "exited" if the line from va-vb exits
* the diamond joining midpoint of each edge of the [x,x+1]x[y,y+1]
* box, i.e., (x+0.5,y)-(x+1,y+0.5)-(x+0.5,y+1)-(x,y+0.5) .
*
*/
function rasterize_line( va, vb, emit_fragment ) {
//TODO: fill in this function
//This is a very bad placeholder implementation, just here so you see something by default.
//Don't actually do this.
//draw equally-spaced fragments along the line:
for (let i = 0; i <= 5; ++i) {
//amount to interpolate
const amt = i / 5.0;
//pt is linearly interpolated between va and vb:
const pt = {
x:(vb.x - va.x) * amt + va.x,
y:(vb.y - va.y) * amt + va.y
};
//px is the pixel containing pt:
const px = {
x:Math.floor(pt.x),
y:Math.floor(pt.y)
};
//emit fragment at px (+0.5 because emit_fragment wants pixel center)
emit_fragment(px.x+0.5, px.y+0.5);
}
}
</script>
<style>
html, body, h1, h2, p { padding: 0; margin: 0; }
html { border-top:25px solid #b00; }
body {
font-family: 'Open Sans', sans-serif;
font-style: normal;
font-size: 12pt;
font-weight: 400;
color: #000;
padding: 0 2em 0 2em;
}
h1, h2 {
font-size: 24pt;
font-weight: 700;
margin:0 auto 14px auto;
}
h1 { text-transform: uppercase; margin-bottom: 5px; }
h2 { font-size: 16pt; }
p, h1, h2 { max-width: 700px; }
p {
color: #505050;
margin:0 auto 1em auto;
}
.highlight { color: #b00; }
</style>
</head>
<body>
<h1>Line Rasterization</h1>
<h2>CMU 15-462/662 Fall 2022 | <span class="highlight">Mini-HW</span></h2>
<p>
Click and drag on the points to change the lines being rasterized.
If the page seems to hang, check your rasterization function for infinite loops.
</p>
<p>
You may find that your browser's developer tools are useful for debugging.
The vertices and fragments are available in the global variables <code>VERTICES</code> and <code>FRAGMENTS</code>, respectively.
</p>
<p>
Grey fragments were emitted by your <code>rasterize_line</code> function exactly once.
Red fragments were emitted more than once (the redder the fragment, the more times it was emitted).
This is generally bad.
</p>
<div id="wrapper">
<canvas id="canvas"></canvas>
</div>
<script>
"use strict";
//-----------------------------------------------------------------
//Code that connects your 'rasterize_line' function to the canvas.
//no need to modify this code.
const WRAPPER = document.getElementById('wrapper');
const CANVAS = document.getElementById('canvas');
const CTX = CANVAS.getContext('2d');
//canvas will be PX_WIDTH x PX_HEIGHT with a PX_MARGIN margin on all sides
const PX_MARGIN = 0.25;
const PX_WIDTH = 16;
const PX_HEIGHT = 8;
let PX_SIZE = 10.0;
const VERTEX_RADIUS = 0.1;
let VERTICES = [
{x: 3.5, y: 3.5},
{x: 6.5, y: 4.5},
{x: 12.5, y: 2.5}
];
VERTICES.dirty = true; //dirty flag controls whether rasterize gets called next draw()
//fragments emitted by last rasterization:
let FRAGMENTS = {};
let HOVERED_VERTEX = null; //reference to object in VERTICES
let DRAGGING = null; //{vertex: , dx:, dy:} -- vertex + relative mouse position
let MOUSE = { };
function setMouse(evt) {
const rect = CANVAS.getBoundingClientRect();
//to canvas pixels:
MOUSE.x = (evt.clientX - rect.left) / rect.width * CANVAS.width;
MOUSE.y = (evt.clientY - rect.bottom) / -rect.height * CANVAS.height;
//to PX space:
MOUSE.x = (MOUSE.x / PX_SIZE) - PX_MARGIN;
MOUSE.y = (MOUSE.y / PX_SIZE) - PX_MARGIN;
}
window.addEventListener('mousemove', function(evt){
evt.preventDefault();
const from = {x:MOUSE.x, y:MOUSE.y};
setMouse(evt);
if (DRAGGING) {
const old = {x:DRAGGING.vertex.x, y:DRAGGING.vertex.y};
DRAGGING.vertex.x = DRAGGING.dx + MOUSE.x;
DRAGGING.vertex.y = DRAGGING.dy + MOUSE.y;
DRAGGING.vertex.x = Math.round(DRAGGING.vertex.x * 8.0) / 8.0;
DRAGGING.vertex.y = Math.round(DRAGGING.vertex.y * 8.0) / 8.0;
DRAGGING.vertex.x = Math.max(0, Math.min(PX_WIDTH, DRAGGING.vertex.x));
DRAGGING.vertex.y = Math.max(0, Math.min(PX_HEIGHT, DRAGGING.vertex.y));
if (DRAGGING.vertex.x != old.x || DRAGGING.vertex.y != old.y) {
VERTICES.dirty = true;
}
} else {
HOVERED_VERTEX = null;
let dis2 = 1.5 * 1.5 * VERTEX_RADIUS * VERTEX_RADIUS; //Infinity;
for (let vertex of VERTICES) {
const test = (vertex.x-MOUSE.x)*(vertex.x-MOUSE.x) + (vertex.y-MOUSE.y)*(vertex.y-MOUSE.y);
if (test < dis2) {
HOVERED_VERTEX = vertex;
dis2 = test;
}
}
}
requestDraw();
return false;
});
window.addEventListener('mousedown', function(evt){
evt.preventDefault();
setMouse(evt);
if (!DRAGGING && HOVERED_VERTEX) {
DRAGGING = {
vertex:HOVERED_VERTEX,
dx:HOVERED_VERTEX.x - MOUSE.x,
dy:HOVERED_VERTEX.y - MOUSE.y
};
}
return false;
});
window.addEventListener('mouseup', function(evt){
evt.preventDefault();
setMouse(evt);
if (DRAGGING) {
DRAGGING = null;
}
requestDraw();
return false;
});
function resized() {
//resize the canvas's underlying context if its shape changes
const style = getComputedStyle(CANVAS);
let size = {x:WRAPPER.clientWidth, y:WRAPPER.clientHeight};
size.x -= parseInt(style.getPropertyValue("padding-left")) + parseInt(style.getPropertyValue("padding-right"));
size.y -= parseInt(style.getPropertyValue("padding-top")) + parseInt(style.getPropertyValue("padding-bottom"));
//compute pixel size to fill canvas nicely:
PX_SIZE = Math.max(10, size.x / (PX_WIDTH + 2.0 * PX_MARGIN));
PX_SIZE *= window.devicePixelRatio;
size.x = Math.floor( PX_SIZE * (PX_WIDTH + 2.0 * PX_MARGIN) );
size.y = Math.floor( PX_SIZE * (PX_HEIGHT + 2.0 * PX_MARGIN) );
if (CANVAS.width != size.x || CANVAS.height != size.y) {
CANVAS.width = size.x;
CANVAS.height = size.y;
CANVAS.style.width = (size.x / window.devicePixelRatio) + "px";
CANVAS.style.height = (size.y / window.devicePixelRatio) + "px";
requestDraw();
}
}
resized();
window.addEventListener('resize', resized);
function rasterize() {
delete VERTICES.dirty;
//clear fragments:
FRAGMENTS = {};
function emit_fragment(x,y) {
if (Math.floor(x) + 0.5 != x || Math.floor(y) + 0.5 != y) {
console.warn(`emit_fragment called with ${x},${y}, which is not a pixel center.`);
return;
}
const key = Math.floor(x) + "." + Math.floor(y);
if (!(key in FRAGMENTS)) {
FRAGMENTS[key] = { x:x, y:y, count:1 };
} else {
FRAGMENTS[key].count += 1;
}
}
for (let i = 1; i < VERTICES.length; ++i) {
//make a copy so rasterize_line can't write vertex positions through sloppy coding:
const va = {x: VERTICES[i-1].x, y: VERTICES[i-1].y};
const vb = {x: VERTICES[i].x, y: VERTICES[i].y};
rasterize_line(va, vb, emit_fragment);
}
}
function draw() {
if (VERTICES.dirty) rasterize();
//clear:
CTX.setTransform(1,0, 0,1, 1,1);
CTX.fillStyle = '#eee';
CTX.fillRect(0, 0, CANVAS.width, CANVAS.height);
//go into pixel grid space:
// (lower-left origin)
CTX.setTransform(PX_SIZE,0, 0,-PX_SIZE, PX_SIZE * PX_MARGIN, PX_SIZE * (PX_HEIGHT + PX_MARGIN));
//for 1layout-px lines, need to draw at:
const px = window.devicePixelRatio / PX_SIZE;
//shade fragments:
for (const key in FRAGMENTS) {
const fragment = FRAGMENTS[key];
const R = 0.5;
if (fragment.count == 1) {
CTX.fillStyle = '#888';
} else {
let amt = 1.0 - Math.pow(0.9, fragment.count);
let R = (1.0 - 0.5) * amt + 0.5;
let G = (0.0 - 0.3) * amt + 0.3;
let B = (0.0 - 0.3) * amt + 0.3;
CTX.fillStyle = `rgb(${Math.round(255*R)},${Math.round(255*G)},${Math.round(255*B)})`;
}
CTX.fillRect(fragment.x-R, fragment.y-R, 2*R, 2*R);
}
//diamonds:
CTX.beginPath();
for (let y = 0; y < PX_HEIGHT; ++y) {
for (let x = 0; x < PX_WIDTH; ++x) {
CTX.moveTo(x + 0.5, y + 0.0);
CTX.lineTo(x + 1.0, y + 0.5);
CTX.lineTo(x + 0.5, y + 1.0);
CTX.lineTo(x + 0.0, y + 0.5);
CTX.closePath();
}
}
CTX.strokeStyle = '#0002';
CTX.lineWidth = px;
CTX.stroke();
//basic grid:
CTX.strokeStyle = '#0008';
CTX.lineWidth = px;
CTX.beginPath();
for (let x = 0; x <= PX_WIDTH; ++x) {
CTX.moveTo(x, 0.0);
CTX.lineTo(x, PX_HEIGHT);
}
for (let y = 0; y <= PX_HEIGHT; ++y) {
CTX.moveTo(0.0, y);
CTX.lineTo(PX_WIDTH, y);
}
CTX.stroke();
//lines:
CTX.strokeStyle = '#f88';
CTX.lineWidth = 4.0 * px;
CTX.beginPath();
for (let x = 1; x < VERTICES.length; ++x) {
CTX.moveTo(VERTICES[x-1].x, VERTICES[x-1].y);
CTX.lineTo(VERTICES[x].x, VERTICES[x].y);
}
CTX.stroke();
//line vertices:
//TODO: nice circles at vertices!
const radius = VERTEX_RADIUS;
for (let vertex of VERTICES) {
CTX.fillStyle = '#f88';
if (vertex == HOVERED_VERTEX) {
CTX.fillStyle = '#ff8';
}
CTX.fillRect(vertex.x-radius, vertex.y-radius, 2*radius, 2*radius);
}
/*//DEBUG: mouse cursor
CTX.beginPath();
CTX.moveTo(MOUSE.x-0.5, MOUSE.y-0.5);
CTX.lineTo(MOUSE.x+0.5, MOUSE.y+0.5);
CTX.moveTo(MOUSE.x-0.5, MOUSE.y+0.5);
CTX.lineTo(MOUSE.x+0.5, MOUSE.y-0.5);
CTX.strokeStyle = '#000';
CTX.lineWidth = 1.0 * px;
CTX.stroke();
*/
}
function requestDraw() {
if (requestDraw.requested) return;
requestDraw.requested = true;
window.requestAnimationFrame(() => {
requestDraw.requested = false;
draw();
});
}
requestDraw();
</script>
</body>
</html>