// trace around a square Box box; P2D lineEnd; //P2D pointA; P2D pointB; P2D pointC; // angles float A; float B; float C; // sides float a; float b; float c; float angle; int then; int now; float elapsed; boolean paused; PFont font; void setup() { size(200,200); noSmooth(); stroke(255); strokeWeight(1); fill(255,120,0); font = loadFont("04b-08-8.vlw"); textFont(font); box = new Box(); pointB = new P2D(); pointC = new P2D(); C = radians(90); angle = 0.0f; then = millis(); // used for pausing paused = false; // if you press UP or DOWN it resumes a "demo" mode } void draw() { background(255,120,0); stroke(255,80); fill(255,255,255,40); rect(box.x, box.y, box.h, box.w); fill(255); text("Angle: " + angle, 0, 8); text("Distance: " + (int)c, 0, 16); text("Use UP/DOWN to move if you want.", 0, height - 1); if (paused == true) { now = millis(); elapsed = ((now - then) / 1000.0f); if (elapsed >= 2) { paused = false; } } //angle = 140.0f; if (! paused) { angle++; } // increment this //A = radians(angle++); if (angle >= 0.0f && angle < 45.0f) { A = radians(angle); } else if (angle >= 45.0f && angle < 90.0f) { A = radians(90.0f - angle); } else if (angle >= 90.0f && angle < 135.0f) { A = radians(135.0f - angle); } else if (angle >= 135.0f && angle < 180.0f) { A = radians(180.0f - angle); } else if (angle >= 180.0f && angle < 225.0f) { A = radians(225.0f - angle); } else if (angle >= 225.0f && angle < 270.0f) { A = radians(270.0f - angle); } else if (angle >= 270.0f && angle < 315.0f) { A = radians(315.0f - angle); } else if (angle >= 315.0f && angle < 360.0f) { A = radians(360.0f - angle); } // solve angle B // all triangles are 180ยบ and we know C is 90 B = radians(180 - C - A); // b is known b = box.h / 2; // TOA tan(A) = opposite (a) / adjacent (b) a = tan(A) * b; // SOH sin(A) = opposite (a) / hypotenuse (c) //c = a / sin(radians(angle)); c = sqrt(abs(a*a) + abs(b*b)); // 1st octet if (angle < 45.0f) { pointC.x = box.center.x; pointC.y = box.center.y - b; pointB.x = pointC.x + a; pointB.y = pointC.y; } // 2nd octet if (angle >= 45.0f && angle < 90.0f) { pointC.x = box.center.x + b; pointC.y = box.center.y; pointB.x = box.center.x + b; pointB.y = box.center.y - a; } // 3rd octet if (angle >= 90.0f && angle < 135.0f) { pointC.x = box.center.x + b; pointC.y = box.center.y; pointB.x = box.center.x + b; pointB.y = box.center.y + (b - a); } // 4th octet if (angle >= 135.0f && angle < 180.0f) { pointC.x = box.center.x; pointC.y = box.center.y + b; pointB.x = box.center.x + a; pointB.y = box.center.y + b; } // 5th octet if (angle >= 180.0f && angle < 225.0f) { pointC.x = box.center.x; pointC.y = box.center.y + b; pointB.x = box.center.x - (b - a); pointB.y = box.center.y + b; } // 6th octet if (angle >= 225.0f && angle < 270.0f) { pointC.x = box.center.x - b; pointC.y = box.center.y; pointB.x = box.center.x - b; pointB.y = box.center.y + a; } // 7th octet if (angle >= 270.0f && angle < 315.0f) { pointC.x = box.center.x - b; pointC.y = box.center.y; pointB.x = box.center.x - b; pointB.y = box.center.y - (b-a); } // 8th octet if (angle >= 315.0f && angle <= 360.0f) { pointC.x = box.center.x; pointC.y = box.center.y - b; pointB.x = pointC.x - a; pointB.y = pointC.y; } // if value is past limit, flip value to fake continuity if (angle > 360.0f) { angle = 0.0f; } // if value is pas limit, flip value to fake continuity if (angle < 0.0f) { angle = 360.0f; } stroke(180,80,20); // adjacent line(box.center.x, box.center.y, pointC.x, pointC.y); stroke(255); // opposite fill(255,255,255,120); rect(pointB.x-8, pointB.y-8, 16, 16); line(pointC.x, pointC.y, pointB.x, pointB.y); stroke(180,80,20); // hypotenuse line(box.center.x, box.center.y, pointB.x, pointB.y); } void keyPressed() { if (keyCode == UP) { //debug(); paused = true; then = millis(); angle++; } if (keyCode == DOWN) { //debug(); paused = true; then = millis(); angle--; } } void debug() { println("angle:" + angle + " RAD:" + A + " a:" + a + " b:" + b); } /* int B(int C, int A) { return 180 - C - A; } float a(int A, int b) { return tan(A) * b; }*/ // quick and dirty class Box { public float x; public float y; public float h; public float w; public P2D center; public Box() { x = 25; y = 25; h = 150; w = 150; center = new P2D(x + (w/2), y + (h/2)); } } class P2D { public float x; public float y; public P2D(float x, float y) { this.x=x; this.y=y; } public P2D() { } }