From 906eeeb5e11b7486d534dc7e4011e01bf05d7094 Mon Sep 17 00:00:00 2001 From: Sam <34695753+SamEyeBam@users.noreply.github.com> Date: Mon, 3 Jan 2022 10:00:37 +1300 Subject: [PATCH] initial commit --- animate 0.2/animate 0.1/animation.html | 521 +++++++++++++++++++++++++ animate 0.2/css/styles.css | 13 + animate 0.2/index.html | 17 + animate 0.2/js/index.js | 141 +++++++ animate 0.2/js/math.js | 81 ++++ animate 0.2/js/objects.js | 460 ++++++++++++++++++++++ 6 files changed, 1233 insertions(+) create mode 100644 animate 0.2/animate 0.1/animation.html create mode 100644 animate 0.2/css/styles.css create mode 100644 animate 0.2/index.html create mode 100644 animate 0.2/js/index.js create mode 100644 animate 0.2/js/math.js create mode 100644 animate 0.2/js/objects.js diff --git a/animate 0.2/animate 0.1/animation.html b/animate 0.2/animate 0.1/animation.html new file mode 100644 index 0000000..268acab --- /dev/null +++ b/animate 0.2/animate 0.1/animation.html @@ -0,0 +1,521 @@ +<!DOCTYPE html> +<html> +<!-- ahhh --> +<body> + + <canvas id="myCanvas" width="1920" height="1080" style="border:1px solid #d3d3d3;"> + Your browser does not support the HTML5 canvas tag.</canvas> + <script> + function rad(degrees) { + var pi = Math.PI; + return degrees * (pi / 180); + } + + + function colourToText(colour) { + return "rgb(" + colour[0] + "," + colour[1] + "," + colour[2] + ")" + + } + function LerpRGB(a, b, t) { + + if (t < 0) { + t *= -1 + } + var newColor = [0, 0, 0]; + newColor[0] = a[0] + (b[0] - a[0]) * t; + newColor[1] = a[1] + (b[1] - a[1]) * t; + newColor[2] = a[2] + (b[2] - a[2]) * t; + return newColor; + } + + function draw_shape(sides, radius, rotation, x, y, colour) { + var rot = Math.round((sides - 2) * 180 / sides * 2) + var piv = 360 / sides; + var stt = 0.5 * Math.PI - rad(rot) + rad(rotation); + var end = 0; + + for (let i = 1; i < sides + 1; i++) { + end = stt + rad(rot); + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt, end, 0); + ctx.strokeStyle = colour; + ctx.stroke(); + + stt = end + -(rad(rot - piv)) //+rad(30); + + } + } + + function draw_shape_sub(sides, radius, rotation, x, y, colour) { + var denominator = sides - 2 + //var rot = Math.round((sides - 2) * 180 / sides * 2) + var rot = (Math.round((sides - 2) * 180 / sides * 2)) * (1 / denominator) + var piv = 360 / sides; + var l = 1 + var stt = 0.5 * Math.PI - rad(rot) * l + rad(rotation); + var end = 0; + + for (let i = 1; i < sides + 1; i++) { + end = (stt + rad(rot)); + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt, end, 0); + ctx.strokeStyle = colour; + ctx.stroke(); + + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt - rad(rot) * l, end - rad(rot) * l, 0); + ctx.strokeStyle = colour; + ctx.stroke(); + + stt = end + -(rad(rot - piv)) //+rad(30); + + } + } + + function draw_shape_fill(sides, radius, rotation, x, y, colour) { + var rot = Math.round((sides - 2) * 180 / sides * 2) + var piv = 360 / sides; + var stt = 0.5 * Math.PI - rad(rot) + rad(rotation); + var end = 0; + var n = radius / ((radius / 10) * (radius / 10)) //pixel correction for mid leaf + + for (let i = 1; i < sides + 1; i++) { + end = stt + rad(rot); + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt - (stt - end) / 2, end + rad(n), 0); + ctx.fillStyle = colour; + ctx.fill(); + + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt, end - (end - stt) / 2 + rad(n), 0); + ctx.fillStyle = colour; + ctx.fill(); + + + stt = end + -(rad(rot - piv)) //+rad(30); + + } + } + + function Draw_Shape_accident(sides, radius, rotation, x, y, colour) { + var rot = Math.round((sides - 2) * 180 / sides * 2) + var piv = 360 / sides; + var stt = 0.5 * Math.PI - rad(rot) //+ rad(rotation); + var end = 0; + var n = radius / ((radius / 10) * (radius / 10)) //pixel correction for mid leaf + + for (let i = 1; i < sides + 1; i++) { + end = stt + rad(rot); + + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt - (stt - end + rad(rotation)) / 2, end + rad(n), 0); + ctx.strokeStyle = colour; + ctx.stroke(); + + + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i - rotation)) * radius, y + Math.sin(rad(90 + piv * i - rotation)) * radius, radius, stt, end - (end - stt - rad(rotation)) / 2 + rad(n), 0); + ctx.strokeStyle = colour; + ctx.stroke(); + + + stt = end + -(rad(rot - piv)) //+rad(30); + } + + } + + function Draw_Spiral_Pattern(sides, radius, rotation, x, y, colour) { + var rot = Math.round((sides - 2) * 180 / sides * 2) + var piv = 360 / sides; + var stt = 0.5 * Math.PI - rad(rot) //+ rad(rotation); + var end = 0; + var n = radius / ((radius / 10) * (radius / 10)) //pixel correction for mid leaf + + for (let i = 1; i < sides + 1; i++) { + end = stt + rad(rot); + + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt + rad(rotation) - (stt - end) / 2, end + rad(rotation) + rad(n), 0); + ctx.strokeStyle = colour; + ctx.stroke(); + + + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i - rotation)) * radius, y + Math.sin(rad(90 + piv * i - rotation)) * radius, radius, stt - rad(rotation), end - (end - stt) / 2 + rad(n) - rad(rotation), 0); + ctx.strokeStyle = colour; + ctx.stroke(); + + + stt = end + -(rad(rot - piv)) //+rad(30); + } + + } + + function Draw_Spiral(sides, radius, rotation, x, y, colour) { + var rot = Math.round((sides - 2) * 180 / sides * 2) + var piv = 360 / sides; + var stt = 0.5 * Math.PI - rad(rot) + rad(rotation); + var end = 0; + var n = radius / ((radius / 10) * (radius / 10)) //pixel correction for mid leaf + + for (let i = 1; i < sides + 1; i++) { + end = stt + rad(rot); + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt - (stt - end) / 2, end + rad(n), 0); + ctx.strokeStyle = colour; + ctx.stroke(); + + /* + ctx.beginPath(); + ctx.arc(x + Math.cos(rad(90 + piv * i + rotation)) * radius, y + Math.sin(rad(90 + piv * i + rotation)) * radius, radius, stt, end - (end - stt) / 2 + rad(n), 0); + ctx.fillStyle = colour; + ctx.fill(); + */ + + stt = end + -(rad(rot - piv)) //+rad(30); + } + + } + + function render_clear() { + ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); + ctx.fillStyle = "black"; + ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); + } + + function draw_triangle(ang, radius) { + + ctx.beginPath(); + ctx.moveTo(startx + Math.cos(rad(90 + 120 * 1 + ang)) * radius, starty + Math.sin(rad(90 + 120 * 1 + ang)) * radius); + ctx.lineTo(startx + Math.cos(rad(90 + 120 * 2 + ang)) * radius, starty + Math.sin(rad(90 + 120 * 2 + ang)) * radius); + ctx.lineTo(startx + Math.cos(rad(90 + 120 * 3 + ang)) * radius, starty + Math.sin(rad(90 + 120 * 3 + ang)) * radius); + ctx.lineTo(startx + Math.cos(rad(90 + 120 * 1 + ang)) * radius, starty + Math.sin(rad(90 + 120 * 1 + ang)) * radius); + ctx.strokeStyle = "red"; + ctx.stroke(); + } + + + var c = document.getElementById("myCanvas"); + var ctx = c.getContext("2d"); + + var sides_org = 3; + var r = 300; + var deg_per_sec = 30; + var time = 120; + var fps = 60; + + var rot_org = Math.round((sides_org - 2) * 180 / sides_org * 2) + var piv = 360 / sides_org; + var angle = 0; + + startx = 1920 / 2//1875/2; + starty = 1080 / 2//950/2;//-(Math.tan(30*Math.PI/180)*width/2); + + j = 0; + i = 0; + + + n_angle = 0 + i_angle = 0 + tmp_a = angle + //var capturer = new CCapture( { format: 'webm' } ); + /* + var capturer = new CCapture( { + format: 'webm', + framerate: 60, + verbose: true + } ); + */ + //capturer.start(); + + + /* + function animate() { + requestAnimationFrame( animate ); + test(); + } + */ + + delMe = 0 + + function render() { + + if (i < time / (1 / fps)) { + setTimeout(() => { + render(); + //requestAnimationFrame(test); + angle = j; + render_clear(); + r = 400 + tmpA = (Math.asin(Math.cos(rad(angle)))) / (2 * Math.PI) + .75 + tmpB = Math.cos(rad(angle)) / 4 + 0.75 + tmpC = Math.cos(angle * (3 * Math.PI / 180)) / 4 + 0.75 + tmpD = (Math.sin(30 * Math.PI / 180)) / Math.sin((150 - (angle - 120 * Math.floor(angle / 120)) * Math.PI / 180)) + fl = angle - 120 * Math.floor(angle / 120) + + tmpE = 1 / (2 * Math.cos(Math.PI / 3 - (fl * Math.PI / 180))) + + //console.log(tmpE) + //console.log("angle: " + angle + " tmpC: " +Math.round(tmpC * 1000) / 1000) + /* + console.log(Math.cos(0*(3*Math.PI/180))/4+0.75) + console.log(Math.cos(30*(3*Math.PI/180))/4+0.75) + console.log(Math.cos(60*(3*Math.PI/180))/4+0.75) + console.log(Math.cos(180*(3*Math.PI/180))/4+0.75) + */ + s = 400 + speed_mult = 1 + tmpF = 1 / (2 * Math.sin(Math.PI / 180 * (150 - angle * speed_mult + 120 * Math.floor(angle * speed_mult / 120)))) + + tmpAng = n_angle + fl1 = (n_angle - 2 * Math.PI / 3 * (Math.floor(3 * n_angle / (2 * Math.PI)))) + t1 = Math.abs(Math.sin(Math.PI / 3 - fl1) / (2 * (Math.cos(Math.PI / 3 - fl1)) ** 2)) + + n_angle += 1 / (t1 + 1) + + new_angle = -120 * tmpF + 120 + tmpG = 1 / (2 * Math.sin(Math.PI / 180 * (150 - angle * speed_mult + 120 * Math.floor(angle * speed_mult / 120)))) + + //console.log("angle: " + t1 + " | fl1 : "+ fl1) + angle_accident = 120 * Math.floor(angle * speed_mult / 120) + + out_angle = 0 + + i_angle += angle - tmp_a + tmp_a = angle + if (i_angle > 120) { + i_angle -= 120 + + } + + if (i_angle < 60) { + out_angle = 120 / (2 * Math.cos(rad(i_angle))) - 60 + + } + else { + out_angle = -120 / (2 * Math.cos(2 * Math.PI / 3 - (i_angle * Math.PI / 180))) + 180 + } + //console.log("angle: " + i_angle + " | out A: " + out_angle) + tmpFinal = 1 / (2 * Math.sin(Math.PI / 180 * (150 - out_angle * speed_mult + 120 * Math.floor(out_angle * speed_mult / 120)))) + //console.log(400*tmpFinal-delMe) + delMe = 400 * tmpFinal + //draw_triangle(180+0,s) + + //draw_triangle(180+angle*speed_mult,s*tmpF) + + //draw_triangle(180+out_angle*speed_mult,s*tmpFinal) + //draw_triangle(180+new_angle*speed_mult,s*tmpG) + + + for (let i = 0; i < 25; i++) { + draw_triangle(180+out_angle*speed_mult*i,s*tmpFinal**i) + //draw_triangle(180+angle*speed_mult*i,s*tmpF**i) + + } + + height = 400 + L = height / 2 * (Math.asin(Math.cos(angle * Math.PI / height)) / Math.PI) + height * 3 / 4 + L2 = -(.5 * angle - height * Math.floor(angle / (height * 2))) + height + B = rad(180) - Math.asin(height * Math.sin(rad(30)) / L2) + theFinalAngle = rad(150) - B + console.log("L :" + L2 + " | B: " + B * 180 / Math.PI) + //draw_triangle(180+ (theFinalAngle*180/Math.PI) ,L) + + //draw_triangle((angle-180)/2.8,tmpA*400) + //draw_shape(7,300,0,startx,starty,"red"); + //draw_shape_sub(7,300,0,startx,starty,"blue"); + //draw_shape_fill(5,300,angle,startx,starty,"purple"); + /* + draw_shape_fill(5,300,angle,startx,starty,"purple"); + draw_shape_fill(5,115,-(angle+108),startx,starty,"red"); + draw_shape_fill(5,44,angle+108,startx,starty,"yellow"); + + draw_shape(5,300,angle,startx,starty,"red"); + draw_shape(5,115,-(angle+108),startx,starty,"yellow"); + draw_shape(5,44,angle+108,startx,starty,"purple"); + */ + //Draw_Spiral(4,400,0,startx,starty,'red'); + //Draw_Spiral_Pattern(50, 250, angle, startx, starty, 'red') + //Draw_Shape_accident(4,300,angle,startx,starty,'red') + //Draw_Spiral(10,400,-angle,startx,starty,'red'); + + //draw_shape(99,300,angle,startx,starty,"red"); + //draw_triangle(0); + + //draw_tri(angle); + + //draw_test_fill(angle); + + //draw_shape(5,300,angle,startx,starty); + //draw_shape(3,300,-angle,startx,starty) + + //draw_shape(7,400,angle,startx,starty) + //draw_shape(6,r,-angle,startx,starty) + + + /* + colour1 = [137, 54, 255]; + colour2 = [158, 255, 54]; + ncolour = LerpRGB(colour1, colour2, Math.cos(rad(angle))); + */ + + colour1 = [137, 54, 255]; + colour2 = [158, 255, 54]; + //Draw_nodal_expanding(2.5,100000,angle*2+33000,angle*0, colour1,colour2,0.5, 5) + //Draw_nodal_expanding(2.5,10000,angle,angle*0, colour1,colour2,0.5, 1) + //console.log(angle*2) + //draw_shape(55,300,angle,startx,starty,"pink") + //Draw_Phyllotaxis(angle/5000+1); + //draw_shape_fill(4,400,angle,startx,starty,colourToText(ncolour)); + + //Draw_rectangle_pattern1(angle, 180, "purple"); + /* + for (let z = 0; z < 360; z+=360/45) { + Draw_rectangle_pattern1(z+angle,200,"blue"); + + } + */ + + + + + //capturer.capture( document.getElementById("myCanvas") ); + + }, (1000 / fps)); + j += deg_per_sec * 1 / fps; + i += 1; + } + else { + //capturer.stop(); + //capturer.save(); + } + } + + render() + //render_clear() + //Draw_nodal(500, 100,33) + //draw_shape_fill(5,300,angle,startx,starty,"purple"); + //draw_shape_fill(5,115,angle+108,startx,starty,"red"); + //draw_shape(5,300,angle,startx,starty); + //Draw_center(); + + + function Draw_Phyllotaxis(angle) { + colour1 = [255, 170, 0]; + colour2 = [255, 0, 221]; + + var c = 32; + + for (let n = 50; n < 300; n += 1) { + ncolour = LerpRGB(colour1, colour2, Math.cos(rad(n / 2))); + var a = n * (angle)//137.5; + var r = c * Math.sqrt(n); + var x = r * Math.cos(a) + startx; + var y = r * Math.sin(a) + starty; + + ctx.beginPath(); + ctx.arc(x, y, 4, 0, 2 * Math.PI); + ctx.fillStyle = colourToText(ncolour); + ctx.fill(); + + } + + + } + + function draw_test_fill(angle) { + + ctx.beginPath(); + ctx.arc(1000, 500, 300, 0, rad(angle)); + ctx.fillStyle = "purple"; + ctx.fill(); + + } + + function Draw_nodal_expanding(expand, points, step, rotate, colour1, colour2, colour_change, line_width) { + var angle = 360 / points * step + + var start_angle = angle; + var done = false; + var total_moves = 1; + var length = expand; + + + for (let z = 1; z <= 2500; z++) { + ctx.beginPath(); + //ctx.moveTo(startx, starty); + ncolour = LerpRGB(colour1, colour2, Math.cos(rad(z * colour_change))); + + ctx.moveTo(startx + (Math.cos(rad(angle * (z - 1) + rotate)) * (length - expand)), starty + (Math.sin(rad(angle * (z - 1) + rotate)) * (length - expand))); + ctx.lineTo(startx + (Math.cos(rad(angle * z + rotate)) * length), starty + (Math.sin(rad(angle * z + rotate)) * length)); + length += expand; + ctx.lineWidth = line_width;//try 1 + ctx.strokeStyle = colourToText(ncolour); + ctx.stroke(); + } + + + } + + function Draw_nodal(radius, points, step, rotate, colour) { + var angle = 360 / points * step + ctx.beginPath(); + var start_angle = angle; + var done = false; + var total_moves = 1; + ctx.moveTo(startx + (Math.cos(rad(angle + rotate)) * radius), starty + (Math.sin(rad(angle + rotate)) * radius)); + + while (done != true) { + if ((total_moves * step) % points != 0) { + total_moves++ + } + else { + total_moves++ + done = true + } + } + for (let z = 1; z <= total_moves; z++) { + ctx.lineTo(startx + (Math.cos(rad(angle * z + rotate)) * radius), starty + (Math.sin(rad(angle * z + rotate)) * radius)); + } + ctx.strokeStyle = colour; + ctx.stroke(); + + } + + function Draw_rectangle_pattern1(angle, size, colour) { + ctx.save(); + ctx.translate(startx, starty)//-(Math.sin(rad(angle)) *startx)); + ctx.rotate(rad(angle + 180)); + ctx.beginPath(); + ctx.strokeStyle = colour; + ctx.rect(-size, -size, size, size); + ctx.stroke(); + ctx.restore(); + + } + + function draw_tri(angle) { + draw_shape(3, 520, angle, startx + 260, starty - 150); + draw_shape(3, 520, angle + 60, startx - 260, starty - 150); + draw_shape(3, 520, angle + 120, startx, starty + 300); + + } + + function Draw_center() { + ctx.beginPath(); + ctx.moveTo(startx - 100, starty); + ctx.lineTo(startx + 100, starty); + ctx.moveTo(startx, starty - 100); + ctx.lineTo(startx, starty + 100); + ctx.strokeStyle = "red"; + ctx.stroke(); + + } + + + + </script> + +</body> + +</html> \ No newline at end of file diff --git a/animate 0.2/css/styles.css b/animate 0.2/css/styles.css new file mode 100644 index 0000000..a91e757 --- /dev/null +++ b/animate 0.2/css/styles.css @@ -0,0 +1,13 @@ +body { + height: 100vh; + min-height: 100vh; + margin: 0; +} + + +#mainCanvas { + width: 100%; + height: 100%; + min-height: 100vh; + background-color: black; +} \ No newline at end of file diff --git a/animate 0.2/index.html b/animate 0.2/index.html new file mode 100644 index 0000000..f5ef239 --- /dev/null +++ b/animate 0.2/index.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Document</title> + <link rel="stylesheet" href="./css/styles.css"> + +</head> +<body> + <canvas onload="index.js" id="mainCanvas"></canvas> +</body> +<script src="./js/math.js" type="text/javascript"></script> +<script src="./js/objects.js" type="text/javascript"></script> +<script src="./js/index.js"></script> +</html> \ No newline at end of file diff --git a/animate 0.2/js/index.js b/animate 0.2/js/index.js new file mode 100644 index 0000000..3b6bbd9 --- /dev/null +++ b/animate 0.2/js/index.js @@ -0,0 +1,141 @@ +//jshint esversion:8 +i = 0; +const canvas = document.getElementById("mainCanvas"); +canvas.width = canvas.clientWidth; +canvas.height = canvas.clientHeight; +var ctx = canvas.getContext("2d"); +centerX = canvas.width / 2; +centerY = canvas.height / 2; +let DEBUGTools = new DebugTools(ctx, centerX, centerY); +let testPoly = new RandomPolygon(ctx, 300, centerX, centerY, 6, "crimson"); +let testSquare = new Square( + ctx, + 500, + centerX, + centerY, + 0, + 0, + 0, + 0, + "blueViolet", + false +); +let testCube = new Cube( + ctx, + 500, + centerX, + centerY, + 0, + 0, + 0, + 0, + "blueViolet", + false +); +let testTet = new Tetrahedron( + ctx, + 300, + centerX, + centerY, + 0, + 0, + 0, + 0, + "blueViolet", + false +); + +let tetArray = []; +// for (let i = 0; i < 100; i++) { +// tetArray.push( +// new Tetrahedron( +// ctx, +// 300 - i * (300/100), +// centerX, +// centerY + 140, +// 0, +// 45, +// 0, +// 35.264, +// "rgb(" + (255 - i * (255/100)) + ", 0, 0)", +// false +// ) +// ); +// } + +// testTet.rotateSetOrg(60,0,35.264); +// testTet.rotateAddY(15); +// testTet.rotateAddX(15); +// testCube.rotateSetOrg(45,0,35.264); +// testTet.globalY +=140; +// testTet.rotateAddY(15); + +tetArray.push( + new Tetrahedron( + ctx, + 200, + centerX, + centerY, + 0, + 0, + 0, + 0, + "rgb(255, 0, 0)", + false + ) +); +tetArray.push( + new Tetrahedron( + ctx, + 115, + centerX, + centerY, + 0, + 0, + 0, + 0, + "rgb(200, 0, 0)", + false + ) +); + +let drawStack = []; +// drawStack.push(testTet); +drawStack.push(...tetArray); + +// drawStack[0].rotateAddY(30); +drawStack[1].rotateAddY(45); +drawStack[1].rotateAddX(35.264); +drawStack[1].rotateAddZ(30); +drawStack[0].rotateAddY(45); +drawStack[0].rotateAddX(35.264); +function main() { + setTimeout(() => { + main(); + ctx.clearRect(0, 0, canvas.width, canvas.height); + // drawStack[1].rotateAddX(1); + // drawStack[1].rotateAddY(1); + // drawStack[1].rotateAddZ(.2); + + // for (let i = 0; i < drawStack.length; i++) { + // drawStack[i].rotateAddY(0.005*(i+1)); + + // } + + // drawStack.push(testCube); + // testTet.rotateAddX(.3); + // testTet.rotateAddY(.05); + // testTet.rotateAddZ(.5); + + // testCube.rotateAddX(1); + // testCube.rotateAddY(1); + // testCube.rotateAddZ(1); + + for (let i = 0; i < drawStack.length; i++) { + drawStack[i].draw(); + } + DEBUGTools.drawCenter(50); + }, 1000 / 60); +} + +window.onload = main; diff --git a/animate 0.2/js/math.js b/animate 0.2/js/math.js new file mode 100644 index 0000000..a3fb25e --- /dev/null +++ b/animate 0.2/js/math.js @@ -0,0 +1,81 @@ +function degToRad(deg) { + return (deg * Math.PI) / 180; +} + +function rotateMatrix2d(p, angle) { + // cos0 sin0 + // -sin0 cos0 + const angleD = degToRad(angle); + const r = [ + [Math.cos(angleD), Math.sin(angleD)], + [-Math.sin(angleD), Math.cos(angleD)], + ]; + const newPoint = [ + p[0] * r[0][0] + p[1] * r[0][1], + p[0] * r[1][0] + p[1] * r[1][1], + ]; + return newPoint; +} + +function rotateMatrix3dX(p, angle) { + // cos0 sin0 + // -sin0 cos0 + const angleD = degToRad(angle); + const r = [ + [1, 0, 0], + [0, Math.cos(angleD), -Math.sin(angleD)], + [0, Math.sin(angleD), Math.cos(angleD)], + ]; + const newPoint = [ + p[0] * r[0][0] + p[1] * r[0][1] + p[2] * r[0][2], + p[0] * r[1][0] + p[1] * r[1][1] + p[2] * r[1][2], + p[0] * r[2][0] + p[1] * r[2][1] + p[2] * r[2][2], + ]; + return newPoint; +} + +function rotateMatrix3dY(p, angle) { + // cos0 sin0 + // -sin0 cos0 + const angleD = degToRad(angle); + const r = [ + [Math.cos(angleD), 0, Math.sin(angleD)], + [0, 1, 0], + [-Math.sin(angleD), 0, Math.cos(angleD)], + ]; + const newPoint = [ + p[0] * r[0][0] + p[1] * r[0][1] + p[2] * r[0][2], + p[0] * r[1][0] + p[1] * r[1][1] + p[2] * r[1][2], + p[0] * r[2][0] + p[1] * r[2][1] + p[2] * r[2][2], + ]; + return newPoint; +} +function rotateMatrix3dZ(p, angle) { + // cos0 sin0 + // -sin0 cos0 + const angleD = degToRad(angle); + const r = [ + [Math.cos(angleD), -Math.sin(angleD), 0], + [Math.sin(angleD), Math.cos(angleD), 0], + [0, 0, 1], + ]; + const newPoint = [ + p[0] * r[0][0] + p[1] * r[0][1] + p[2] * r[0][2], + p[0] * r[1][0] + p[1] * r[1][1] + p[2] * r[1][2], + p[0] * r[2][0] + p[1] * r[2][1] + p[2] * r[2][2], + ]; + return newPoint; +} + +function projectionOrth(v) { + const p = [ + [1, 0, 0], + [0, 1, 0], + ]; + + const nPoint = [ + p[0][0] * v[0] + p[0][1] * v[1] + p[0][2] * v[2], + p[1][0] * v[0] + p[1][1] * v[1] + p[1][2] * v[2], + ]; + return nPoint; +} diff --git a/animate 0.2/js/objects.js b/animate 0.2/js/objects.js new file mode 100644 index 0000000..f656859 --- /dev/null +++ b/animate 0.2/js/objects.js @@ -0,0 +1,460 @@ +// import math from "math.js"; + +class Cube { + constructor(ctx, width, x, y, z, rx, ry, rz, color, solid) { + this.ctx = ctx; + this.color = color; + this.width = width; + this.solid = solid; + this.maxRadius = this.width / Math.cos(degToRad(45)); //TODO trig + this.pointsOrg = []; + this.pointsOrg.push([-this.width / 2, -this.width / 2, -this.width / 2]); + this.pointsOrg.push([this.width / 2, -this.width / 2, -this.width / 2]); + this.pointsOrg.push([this.width / 2, this.width / 2, -this.width / 2]); + this.pointsOrg.push([-this.width / 2, this.width / 2, -this.width / 2]); + + this.pointsOrg.push([-this.width / 2, -this.width / 2, this.width / 2]); + this.pointsOrg.push([this.width / 2, -this.width / 2, this.width / 2]); + this.pointsOrg.push([this.width / 2, this.width / 2, this.width / 2]); + this.pointsOrg.push([-this.width / 2, this.width / 2, this.width / 2]); + + this.points = JSON.parse(JSON.stringify(this.pointsOrg)); + this.centerX = 0; + this.centerY = 0; + this.globalX = x; + this.globalY = y; + this.globalZ = z; //might not need + + this.rotX = 0; + this.rotY = 0; + this.rotZ = 0; + } + draw() { + let projPoints = []; + for (let i = 0; i < this.points.length; i++) { + projPoints.push(projectionOrth(this.points[i])); + } + + if (this.solid) { + this.ctx.fillStyle = this.color; + } else { + this.ctx.strokeStyle = this.color; + } + this.ctx.beginPath(); + this.ctx.moveTo( + this.globalX + projPoints[0][0], + this.globalY + projPoints[0][1] + ); + for (let i = 1; i < 4; i++) { + this.ctx.lineTo( + this.globalX + projPoints[i][0], + this.globalY + projPoints[i][1] + ); + } + this.ctx.lineTo( + this.globalX + projPoints[0][0], + this.globalY + projPoints[0][1] + ); + + this.ctx.moveTo( + this.globalX + projPoints[4][0], + this.globalY + projPoints[4][1] + ); + for (let i = 5; i < 8; i++) { + this.ctx.lineTo( + this.globalX + projPoints[i][0], + this.globalY + projPoints[i][1] + ); + } + this.ctx.lineTo( + this.globalX + projPoints[4][0], + this.globalY + projPoints[4][1] + ); + + for (let i = 0; i < 4; i++) { + this.ctx.moveTo( + this.globalX + projPoints[i][0], + this.globalY + projPoints[i][1] + ); + this.ctx.lineTo( + this.globalX + projPoints[i + 4][0], + this.globalY + projPoints[i + 4][1] + ); + } + + if (this.solid) { + this.ctx.fill(); + } else { + this.ctx.stroke(); + } + } + drawCenter(x, y, width, color) { + let testLineX = new Line( + this.ctx, + [x + this.centerX - width, y + this.centerY], + [x + this.centerX + width, y + this.centerY], + color + ); + let testLineY = new Line( + this.ctx, + [x + this.centerX, y + this.centerY - width], + [x + this.centerX, y + this.centerY + width], + color + ); + + testLineX.draw(); + testLineY.draw(); + } + rotateAddX(deg) { + this.rotX += deg; + for (let i = 0; i < this.points.length; i++) { + this.points[i] = rotateMatrix3dX(this.points[i], deg); + } + } + rotateAddY(deg) { + this.rotY += deg; + for (let i = 0; i < this.points.length; i++) { + this.points[i] = rotateMatrix3dY(this.points[i], deg); + } + } + rotateAddZ(deg) { + this.rotZ += deg; + for (let i = 0; i < this.points.length; i++) { + this.points[i] = rotateMatrix3dZ(this.points[i], deg); + } + console.log(this.rotZ); + } + rotateSetOrg(x, y, z) { + for (let i = 0; i < this.points.length; i++) { + this.pointsOrg[i] = rotateMatrix3dX(this.pointsOrg[i], x); + } + for (let i = 0; i < this.points.length; i++) { + this.pointsOrg[i] = rotateMatrix3dZ(this.pointsOrg[i], y); + } + for (let i = 0; i < this.points.length; i++) { + this.pointsOrg[i] = rotateMatrix3dZ(this.pointsOrg[i], z); + } + + this.points = JSON.parse(JSON.stringify(this.pointsOrg)); + } +} +class Tetrahedron { + constructor(ctx, width, x, y, z, rx, ry, rz, color, solid) { + this.ctx = ctx; + this.color = color; + this.width = width; + this.solid = solid; + this.maxRadius = this.width / Math.cos(degToRad(45)); //TODO trig + this.pointsOrg = []; + // this.pointsOrg.push([-1 * width, 1 * width, -1 * width]); + // this.pointsOrg.push([1 * width, 1 * width, 1 * width]); + // this.pointsOrg.push([1 * width, -1 * width, -1 * width]); + // this.pointsOrg.push([-1 * width, -1 * width, 1 * width]); + + this.pointsOrg.push([1 * width, 1 * width, 1 * width]); + this.pointsOrg.push([1 * width, -1 * width, -1 * width]); + this.pointsOrg.push([-1 * width, 1 * width, -1 * width]); + this.pointsOrg.push([-1 * width, -1 * width, 1 * width]); + + // this.pointsOrg.push([(8/9)^.5 * width, 0, -1/3 * width]); + // this.pointsOrg.push([-(2/9)^.5 * width, (2/3)^.5 * width, -1/3 * width]); + // this.pointsOrg.push([-(2/9)^.5 * width, -(2/3)^.5 * width, -1/3 * width]); + // this.pointsOrg.push([0,0, 1 * width]); + + this.points = JSON.parse(JSON.stringify(this.pointsOrg)); + this.centerX = 0; + this.centerY = 0; + this.globalX = x; + this.globalY = y; + + this.rotX = 0; + this.rotY = 0; + this.rotZ = 0; + + this.rotateSetOrg(rx, ry, rz); + + } + draw() { + let projPoints = []; + for (let i = 0; i < this.points.length; i++) { + projPoints.push(projectionOrth(this.points[i])); + } + + if (this.solid) { + this.ctx.fillStyle = this.color; + } else { + this.ctx.strokeStyle = this.color; + } + this.ctx.beginPath(); + this.ctx.moveTo( + this.globalX + projPoints[0][0], + this.globalY + projPoints[0][1] + ); + for (let i = 1; i < 3; i++) { + this.ctx.lineTo( + this.globalX + projPoints[i][0], + this.globalY + projPoints[i][1] + ); + } + this.ctx.lineTo( + this.globalX + projPoints[0][0], + this.globalY + projPoints[0][1] + ); + this.ctx.lineTo( + this.globalX + projPoints[3][0], + this.globalY + projPoints[3][1] + ); + this.ctx.lineTo( + this.globalX + projPoints[1][0], + this.globalY + projPoints[1][1] + ); + this.ctx.moveTo( + this.globalX + projPoints[3][0], + this.globalY + projPoints[3][1] + ); + this.ctx.lineTo( + this.globalX + projPoints[2][0], + this.globalY + projPoints[2][1] + ); + + if (this.solid) { + this.ctx.fill(); + } else { + this.ctx.stroke(); + } + } + drawCenter(x, y, width, color) { + let testLineX = new Line( + this.ctx, + [x + this.centerX - width, y + this.centerY], + [x + this.centerX + width, y + this.centerY], + color + ); + let testLineY = new Line( + this.ctx, + [x + this.centerX, y + this.centerY - width], + [x + this.centerX, y + this.centerY + width], + color + ); + + testLineX.draw(); + testLineY.draw(); + } + + rotateAddX(deg) { + this.rotX += deg; + for (let i = 0; i < this.points.length; i++) { + this.points[i] = rotateMatrix3dX(this.points[i], deg); + } + } + rotateAddY(deg) { + this.rotY += deg; + for (let i = 0; i < this.points.length; i++) { + this.points[i] = rotateMatrix3dY(this.points[i], deg); + } + } + rotateAddZ(deg) { + this.rotZ += deg; + for (let i = 0; i < this.points.length; i++) { + this.points[i] = rotateMatrix3dZ(this.points[i], deg); + } + console.log(this.rotZ); + } + rotateSetOrg(x, y, z) { + for (let i = 0; i < this.points.length; i++) { + this.pointsOrg[i] = rotateMatrix3dX(this.pointsOrg[i], x); + } + for (let i = 0; i < this.points.length; i++) { + this.pointsOrg[i] = rotateMatrix3dZ(this.pointsOrg[i], y); + } + for (let i = 0; i < this.points.length; i++) { + this.pointsOrg[i] = rotateMatrix3dZ(this.pointsOrg[i], z); + } + + this.points = JSON.parse(JSON.stringify(this.pointsOrg)); + } +} + +class Square { + constructor(ctx, width, x, y, color, solid) { + this.ctx = ctx; + this.color = color; + this.width = width; + this.solid = solid; + this.maxRadius = this.width / Math.cos(degToRad(45)); + this.points = []; + this.points.push([-this.width / 2, -this.width / 2, 0]); + this.points.push([this.width / 2, -this.width / 2, 0]); + this.points.push([this.width / 2, this.width / 2, 0]); + this.points.push([-this.width / 2, this.width / 2, 0]); + + this.centerX = 0; + this.centerY = 0; + this.globalX = x; + this.globalY = y; + } + draw() { + if (this.solid) { + this.ctx.fillStyle = this.color; + } else { + this.ctx.strokeStyle = this.color; + } + this.ctx.beginPath(); + this.ctx.moveTo( + this.globalX + this.points[0][0], + this.globalY + this.points[0][1] + ); + for (let i = 1; i < this.points.length; i++) { + this.ctx.lineTo( + this.globalX + this.points[i][0], + this.globalY + this.points[i][1] + ); + } + this.ctx.lineTo( + this.globalX + this.points[0][0], + this.globalY + this.points[0][1] + ); + + if (this.solid) { + this.ctx.fill(); + } else { + this.ctx.stroke(); + } + } + drawCenter(x, y, width, color) { + let testLineX = new Line( + this.ctx, + [x + this.centerX - width, y + this.centerY], + [x + this.centerX + width, y + this.centerY], + color + ); + let testLineY = new Line( + this.ctx, + [x + this.centerX, y + this.centerY - width], + [x + this.centerX, y + this.centerY + width], + color + ); + + testLineX.draw(); + testLineY.draw(); + } + rotateAdd(deg) { + for (let i = 0; i < this.points.length; i++) { + this.points[i] = rotateMatrix2d(this.points[i], deg); + } + } + rotateSet(deg) {} +} + +class RandomPolygon { + constructor(ctx, width, x, y, sides, color) { + this.ctx = ctx; + this.color = color; + this.sides = sides; + this.width = width; + this.maxRadius = this.width / Math.cos(degToRad(45)); + this.points = []; + for (let i = 0; i < sides; i++) { + const rndNumX = Math.random() * 2 - 1; + const rndNumY = Math.random() * 2 - 1; + const rndPointX = (this.width * rndNumX) / 2; + const rndPointY = (this.width * rndNumY) / 2; + this.points.push([Math.round(rndPointX), Math.round(rndPointY)]); + } + this.globalX = x; + this.globalY = y; + this.centerX; + this.centerY; + + this.findCenter(); + for (let i = 0; i < this.points.length; i++) { + this.points[i][0] -= this.centerX; + this.points[i][1] -= this.centerY; + } + this.findCenter(); + } + draw() { + this.ctx.fillStyle = this.color; + this.ctx.beginPath(); + this.ctx.moveTo( + this.globalX + this.points[0][0], + this.globalY + this.points[0][1] + ); + for (let i = 1; i < this.points.length; i++) { + this.ctx.lineTo( + this.globalX + this.points[i][0], + this.globalY + this.points[i][1] + ); + } + this.ctx.fill(); + } + drawCenter(x, y, width, color) { + let testLineX = new Line( + this.ctx, + [x + this.centerX - width, y + this.centerY], + [x + this.centerX + width, y + this.centerY], + color + ); + let testLineY = new Line( + this.ctx, + [x + this.centerX, y + this.centerY - width], + [x + this.centerX, y + this.centerY + width], + color + ); + + testLineX.draw(); + testLineY.draw(); + } + findCenter() { + let xTot = 0; + let yTot = 0; + for (let i = 0; i < this.points.length; i++) { + xTot += this.points[i][0]; + yTot += this.points[i][1]; + } + this.centerX = xTot / this.points.length; + this.centerY = yTot / this.points.length; + } +} + +class Line { + constructor(ctx, p1, p2, color) { + this.ctx = ctx; + this.p1 = p1; + this.p2 = p2; + this.color = color; + } + draw() { + this.ctx.strokeStyle = this.color; + this.ctx.beginPath(); + this.ctx.moveTo(this.p1[0], this.p1[1]); + this.ctx.lineTo(this.p2[0], this.p2[1]); + this.ctx.closePath(); + this.ctx.stroke(); + } +} + +class DebugTools { + constructor(ctx, centerX, centerY) { + this.ctx = ctx; + this.centerX = centerX; + this.centerY = centerY; + } + + drawCenter(width) { + let centerLineX = new Line( + this.ctx, + [this.centerX - width, this.centerY], + [this.centerX + width, this.centerY], + "orange" + ); + let centerLineY = new Line( + this.ctx, + [this.centerX, this.centerY - width], + [this.centerX, this.centerY + width], + "orange" + ); + centerLineX.draw(); + centerLineY.draw(); + } +}