/**
*
* UPDATE: See the finished product by Rich Cannon here
*
*
* Type the name of a variable ('R', 'r', 'z' or 'h') to select it for editing.
* To change the value of the selected variable, click and drag the mouse,
* or use the following keys for fine tuning:
*
*
* '.' (increase by 0.01)
* '>' (increase by 0.001)
* '<' (decrease by 0.001)
* ',' (decrease by 0.01)
*
*
* The 'z' variable represents the full hub width, so that the distance from
* the center plane of the wheel to where the spokes attach on one side is z/2.
*/
float R = 36.875 / TWO_PI;
float r = 0.71875;
float z = 1.0;
float h = 1.75;
int N = 20;
char selected = 'h';
void setup() {
size(640, 480);
}
void mouseDragged() {
float stretch = min(width/20, height/20);
float dx, dy;
switch (selected) {
case 'R':
dx = (mouseX - width/2) / stretch;
dy = (mouseY - height/2) / stretch;
R = sqrt(dx*dx + dy*dy);
R = max(R, abs(h) + r);
break;
case 'r':
dx = (mouseX - width/2) / stretch - h;
dy = (mouseY - height/2) / stretch;
r = sqrt(dx*dx + dy*dy);
r = min(r, R - abs(h));
break;
case 'h':
dx = (mouseX - width/2) / stretch;
h = min(dx, R - r);
h = max(dx, r - R);
break;
case 'z':
dy = (mouseY - height/2) / stretch;
z = max(0, -dy);
break;
}
}
void keyTyped() {
switch (key) {
case 'R':
case 'r':
case 'z':
case 'h':
case ' ':
selected = key;
break;
case '=': case '.': changeSelection(0.01); break;
case '+': case '>': changeSelection(0.001); break;
case '_': case '<': changeSelection(-0.001); break;
case '-': case ',': changeSelection(-0.01); break;
}
}
void changeSelection(float inc) {
switch (selected) {
case 'R': R += inc; break;
case 'r': r += inc; break;
case 'z': z += inc; break;
case 'h': h += inc; break;
}
}
void highlightIf(char c) {
if (selected == c) {
fill(0, 150, 0);
} else {
fill(0, 0, 0);
}
}
void draw() {
background(200);
float stretch = min(width/20, height/20);
float x0 = h;
float y0 = 0;
textAlign(LEFT, TOP);
highlightIf('R');
text(String.format("rim radius (R): %5.3f in", R), 5, 5);
highlightIf('r');
text(String.format("hub radius (r): %5.3f in", r), 5, 25);
highlightIf('z');
text(String.format("hub width (z): %5.3f in", z), 5, 45);
highlightIf('h');
text(String.format("hub offset (h): %5.3f in", h), 5, 65);
translate(width/2, height/2);
stroke(0, 0, 0);
fill(255, 255, 255);
ellipse(0, 0, 2*R*stretch, 2*R*stretch);
ellipse(x0*stretch, y0*stretch, 2*r*stretch, 2*r*stretch);
for (int i = 0; i < N; i++) {
float x = x0 + r * cos(TWO_PI*i/N);
float y = y0 + r * sin(TWO_PI*i/N);
float X = R * cos(TWO_PI*i/N);
float Y = R * sin(TWO_PI*i/N);
float dx = X - x;
float dy = Y - y;
float dz = z/2;
float len = sqrt(dx*dx + dy*dy + dz*dz);
if (i % 2 == 0) {
stroke(0, 0, 255);
fill(0, 0, 255);
} else {
stroke(255, 0, 0);
fill(255, 0, 0);
}
line(x*stretch, y*stretch, X*stretch, Y*stretch);
textAlign(CENTER, CENTER);
text(len, X * 1.2 * stretch, Y * 1.2 * stretch);
}
}