forked from NCSUCGClass/prog1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawstuff.js
490 lines (425 loc) · 20.4 KB
/
drawstuff.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* classes */
// Color constructor
class Color {
constructor(r,g,b,a) {
try {
if ((typeof(r) !== "number") || (typeof(g) !== "number") || (typeof(b) !== "number") || (typeof(a) !== "number"))
throw "color component not a number";
else if ((r<0) || (g<0) || (b<0) || (a<0))
throw "color component less than 0";
else if ((r>255) || (g>255) || (b>255) || (a>255))
throw "color component bigger than 255";
else {
this.r = r; this.g = g; this.b = b; this.a = a;
}
} // end try
catch (e) {
console.log(e);
}
} // end Color constructor
// Color change method
change(r,g,b,a) {
try {
if ((typeof(r) !== "number") || (typeof(g) !== "number") || (typeof(b) !== "number") || (typeof(a) !== "number"))
throw "color component not a number";
else if ((r<0) || (g<0) || (b<0) || (a<0))
throw "color component less than 0";
else if ((r>255) || (g>255) || (b>255) || (a>255))
throw "color component bigger than 255";
else {
this.r = r; this.g = g; this.b = b; this.a = a;
}
} // end throw
catch (e) {
console.log(e);
}
} // end Color change method
} // end color class
/* utility functions */
// draw a pixel at x,y using color
function drawPixel(imagedata,x,y,color) {
try {
if ((typeof(x) !== "number") || (typeof(y) !== "number"))
throw "drawpixel location not a number";
else if ((x<0) || (y<0) || (x>=imagedata.width) || (y>=imagedata.height))
throw "drawpixel location outside of image";
else if (color instanceof Color) {
var pixelindex = (y*imagedata.width + x) * 4;
imagedata.data[pixelindex] = color.r;
imagedata.data[pixelindex+1] = color.g;
imagedata.data[pixelindex+2] = color.b;
imagedata.data[pixelindex+3] = color.a;
} else
throw "drawpixel color is not a Color";
} // end try
catch(e) {
console.log(e);
}
} // end drawPixel
// draw random pixels
function drawRandPixels(context) {
var c = new Color(0,0,0,0); // the color at the pixel: black
var w = context.canvas.width;
var h = context.canvas.height;
var imagedata = context.createImageData(w,h);
const PIXEL_DENSITY = 0.01;
var numPixels = (w*h)*PIXEL_DENSITY;
// Loop over 1% of the pixels in the image
for (var x=0; x<numPixels; x++) {
c.change(Math.random()*255,Math.random()*255,
Math.random()*255,255); // rand color
drawPixel(imagedata,
Math.floor(Math.random()*w),
Math.floor(Math.random()*h),
c);
} // end for x
context.putImageData(imagedata, 0, 0);
} // end draw random pixels
// get the input ellipsoids from the standard class URL
function getInputEllipsoids() {
const INPUT_ELLIPSOIDS_URL =
"https://ncsucgclass.github.io/prog1/ellipsoids.json";
// load the ellipsoids file
var httpReq = new XMLHttpRequest(); // a new http request
httpReq.open("GET",INPUT_ELLIPSOIDS_URL,false); // init the request
httpReq.send(null); // send the request
var startTime = Date.now();
while ((httpReq.status !== 200) && (httpReq.readyState !== XMLHttpRequest.DONE)) {
if ((Date.now()-startTime) > 3000)
break;
} // until its loaded or we time out after three seconds
if ((httpReq.status !== 200) || (httpReq.readyState !== XMLHttpRequest.DONE)) {
console.log*("Unable to open input ellipses file!");
return String.null;
} else
return JSON.parse(httpReq.response);
} // end get input ellipsoids
//get the input triangles from the standard class URL
function getInputTriangles() {
const INPUT_TRIANGLES_URL =
"https://ncsucgclass.github.io/prog1/triangles.json";
// load the triangles file
var httpReq = new XMLHttpRequest(); // a new http request
httpReq.open("GET",INPUT_TRIANGLES_URL,false); // init the request
httpReq.send(null); // send the request
var startTime = Date.now();
while ((httpReq.status !== 200) && (httpReq.readyState !== XMLHttpRequest.DONE)) {
if ((Date.now()-startTime) > 3000)
break;
} // until its loaded or we time out after three seconds
if ((httpReq.status !== 200) || (httpReq.readyState !== XMLHttpRequest.DONE)) {
console.log*("Unable to open input triangles file!");
return String.null;
} else
return JSON.parse(httpReq.response);
} // end get input triangles
//get the input boxex from the standard class URL
function getInputBoxes() {
const INPUT_BOXES_URL =
"https://ncsucgclass.github.io/prog1/boxes.json";
// load the boxes file
var httpReq = new XMLHttpRequest(); // a new http request
httpReq.open("GET",INPUT_BOXES_URL,false); // init the request
httpReq.send(null); // send the request
var startTime = Date.now();
while ((httpReq.status !== 200) && (httpReq.readyState !== XMLHttpRequest.DONE)) {
if ((Date.now()-startTime) > 3000)
break;
} // until its loaded or we time out after three seconds
if ((httpReq.status !== 200) || (httpReq.readyState !== XMLHttpRequest.DONE)) {
console.log*("Unable to open input boxes file!");
return String.null;
} else
return JSON.parse(httpReq.response);
} // end get input boxes
// put random points in the ellipsoids from the class github
function drawRandPixelsInInputEllipsoids(context) {
var inputEllipsoids = getInputEllipsoids();
var w = context.canvas.width;
var h = context.canvas.height;
var imagedata = context.createImageData(w,h);
const PIXEL_DENSITY = 0.1;
var numCanvasPixels = (w*h)*PIXEL_DENSITY;
if (inputEllipsoids != String.null) {
var x = 0; var y = 0; // pixel coord init
var cx = 0; var cy = 0; // init center x and y coord
var ellipsoidXRadius = 0; // init ellipsoid x radius
var ellipsoidYRadius = 0; // init ellipsoid y radius
var numEllipsoidPixels = 0; // init num pixels in ellipsoid
var c = new Color(0,0,0,0); // init the ellipsoid color
var n = inputEllipsoids.length; // the number of input ellipsoids
//console.log("number of ellipses: " + n);
// Loop over the ellipsoids, draw rand pixels in each
for (var e=0; e<n; e++) {
cx = w*inputEllipsoids[e].x; // ellipsoid center x
cy = h*inputEllipsoids[e].y; // ellipsoid center y
ellipsoidXRadius = Math.round(w*inputEllipsoids[e].a); // x radius
ellipsoidYRadius = Math.round(h*inputEllipsoids[e].b); // y radius
numEllipsoidPixels = ellipsoidXRadius*ellipsoidYRadius*Math.PI; // projected ellipsoid area
numEllipsoidPixels *= PIXEL_DENSITY; // percentage of ellipsoid area to render to pixels
numEllipsoidPixels = Math.round(numEllipsoidPixels);
//console.log("ellipsoid x radius: "+ellipsoidXRadius);
//console.log("ellipsoid y radius: "+ellipsoidYRadius);
//console.log("num ellipsoid pixels: "+numEllipsoidPixels);
c.change(
inputEllipsoids[e].diffuse[0]*255,
inputEllipsoids[e].diffuse[1]*255,
inputEllipsoids[e].diffuse[2]*255,
255); // ellipsoid diffuse color
for (var p=0; p<numEllipsoidPixels; p++) {
do {
x = Math.random()*2 - 1; // in unit square
y = Math.random()*2 - 1; // in unit square
} while (Math.sqrt(x*x + y*y) > 1) // a circle is also an ellipse
drawPixel(imagedata,
cx+Math.round(x*ellipsoidXRadius),
cy+Math.round(y*ellipsoidYRadius),c);
//console.log("color: ("+c.r+","+c.g+","+c.b+")");
//console.log("x: "+Math.round(w*inputEllipsoids[e].x));
//console.log("y: "+Math.round(h*inputEllipsoids[e].y));
} // end for pixels in ellipsoid
} // end for ellipsoids
context.putImageData(imagedata, 0, 0);
} // end if ellipsoids found
} // end draw rand pixels in input ellipsoids
// draw 2d projections read from the JSON file at class github
function drawInputEllipsoidsUsingArcs(context) {
var inputEllipsoids = getInputEllipsoids();
if (inputEllipsoids != String.null) {
var c = new Color(0,0,0,0); // the color at the pixel: black
var w = context.canvas.width;
var h = context.canvas.height;
var n = inputEllipsoids.length;
//console.log("number of ellipsoids: " + n);
// Loop over the ellipsoids, draw each in 2d
for (var e=0; e<n; e++) {
context.fillStyle =
"rgb(" + Math.floor(inputEllipsoids[e].diffuse[0]*255)
+","+ Math.floor(inputEllipsoids[e].diffuse[1]*255)
+","+ Math.floor(inputEllipsoids[e].diffuse[2]*255) +")"; // diffuse color
context.save(); // remember previous (non-) scale
context.scale(1, inputEllipsoids[e].b/inputEllipsoids[e].a); // scale by ellipsoid ratio
context.beginPath();
context.arc(
Math.round(w*inputEllipsoids[e].x),
Math.round(h*inputEllipsoids[e].y),
Math.round(w*inputEllipsoids[e].a),
0,2*Math.PI);
context.restore(); // undo scale before fill so stroke width unscaled
context.fill();
//console.log(context.fillStyle);
//console.log("x: "+Math.round(w*inputEllipsoids[e].x));
//console.log("y: "+Math.round(h*inputEllipsoids[e].y));
//console.log("a: "+Math.round(w*inputEllipsoids[e].a));
//console.log("b: "+Math.round(h*inputEllipsoids[e].b));
} // end for ellipsoids
} // end if ellipsoids found
} // end draw input ellipsoids
//put random points in the triangles from the class github
function drawRandPixelsInInputTriangles(context) {
var inputTriangles = getInputTriangles();
var w = context.canvas.width;
var h = context.canvas.height;
var imagedata = context.createImageData(w,h);
const PIXEL_DENSITY = 0.1;
var numCanvasPixels = (w*h)*PIXEL_DENSITY;
if (inputTriangles != String.null) {
var x = 0; var y = 0; // pixel coord init
var cx = 0; var cy = 0; // init center x and y coord
var numTrianglePixels = 0; // init num pixels in triangle
var c = new Color(0,0,0,0); // init the triangle color
var n = inputTriangles.length; // the number of input files
//console.log("number of files: " + n);
// Loop over the triangles, draw rand pixels in each
for (var f=0; f<n; f++) {
var tn = inputTriangles[f].triangles.length;
//console.log("number of triangles in this files: " + tn);
// Loop over the triangles, draw each in 2d
for(var t=0; t<tn; t++){
var vertex1 = inputTriangles[f].triangles[t][0];
var vertex2 = inputTriangles[f].triangles[t][1];
var vertex3 = inputTriangles[f].triangles[t][2];
var vertexPos1 = inputTriangles[f].vertices[vertex1];
var vertexPos2 = inputTriangles[f].vertices[vertex2];
var vertexPos3 = inputTriangles[f].vertices[vertex3];
//console.log("vertexPos1 " + vertexPos1);
//console.log("vertexPos2 " + vertexPos2);
//console.log("vertexPos3 " + vertexPos3);
// triangle position on canvas
var v1 = [w*vertexPos1[0], h*vertexPos1[1]];
var v2 = [w*vertexPos2[0], h*vertexPos2[1]];
var v3 = [w*vertexPos3[0], h*vertexPos3[1]];
// calculate triangle area on canvas (shoelace formula)
var triangleArea = 0.5*Math.abs(v1[0]*v2[1]+v2[0]*v3[1]+v3[0]*v1[1]-v2[0]*v1[1]-v3[0]*v2[1]-v1[0]*v3[1]);
var numTrianglePixels = triangleArea; // init num pixels in triangle
//console.log("triangle area " + triangleArea);
numTrianglePixels *= PIXEL_DENSITY; // percentage of triangle area to render to pixels
numTrianglePixels = Math.round(numTrianglePixels);
// console.log("numTrianglePixels " + numTrianglePixels);
c.change(
inputTriangles[f].material.diffuse[0]*255,
inputTriangles[f].material.diffuse[1]*255,
inputTriangles[f].material.diffuse[2]*255,
255); // triangle diffuse color
for (var p=0; p<numTrianglePixels; p++) {
var point; // on canvas plane
var triangleTest = 0;
while (triangleTest == 0 ){ //if the pixel outside the triangle
point = [Math.floor(Math.random()*w), Math.floor(Math.random()*h)];
// plane checking
var t1 = ((point[0]-v2[0]) * (v1[1] - v2[1]) - (v1[0] - v2[0]) * (point[1] - v2[1])) < 0.0;
var t2 = ((point[0]-v3[0]) * (v2[1] - v3[1]) - (v2[0] - v3[0]) * (point[1] - v3[1])) < 0.0;
var t3 = ((point[0]-v1[0]) * (v3[1] - v1[1]) - (v3[0] - v1[0]) * (point[1] - v1[1])) < 0.0;
if((t1==t2)&&(t2==t3)) // draw the pixel if inside the triangle
triangleTest = 1;
}
drawPixel(imagedata,point[0],point[1],c);
//console.log("color: ("+c.r+","+c.g+","+c.b+")");
//console.log("x: "+ x);
//console.log("y: "+ y);
} // end for pixels in triangle
} // end for triangles
} // end for files
context.putImageData(imagedata, 0, 0);
} // end if triangle file found
} // end draw rand pixels in input triangles
//draw 2d projections traingle from the JSON file at class github
function drawInputTrainglesUsingPaths(context) {
var inputTriangles = getInputTriangles();
if (inputTriangles != String.null) {
var c = new Color(0,0,0,0); // the color at the pixel: black
var w = context.canvas.width;
var h = context.canvas.height;
var n = inputTriangles.length;
//console.log("number of files: " + n);
// Loop over the input files
for (var f=0; f<n; f++) {
var tn = inputTriangles[f].triangles.length;
//console.log("number of triangles in this files: " + tn);
// Loop over the triangles, draw each in 2d
for(var t=0; t<tn; t++){
var vertex1 = inputTriangles[f].triangles[t][0];
var vertex2 = inputTriangles[f].triangles[t][1];
var vertex3 = inputTriangles[f].triangles[t][2];
var vertexPos1 = inputTriangles[f].vertices[vertex1];
var vertexPos2 = inputTriangles[f].vertices[vertex2];
var vertexPos3 = inputTriangles[f].vertices[vertex3];
//console.log("vertexPos1 " + vertexPos1);
//console.log("vertexPos2 " + vertexPos2);
//console.log("vertexPos3 " + vertexPos3);
context.fillStyle =
"rgb(" + Math.floor(inputTriangles[f].material.diffuse[0]*255)
+","+ Math.floor(inputTriangles[f].material.diffuse[1]*255)
+","+ Math.floor(inputTriangles[f].material.diffuse[2]*255) +")"; // diffuse color
var path=new Path2D();
path.moveTo(w*vertexPos1[0],h*vertexPos1[1]);
path.lineTo(w*vertexPos2[0],h*vertexPos2[1]);
path.lineTo(w*vertexPos3[0],h*vertexPos3[1]);
path.closePath();
context.fill(path);
} // end for triangles
} // end for files
} // end if triangle files found
} // end draw input triangles
// put random points in the boxes from the class github
function drawRandPixelsInInputBoxes(context) {
var inputBoxes = getInputBoxes();
var w = context.canvas.width;
var h = context.canvas.height;
var imagedata = context.createImageData(w,h);
const PIXEL_DENSITY = 0.1;
var numCanvasPixels = (w*h)*PIXEL_DENSITY;
if (inputBoxes != String.null) {
var x = 0; var y = 0; // pixel coord init
var lx = 0; var rx = 0; // input lx, rx from boxes.json
var by = 0; var ty = 0; // input by, ty from boxes.json
var fz = 0; var rz = 0; // input fz, rz from boxes.json
var numBoxPixels = 0; // init num pixels in boxes
var c = new Color(0,0,0,0); // init the box color
var n = inputBoxes.length; // the number of input boxes
//console.log("number of ellipses: " + n);
// Loop over the ellipsoids, draw rand pixels in each
for (var b=0; b<n; b++) {
// input lx,rx,by,ty on canvas
lx = w*inputBoxes[b].lx;
rx = w*inputBoxes[b].rx;
by = h*inputBoxes[b].by;
ty = h*inputBoxes[b].ty;
numBoxesPixels = (rx-lx)*(ty-by); // projected box area
numBoxesPixels *= PIXEL_DENSITY; // percentage of box area to render to pixels
numBoxesPixels = Math.round(numBoxesPixels);
//console.log("num box pixels: "+numBoxesPixels);
c.change(
inputBoxes[b].diffuse[0]*255,
inputBoxes[b].diffuse[1]*255,
inputBoxes[b].diffuse[2]*255,
255); // box diffuse color
for (var p=0; p<numBoxesPixels; p++) {
do {
x = Math.floor(Math.random()*w);
y = Math.floor(Math.random()*h);
} while ( x<lx || x>rx || y>ty || y<by ) // inside the projection
drawPixel(imagedata,x,y,c);
//console.log("color: ("+c.r+","+c.g+","+c.b+")");
//console.log("x: " + x);
//console.log("y: " + y);
} // end for pixels in box
} // end for boxes
context.putImageData(imagedata, 0, 0);
} // end if boxes found
} // end draw rand pixels in input boxes
//draw 2d projections boxes from the JSON file at class github
function drawInputBoxesUsingPaths(context) {
var inputBoxes = getInputBoxes();
var n = inputBoxes.length; // the number of input boxes
if (inputBoxes != String.null) {
var w = context.canvas.width;
var h = context.canvas.height;
var c = new Color(0,0,0,0); // the color at the pixel: black
var x = 0; var y = 0; // pixel coord init
var lx = 0; var rx = 0; // input lx, rx from boxes.json
var by = 0; var ty = 0; // input by, ty from boxes.json
var fz = 0; var rz = 0; // input fz, rz from boxes.json
//console.log("number of files: " + n);
// Loop over the input files
for (var b=0; b<n; b++) {
// input lx,rx,by,ty on canvas
lx = w*inputBoxes[b].lx;
rx = w*inputBoxes[b].rx;
by = h*inputBoxes[b].by;
ty = h*inputBoxes[b].ty;
context.fillStyle =
"rgb(" + Math.floor(inputBoxes[b].diffuse[0]*255)
+","+ Math.floor(inputBoxes[b].diffuse[1]*255)
+","+ Math.floor(inputBoxes[b].diffuse[2]*255) +")"; // diffuse color
var path=new Path2D();
path.moveTo(lx,ty);
path.lineTo(lx,by);
path.lineTo(rx,by);
path.lineTo(rx,ty);
path.closePath();
context.fill(path);
} // end for files
} // end if box files found
} // end draw input boxes
/* main -- here is where execution begins after window load */
function main() {
// Get the canvas and context
var canvas = document.getElementById("viewport");
var context = canvas.getContext("2d");
// Create the image
//drawRandPixels(context);
// shows how to draw pixels
//drawRandPixelsInInputEllipsoids(context);
// shows how to draw pixels and read input file
//drawInputEllipsoidsUsingArcs(context);
// shows how to read input file, but not how to draw pixels
//drawRandPixelsInInputTriangles(context);
// shows how to draw pixels and read input file
//drawInputTrainglesUsingPaths(context);
// shows how to read input file, but not how to draw pixels
drawRandPixelsInInputBoxes(context);
// shows how to draw pixels and read input file
//drawInputBoxesUsingPaths(context);
// shows how to read input file, but not how to draw pixels
}