{"id":735,"date":"2021-09-14T20:33:49","date_gmt":"2021-09-15T00:33:49","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/?p=735"},"modified":"2021-09-15T01:16:48","modified_gmt":"2021-09-15T05:16:48","slug":"grape-lineexercises","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/grape\/09\/14\/grape-lineexercises\/","title":{"rendered":"grape &#8211; LineExercises"},"content":{"rendered":"<p>A. <img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-750\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-14-at-8.01.46-PM-358x480.png\" alt=\"\" width=\"358\" height=\"480\" srcset=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-14-at-8.01.46-PM-358x480.png 358w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-14-at-8.01.46-PM.png 550w\" sizes=\"(max-width: 358px) 85vw, 358px\" \/><code><\/code><\/p>\n<pre>\/* Basically I used the example code from the lerp() page on Processing\r\n * but changed it so it would produce dotted lines for any random\r\n * pair of vectors\r\n * *\/\r\nfloat dashed_length = 20;\r\n\r\nvoid setup(){\r\n  size(400, 400);\r\n  background(100);\r\n}\r\n\r\nvoid draw(){\r\n  background(255);\r\n  int x1 = int(random(0,width));\r\n  int x2 = int(random(0,width));\r\n  int y1 = int(random(0,height));\r\n  int y2 = int(random(0,height));\r\n  \r\n  float ydif = y2-y1;\r\n  float xdif = x2-x1;\r\n  float num_lines = sqrt(pow(ydif,2) + pow(xdif,2))\/dashed_length;\r\n  print(num_lines);\r\n  \r\n  for (int i = 0; i &lt;= int(num_lines); i+=2) {\r\n    float dx1 = lerp(x1, x2, i\/num_lines) + num_lines;\r\n    float dx2 = lerp(x1, x2, (i+1)\/num_lines) + num_lines;\r\n    float dy1 = lerp(y1, y2, i\/num_lines);\r\n    float dy2 = lerp(y1, y2, (i+1)\/num_lines);\r\n    line(dx1, dy1, dx2, dy2);\r\n  }\r\n  noLoop();\r\n}\r\n<\/pre>\n<p>B. <img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-751\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_8990.gif\" alt=\"\" width=\"444\" height=\"404\" \/><\/p>\n<p>C.<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-752\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_8991-506x480.gif\" alt=\"\" width=\"506\" height=\"480\" \/><code><\/code><\/p>\n<pre>\/**\r\n * I basically altered the brownian motion tutorial from Processing\r\n * to record mouseX, mouseY instead of random movement.\r\n *\/\r\n \r\nint num = 100;\r\nint range = 6;\r\n\r\nfloat[] ax = new float[num];\r\nfloat[] ay = new float[num]; \r\nfloat[] dx = new float[num];\r\nfloat[] dy = new float[num];\r\n\/\/ float[][] a = new float[num][2]; \/\/ LIVING LINE 2D ARRAY\r\n\/\/ PVector[] a = new PVector[num]; \/\/ LIVING LINE PVECTORS\r\nvoid setup() \r\n{\r\n  size(640, 360);\r\n  for(int i = 0; i &lt; num; i++) {\r\n    ax[i] = width\/2;\r\n    ay[i] = height\/2;\r\n    dx[i] = 0;\r\n    dy[i] = 0;\r\n  }\r\n  \/* LIVING LINE PVECTORS\r\n  for(int i = 0; i &lt; num; i++) {\r\n    a[i] = new PVector();\r\n    a[i].x = width\/2;\r\n    a[i].y = height\/2;\r\n  }\r\n  *\/\r\n  \/* LIVING LINE 2D ARRAY\r\n  for(int i = 0; i &lt; num; i++){\r\n    for(int j = 0; j &lt; 2; j++){\r\n      a[i][j] = width\/2;\r\n    }\r\n  }\r\n  *\/\r\n  frameRate(30);\r\n}\r\n\r\nvoid draw() \r\n{\r\n  background(255);\r\n  \r\n  \/\/ Shift all elements 1 place to the left\r\n  for(int i = 1; i &lt; num; i++) {\r\n    dx[i-1] = dx[i];\r\n    dy[i-1] = dy[i];\r\n    ax[i-1] = ax[i];\/\/ + random(-range, range); SPICY LINE COMMENT\r\n    ay[i-1] = ay[i];\/\/ + random(-range, range); SPICY LINE COMMENT\r\n  }\r\n  \/* LIVING LINE PVECTORS\r\n  for(int i = 1; i &lt; num; i++) {\r\n    a[i-1].x = a[i].x;\r\n    a[i-1].y = a[i].y;\r\n  }\r\n  *\/\r\n  \/* \/\/ LIVING LINE 2D ARRAY\r\n  for(int i = 1; i &lt; num; i++){\r\n    a[i-1][0] = a[i][0]; \r\n    a[i-1][1] = a[i][1];\r\n  }\r\n  *\/\r\n  \/\/ Put a new value at the end of the array\r\n  ax[num-1] = mouseX;\r\n  ay[num-1] = mouseY;\r\n  dx[num-1] = (ax[num-1] - ax[num-2]);\r\n  dy[num-1] = (ay[num-1] - ay[num-2]);\r\n  \/\/ a[num-1][0] = mouseX; \/\/ LIVING LINE 2D ARRAY\r\n  \/\/ a[num-1][1] = mouseY; \/\/ LIVING LINE 2D ARRAY\r\n  \/\/ a[num-1].x = mouseX; \/\/ LIVING LINE PVECTORS\r\n  \/\/ a[num-1].y = mouseY; \/\/ LIVING LINE PVECTORS\r\n  \/\/ Constrain all points to the screen\r\n  \/\/ax[num-1] = constrain(ax[num-1], 0, width);\r\n  \/\/ay[num-1] = constrain(ay[num-1], 0, height);\r\n  \r\n  \/\/ Draw a line connecting the points\r\n  for(int i=1; i&lt;num; i++) {    \r\n    line(ax[i-1], ay[i-1], ax[i], ay[i]);\r\n    \/\/ line(a[i-1].x, a[i-1].y, a[i].x, a[i].y); \/\/ LIVING LINE PVECTORS\r\n    \/\/ line(a[i-1][0],a[i-1][1],a[i][0],a[i][1]); \/\/ LIVING LINE 2D ARRAY\r\n  }\r\n}\r\n<\/pre>\n<p>D.<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-753\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-12-at-11.21.30-PM.png\" alt=\"\" width=\"328\" height=\"332\" \/><\/p>\n<p>E. <img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-754\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-12-at-11.45.01-PM-484x480.png\" alt=\"\" width=\"484\" height=\"480\" srcset=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-12-at-11.45.01-PM-484x480.png 484w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-12-at-11.45.01-PM-150x150.png 150w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-12-at-11.45.01-PM-768x762.png 768w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-12-at-11.45.01-PM.png 794w\" sizes=\"(max-width: 484px) 85vw, 484px\" \/><\/p>\n<p><code><\/code><\/p>\n<pre>float r = 40;\r\n\r\n\r\nvoid setup(){\r\n  size(400, 400);\r\n  background(100);\r\n}\r\n\r\nvoid draw(){\r\n  background(255);\r\n  translate(width\/2, height\/2);\r\n  \/\/trig_circle();\r\n  trig_spiral();\r\n  noLoop();\r\n}\r\n\r\nvoid trig_spiral(){\r\n  beginShape();\r\n  for(float i = 1; i &lt; 40; i+=0.05){\r\n    vertex(cos(i)*(r), sin(i)*(r));\r\n    r+=0.2;\r\n  }\r\n  endShape();\r\n}\r\n\r\nvoid trig_circle(){\r\n  beginShape();\r\n  for(float i = 1; i &lt; 360; i+=1){\r\n    vertex(cos(i)*(r), sin(i)*(r));\r\n  }\r\n  endShape();\r\n}\r\n<\/pre>\n<p>F. I got tired of math so I made a shitty offset curve &#8211; not parallel, just the normal vector of the slope<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-767\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_8995-507x480.gif\" alt=\"\" width=\"507\" height=\"480\" \/><\/p>\n<p><code><\/code><\/p>\n<pre>int num = 100;\r\nint range = 6;\r\n\r\nfloat[] ax = new float[num];\r\nfloat[] ay = new float[num]; \r\nfloat[] dx = new float[num];\r\nfloat[] dy = new float[num];\r\nfloat[] ox = new float[num];\r\nfloat[] oy = new float[num]; \r\nvoid setup() \r\n{\r\n  size(640, 360);\r\n  for(int i = 0; i &lt; num; i++) {\r\n    ax[i] = width\/2;\r\n    ay[i] = height\/2;\r\n    dx[i] = 0;\r\n    dy[i] = 0;\r\n    ox[i] = width\/2;\r\n    oy[i] = height\/2;\r\n  }\r\n  frameRate(30);\r\n}\r\n\r\nvoid draw() \r\n{\r\n  background(255);\r\n  \r\n  \/\/ Shift all elements 1 place to the left\r\n  for(int i = 1; i &lt; num; i++) {\r\n    dx[i-1] = dx[i];\r\n    dy[i-1] = dy[i];\r\n    ax[i-1] = ax[i];\r\n    ay[i-1] = ay[i];\r\n    ox[i-1] = ox[i];\r\n    oy[i-1] = oy[i];\r\n  }\r\n  \/\/ Put a new value at the end of the array\r\n  ax[num-1] = mouseX;\r\n  ay[num-1] = mouseY;\r\n  dx[num-1] = (ax[num-1] - ax[num-2]);\r\n  dy[num-1] = (ay[num-1] - ay[num-2]);\r\n  \/\/dksjfldkjslkdfjlsdj i hate this\r\n  \/\/ox[num-1] = 25\/(1+pow((ay[num-1] + dx[num-1])\/(ax[num-1] - dy[num-1]),2));\r\n  \/\/oy[num-1] = sqrt(25 - pow(ox[num-1],2));\r\n  ox[num-1] = ax[num-1] - dy[num-1];\r\n  oy[num-1] = ay[num-1] + dx[num-1];\r\n  \r\n  \/\/ Draw a line connecting the points\r\n  for(int i=1; i&lt;num; i++) {    \r\n    \/\/float val = float(i)\/num * 204.0 + 51;\r\n    \/\/stroke(val);\r\n    line(ax[i-1], ay[i-1], ax[i], ay[i]);\r\n    \/\/float dx = 25\/(1+pow(ay[num-1] + dx[num-1]\/ax[num-1] - dy[num-1]),2));\r\n    \/\/float dy = sqrt(25 - pow(dx,2));\r\n    line(ox[i-1], oy[i-1], ox[i], oy[i]);\r\n  }\r\n}\r\n<\/pre>\n<p>G.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-757\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_0883-640x372.jpg\" alt=\"\" width=\"640\" height=\"372\" srcset=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_0883-640x372.jpg 640w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_0883-1024x595.jpg 1024w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_0883-768x447.jpg 768w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_0883-1536x893.jpg 1536w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_0883-1200x698.jpg 1200w, https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/IMG_0883.jpg 1897w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><a href=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/foo_209889.svg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-785\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/foo_209889.svg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>i was watching a lot of gordon ramsey<\/p>\n<p>H.<\/p>\n<p><a href=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/foo_35534.svg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-786\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/foo_35534.svg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>&#8220;bears beets battlestar galactica&#8221;<br \/>\n<code><\/code><\/p>\n<pre>import processing.svg.*;\r\nint num = 1000;\r\nint range = 6;\r\n\r\nboolean record = false;\r\n\r\nfloat[] ax = new float[num];\r\nfloat[] ay = new float[num]; \r\nfloat[] dx = new float[num];\r\nfloat[] dy = new float[num];\r\nfloat[] ox = new float[num];\r\nfloat[] oy = new float[num]; \r\n\r\nint num_lines = 5;\r\nvoid setup() \r\n{\r\n  size(640, 360);\r\n  for(int i = 0; i &lt; num; i++) {\r\n    ax[i] = width\/2;\r\n    ay[i] = height\/2;\r\n    dx[i] = 0;\r\n    dy[i] = 0;\r\n    ox[i] = width\/2;\r\n    oy[i] = height\/2;\r\n  }\r\n  frameRate(30);\r\n}\r\n\r\nvoid draw() \r\n{\r\n  background(255);\r\n\r\n  \/\/ Shift all elements 1 place to the left\r\n  for(int i = 1; i &lt; num; i++) {\r\n    dx[i-1] = dx[i];\r\n    dy[i-1] = dy[i];\r\n    ax[i-1] = ax[i];\r\n    ay[i-1] = ay[i];\r\n    ox[i-1] = ox[i];\r\n    oy[i-1] = oy[i];\r\n  }\r\n  \/\/ Put a new value at the end of the array\r\n  ax[num-1] = mouseX;\r\n  ay[num-1] = mouseY;\r\n  dx[num-1] = (ax[num-1] - ax[num-2]);\r\n  dy[num-1] = (ay[num-1] - ay[num-2]);\r\n  \r\n  \/\/ Draw a line connecting the points\r\n  if(record){\r\n    int now = millis();\r\n    randomSeed(now);\r\n    beginRecord(SVG, \"foo_\" + now + \".svg\");\r\n  }\r\n  for(int i=1; i&lt;num; i++) {    \r\n    float mult = map(dx[i] + dy[i], 0, width+height, 1, 60);\r\n    for(int j = 0; j &lt; num_lines; j++){\r\n      line(ax[i-1] - j*mult, ay[i-1], ax[i] - j*mult, ay[i]);\r\n    }\r\n\r\n  }\r\n  if(record){\r\n    endRecord();\r\n    record = false;\r\n  }\r\n}\r\n\r\nvoid keyPressed() {\r\n  record = true;\r\n}\r\n<\/pre>\n<p>BUT, then I added some perpendicular lines instead:<\/p>\n<p><a href=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/foo_51372.svg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-791\" src=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-content\/uploads\/2021\/09\/foo_51372.svg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p><code><\/code><\/p>\n<pre>import processing.svg.*;\r\nint num = 1500;\r\nint range = 6;\r\n\r\nboolean record = false;\r\n\r\nfloat[] ax = new float[num];\r\nfloat[] ay = new float[num]; \r\nfloat[] dx = new float[num];\r\nfloat[] dy = new float[num];\r\nfloat[] ox = new float[num];\r\nfloat[] oy = new float[num]; \r\n\r\nint num_lines = 15;\r\nvoid setup() \r\n{\r\n  size(640, 640);\r\n  for(int i = 0; i &lt; num; i++) {\r\n    ax[i] = width\/2;\r\n    ay[i] = height\/2;\r\n    dx[i] = 0;\r\n    dy[i] = 0;\r\n    ox[i] = width\/2;\r\n    oy[i] = height\/2;\r\n  }\r\n  frameRate(30);\r\n}\r\n\r\nvoid draw() \r\n{\r\n  background(255);\r\n\r\n  \/\/ Shift all elements 1 place to the left\r\n  for(int i = 1; i &lt; num; i++) {\r\n    dx[i-1] = dx[i];\r\n    dy[i-1] = dy[i];\r\n    ax[i-1] = ax[i];\r\n    ay[i-1] = ay[i];\r\n    ox[i-1] = ox[i];\r\n    oy[i-1] = oy[i];\r\n  }\r\n  \/\/ Put a new value at the end of the array\r\n  ax[num-1] = mouseX;\r\n  ay[num-1] = mouseY;\r\n  dx[num-1] = (ax[num-1] - ax[num-2]);\r\n  dy[num-1] = (ay[num-1] - ay[num-2]);\r\n\r\n  \/\/ Draw a line connecting the points\r\n  if(record){\r\n    int now = millis();\r\n    randomSeed(now);\r\n    beginRecord(SVG, \"foo_\" + now + \".svg\");\r\n  }\r\n  for(int i=1; i&lt;num; i++) {    \r\n    float d_lines = int(map(dx[i] + dy[i],-200, 200, 0,num_lines));\r\n    for(float j = 0; j &lt; d_lines; j++){\r\n      float multx1 = lerp(ax[i], ax[i-1], j\/d_lines);\r\n      float multx2 = lerp(ax[i]-dy[i], ax[i-1] - dy[i-1], j\/d_lines);\r\n      float multy1 = lerp(ay[i], ay[i-1], j\/d_lines);\r\n      float multy2 = lerp(ay[i] + dx[i], ay[i-1] + dx[i-1], j\/d_lines);\r\n      line(multx1, multy1, multx2, multy2);\r\n    }\r\n  }\r\n  if(record){\r\n    endRecord();\r\n    record = false;\r\n  }\r\n}\r\n\r\nvoid keyPressed() {\r\n  record = true;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A. \/* Basically I used the example code from the lerp() page on Processing * but changed it so it would produce dotted lines for any random * pair of vectors * *\/ float dashed_length = 20; void setup(){ size(400, 400); background(100); } void draw(){ background(255); int x1 = int(random(0,width)); int x2 = int(random(0,width)); int &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/grape\/09\/14\/grape-lineexercises\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;grape &#8211; LineExercises&#8221;<\/span><\/a><\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/posts\/735"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/comments?post=735"}],"version-history":[{"count":10,"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/posts\/735\/revisions"}],"predecessor-version":[{"id":793,"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/posts\/735\/revisions\/793"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/media?parent=735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/categories?post=735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/60-428\/f2021\/wp-json\/wp\/v2\/tags?post=735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}