The following two sketches are vine tests for the flora project. The final design will probably have to incorporate elements from both these applets.
This first sketch is a curve fitting example. It draws a path following the mouse movements, adding a point whenever the mouse moves far enough in a direction different enough from the previous point.
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 | ArrayList pts; PVector lastPt, beforeLastPt; int distThreshold = 50; float slopeThreshold = 0.5; void setup() { size(400, 400); smooth(); pts = new ArrayList(); lastPt = new PVector(); beforeLastPt = new PVector(); } void draw() { // clear the screen background(255); // draw a path through all key points beginShape(); for (int i=0; i < pts.size(); i++) { PVector currPt = (PVector)pts.get(i); // draw the key point noStroke(); fill(0); ellipse(currPt.x, currPt.y, 5, 5); // add the vertex to the path noFill(); stroke(255, 0, 0); curveVertex(currPt.x, currPt.y); } endShape(); } void mouseDragged() { // if the new point is far enough from the last key point... if (dist(mouseX, mouseY, lastPt.x, lastPt.y) > distThreshold) { // ...and there are less than 2 key points total // OR // ...the slope between the new point and the last key point is // different enough from the slope between the last 2 key points... if (pts.size() < 2 || abs(slope(mouseX, mouseY, lastPt.x, lastPt.y) - slope(lastPt.x, lastPt.y, beforeLastPt.x, beforeLastPt.y)) > slopeThreshold) { // ...add a new key point beforeLastPt = lastPt; lastPt = new PVector(mouseX, mouseY); pts.add(lastPt); } } } float slope(float x1, float y1, float x2, float y2) { if (x2-x1 == 0) return 0; // avoid division by 0! return (y2-y1)/(x2-x1); } void keyPressed() { if (key == ' ') { // reset all the key points pts.clear(); lastPt = new PVector(); beforeLastPt = new PVector(); } } |
This second sketch draws a vine using circles on a bezier path. These have varying diameters which results in a tapering effect. It also adds leaves at regular intervals on the curve.
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 | int ax1 = 385; int ay1 = 20; int cx1 = 10; int cy1 = 10; int ax2 = 15; int ay2 = 380; int cx2 = 390; int cy2 = 390; void setup() { size(400, 400); smooth(); } void draw() { background(255); ax1 = mouseX; ay1 = mouseY; noFill(); stroke(128); bezier(ax1, ay1, cx1, cy1, cx2, cy2, ax2, ay2); stroke(128, 0, 0); //line(ax1, ay1, cx1, cy1); //line(cx2, cy2, ax2, ay2); noStroke(); int steps = 300; int leafSteps = 6; boolean leafDir = true; float px = 0; float py = 0; for (int i = 0; i <= steps; i++) { float t = i / float(steps); float x = bezierPoint(ax1, cx1, cx2, ax2, t); float y = bezierPoint(ay1, cy1, cy2, ay2, t); float s = sin(i*PI/(steps*2))*10; fill(#743632); noStroke(); ellipse(x, y, s, s); if (i%leafSteps == 1) { float a = atan2(y - py, x - px); leaf(x, y, leafDir? a+PI*3/4 : a+PI*1/4, leafDir? s*3 : -s*3); leafDir = !leafDir; } px = x; py = y; } } void leaf(float x, float y, float r, float s) { stroke(#07903D); fill(#1AEA11); pushMatrix(); translate(x, y); rotate(r); beginShape(); curveVertex(0, 0); curveVertex(0, 0); curveVertex(s/2, -s/4); curveVertex(s, 0); curveVertex(s, 0); curveVertex(s/2, s/4); curveVertex(0, 0); curveVertex(0, 0); endShape(); line(s/4, 0, s*3/4, 0); popMatrix(); } |

0 Responses to “flora Vine Tests”