|
Author |
Topic: Adjusting drawing tool (Read 906 times) |
|
pitaru
|
Adjusting drawing tool
« on: Nov 6th, 2002, 6:29pm » |
|
/* This program determines how to best draw a trailing line, according to the user's mouse movement. Slow movements are drawn using the 'vertex' command, which allows precision. In return, the 'curveVertex' command is used for fast movement - which results in smooth curves. (See next posting for Ben Fry's optimized version) */ int num = 255; float dx = 0; float dy = 0; float oldmx = 0; float oldmy = 0; float delta = 0; float[] mx = new float[num]; float[] my = new float[num]; boolean[] md = new boolean[num]; void setup() { size(500, 500); background(255); strokeWidth(0.5); } void loop() { // determine mouse speed by calculating vector distance. dx = mouseX - oldmx; dy = mouseY - oldmy; delta = sqrt(sq(dx) + sq(dy)); oldmx = mouseX; oldmy = mouseY; // re-adjust array elements for(int i=1; i<num; i++) { mx[i-1] = mx[i]; my[i-1] = my[i]; md[i-1] = md[i]; } mx[num-1] = mouseX; my[num-1] = mouseY; // set a Flag in an array element //specifying whether to use a vertex (true) or curveVertex (false) if (delta < 3){ md[num-1] = true; } else { md[num-1] = false; } // draw the shape, use the Flag (md[]) to determine method for each sub-element in the shape. beginShape(LINE_STRIP); for(int i=0; i<num; i++) { if (md[i]){ stroke(255-i); vertex(mx[i], my[i]); } else { stroke(255-i); curveVertex(mx[i], my[i]); } } endShape(); }
|
|
|
|
fry
|
Re: Adjusting drawing tool
« Reply #1 on: Nov 6th, 2002, 6:46pm » |
|
/* that's a neat example, and a clever use of curveVertex.. a couple of (minor?) changes to tweak this up a bit.. 1. by keeping track of an index for what is the 'first' point in the array, it's possible to keep adding new points without having to move them in the array.. moving them on each step takes time. 2. also changed variable names a bit to make them clearer as to their purpose. 3. similarly, variables that are only used in a small part of a method (i.e. dx, dy) are defined inside the method rather than globally for the program.. this helps clear up the program a bit, making it more readable. there's no cost for doing this in java, the compiler will optimize it out. */ int pointCount = 255; float[] xpoints = new float[pointCount]; float[] ypoints = new float[pointCount]; boolean[] isCurve = new boolean[pointCount]; int first; int last; void setup() { size(500, 500); background(255); strokeWidth(0.5); first = 0; last = pointCount - 1; } void loop() { float dx = mouseX - pmouseX; float dy = mouseY - pmouseY; float delta = sqrt(sq(dx) + sq(dy)); xpoints[last] = mouseX; ypoints[last] = mouseY; isCurve[last] = (delta < 3); beginShape(LINE_STRIP); for(int j = 0; j < pointCount; j++) { int i = (first + j) % pointCount; stroke(pointCount - j); if (isCurve[i]) { vertex(xpoints[i], ypoints[i]); } else { curveVertex(xpoints[i], ypoints[i]); } } endShape(); last++; if (last == pointCount) last = 0; first++; if (first == pointCount) first = 0; }
|
|
|
|
|