{"id":72259,"date":"2022-09-18T04:34:57","date_gmt":"2022-09-18T08:34:57","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/?p=72259"},"modified":"2022-09-18T04:57:41","modified_gmt":"2022-09-18T08:57:41","slug":"project-3-dynamic-drawing-4","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/2022\/09\/18\/project-3-dynamic-drawing-4\/","title":{"rendered":"Project 3 &#8211; Dynamic Drawing"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<html><body><p>Controls:<br>&ndash; upper right corner = scale up<br>&ndash; lower left corner = scale down<br>&ndash; up &amp; down = triangles move to the right<br>&ndash; click &amp; hold on triangle = rotate<br>&ndash; hover over triangle = fade to white<\/p>\n\n\n\n<p>Originally I wanted to make a field of triangles, where the triangle &amp; neighboring triangles would flip out into a ripple. Initial generation of the triangular grid was easy, but when I wanted to proceed with more complicated maneuvers, I found my self going back to and rewriting my code so it was more structured and centralizing all the important parameters I wanted my operations to depend on. <\/p>\n\n\n\n<p><br>In particular, I really struggled with the rotation implementation. P5&rsquo;s scale operation acts as a blanket technique, operating on all the geometries called after it, whereas I wanted triangles to rotate individually, meaning I had to parse through every single triangle individually to find the one I wanted to rotate.  When rotating, wiggling the mouse causes the rotation to flicker, as the file is looping through draw to update the new mouse position, so keeping the mouse still when rotating pieces is advised. The rest were relatively straightforward to implement. <\/p>\n\n\n\n<div> <a class=\"p5_sketch_link\" data-width=\"600\" data-height=\"600\" href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2022\/09\/sketch-302.js\">sketch<\/a> <iframe src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/plugins\/p5-embedder\/p5_iframe.html\" class=\"p5_exampleFrame\" width=\"600\" height=\"600\"><\/iframe><pre class=\"language-javascript\"><code class=\"p5_editor language-javascript\">\/\/ Tsz Wing Clover Chau\n\/\/ Section E\n\n\nfunction setup() {\n    createCanvas(600, 600);\n    background(220);\n    text(\"p5.js vers 0.9.0 test.\", 10, 15);\n    frameRate(200);\n}\n\nvar init = true;\n\nvar row = 0;\nvar col = 0;\nvar selCol = 0;\nvar selRow = 0;\n\nvar offset = 30;\n\nvar n = 3;\nvar l = 0;\n\n\nvar leftX = 0;\nvar rightX = 0;\nvar ptX = 0;\nvar leftY = 0;\nvar rightY = 0;\nvar ptY = 0;\n\nvar mX = 0;\nvar mY = 0;\n\nvar wait = 70;\n\nvar sizeCounter = 1;\n\n\nlet neighC = [255, 255, 255];\n\nlet triList = [];\n\n\nfunction area(x1, y1, x2, y2, x3, y3) {\n    return abs(((x1*(y2-y3)) + (x2*(y3-y1))+ (x3*(y1-y2)))\/2);\n}\n\nfunction inbounds(x1, y1, x2, y2, x3, y3, mouseX, mouseY){\n    let A = area(x1, y1, x2, y2, x3, y3);\n    let A1 = area(mouseX, mouseY, x2, y2, x3, y3);\n    let A2 = area(x1, y1, mouseX, mouseY, x3, y3);\n    let A3 = area(x1, y1, x2, y2, mouseX, mouseY);\n    return (A == A1+A2+A3);\n}\n\n\n\nfunction draw() {\n    noStroke();\n    background(220);\n    \n    l = (width-(offset*(n-1)))\/n;\n    var h = sqrt(3)\/2 * l; \n\n    \/\/ init triangle grid\n    if (init){\n        if (row %4 == 0){\n            leftX = (col*(l+offset)) + offset;\n            rightX = leftX + l;\n            ptX = leftX + l\/2;\n\n            leftY = (row\/2*(h+offset\/2)) + offset;\n            rightY = leftY;\n            ptY =  leftY + h;\n\n            midY = leftY + h\/2;\n\n\n        } else if (row %4 == 2) {\n            leftX = ((col-0.5)*(l+offset)) + offset;\n            rightX = leftX + l;\n            ptX = leftX + l\/2;\n\n\n            leftY = ((row\/2)*(h+offset\/2)) + offset;\n            rightY = leftY;\n            ptY =  leftY + h;\n\n            midY = leftY + h\/2;\n\n        } else if (row %4 == 3) {\n            leftX = ((col - 0.5)*(l+offset)) + l\/2 + offset*(3\/2);\n            rightX = leftX + l;\n            ptX = leftX + l\/2;\n\n            leftY = (int(row\/2)*(h+offset\/2)) + h + offset;\n            rightY = leftY;\n            ptY = leftY - h; \n\n            midY = leftY - h\/2;\n\n\n        } else {\n            leftX = ((col-1)*(l+offset)) + l\/2 + offset*(3\/2);\n            rightX = leftX + l;\n            ptX = leftX + l\/2;\n\n            leftY = (int(row\/2)*(h+offset\/2)) + h + offset;\n            rightY = leftY;\n            ptY = leftY - h;  \n\n            midY = leftY - h\/2;\n        }\n        midX = ptX;\n\n        var cShift = false;\n        var a = 90;\n        let selC = [0, 0, 0];\n\n        append(triList, [leftX, leftY, rightX, rightY, ptX, ptY, selC, cShift,\n                         a, midX, midY, row, col]);\n\n    } else {\n\n        \/\/controlling SCALE\n        if (wait == 0 & mouseX < width\/8 && mouseY < height\/8){\n            sizeCounter += 0.01;\n        } else if (sizeCounter &gt; 1 & mouseX > width\/4 && mouseY > height*3\/4){\n            sizeCounter -= 0.01;\n            }\n        scale(sizeCounter);\n        \n        \n\n        for (let i = 0; i&lt; triList.length; i++) {\n            mX = mouseX;\n            mY = mouseY;\n            elem = triList[i];\n\n\n            fill(elem[6]);\n\n            \/\/controlling POSITION\n            if ((mouseY &gt; elem[2] & mouseY > elem[5]) ||\n                (mouseY > elem[2] && mouseY < elem[5])){\n                    elem[0] -= 0.5;\n                    elem[2] -= 0.5;\n                    elem[4] -= 0.5;\n                    elem[9] -= 0.5;\n                }\n\n            if (inbounds(elem[0], elem[1], elem[2], elem[3], \n                         elem[4], elem[5], mX, mY)) {\n                elem[7] = true;\n\n                \/\/controlling SHADE\n                if (elem[7]){\n                    for (let i = 0; i&lt; 3; i++){\n                        elem[6][i] += 10;\n                    }\n                }\n                \/\/ controlling ROTATION  (don't move mouse during rotation - it flickers)\n                if (mouseIsPressed){\n                    push();\n                    fill(255, 255, 255);\n                    translate( elem[9], elem[10]);\n                    rotate(radians(elem[8]), [elem[9], elem[10], 0]);\n                    translate(-elem[9], -elem[10]);\n                    triangle(elem[0], elem[1], elem[2], elem[3], elem[4], elem[5]);\n                    pop();\n\n                    elem[8] += 3;\n                }\n            } else {\n                elem[7] = false;\n            }\n            if (elem[8] == 90){\n                triangle(elem[0], elem[1], elem[2], elem[3], elem[4], elem[5]);\n            }\n        }\n    }\n\n\n    if (col &gt; n){\n        row ++;\n        col = 0;\n        if (rightY +h &gt; height) {\n        init = false;\n        }\n    } else {\n        col ++;\n    }\n\n    if (wait &gt; 0){\n        wait --;\n    }\n}\n<\/code><\/pre><\/div><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>Controls:&ndash; upper right corner = scale up&ndash; lower left corner = scale down&ndash; up &amp; down = triangles move to the right&ndash; click &amp; hold on triangle = rotate&ndash; hover over triangle = fade to white Originally I wanted to make a field of triangles, where the triangle &amp; neighboring triangles would flip out into &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/2022\/09\/18\/project-3-dynamic-drawing-4\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Project 3 &#8211; Dynamic Drawing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":708,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[100,121],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/72259"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/users\/708"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/comments?post=72259"}],"version-history":[{"count":6,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/72259\/revisions"}],"predecessor-version":[{"id":72269,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/72259\/revisions\/72269"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/media?parent=72259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/categories?post=72259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/tags?post=72259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}