why is my rotation 360 but draws like 0?
in
Programming Questions
•
1 year ago
I expect more of a circle with the 3 elements that get drawn...
- PFont f;
- float textHeight;
- PVector pos;
- PVector addV;
- void setup() {
- size(800, 800);
- f = createFont("Sans-serif", 40);
- textFont(f);
- // The text must be centered!
- textAlign(CENTER, CENTER);
- smooth();
- textHeight = textAscent()+textDescent();
- pos = new PVector(width/5, height-50, 0);
- noLoop();
- }
- void draw() {
- background(255);
- pushMatrix();
- // Start in the center and draw the circle
- translate(pos.x, pos.y);
- rotate(pos.z);
- // show start as red dot
- fill(255, 0, 0);
- ellipse(0, 0, 15, 15);
- fill(0);
- addV = drawCurved("W01 | 9+", 1, HALF_PI);
- pos.x += addV.x;
- pos.y += addV.y;
- pos.z += addV.z;
- popMatrix();
- pushMatrix();
- // green dot to show end of previous one
- fill(0, 255, 0);
- ellipse(pos.x, pos.y, 7, 7);
- translate(pos.x, pos.y);
- rotate(pos.z);
- addV = drawCurved("W32 | 27+", 1, HALF_PI);
- pos.x += addV.x;
- pos.y += addV.y;
- pos.z += addV.z;
- popMatrix();
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(pos.z);
- addV = drawCurved("A BIGGER TEXT THEN THE PREVIOUS ONE", 1, HALF_PI);
- popMatrix();
- /*
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(pos.z);
- pos = drawCurved("Bend to left instand of right", 1, HALF_PI);
- popMatrix();
- */
- noLoop();
- }
- PVector drawCurved(String message, int direction, float stop) {
- PVector returnVector = new PVector();
- float startX = screenX(0, 0);
- float startY = screenY(0, 0);
- pushMatrix();
- if (direction == 1) { // clockwise (bend to right)
- // get the length of the message
- float l = textWidth(message);
- // the radius
- float r = l / (TWO_PI*norm(stop, 0, TWO_PI));
- translate(r, 0);
- // We must keep track of our position along the curve
- float arclength = 0;
- pushStyle();
- noFill();
- stroke(0);
- strokeWeight(0.5);
- ellipse(0, 0, r*2, r*2);
- line(0, -r, 0, r);
- line(-r, 0, r, 0);
- popStyle();
- // For every box
- for (int i = 0; i < message.length(); i ++ ) {
- // The character and its width
- char currentChar = message.charAt(i);
- // Instead of a constant width, we check the width of each character.
- float w = textWidth(currentChar);
- // Each box is centered so we move half the width
- arclength += w/2;
- // Angle in radians is the arclength divided by the radius
- // Starting on the left side of the circle by adding PI
- float theta = PI + arclength / r;
- pushMatrix();
- // Polar to Cartesian conversion allows us to find the point along the curve. See Chapter 13 for a review of this concept.
- translate(r*cos(theta), r*sin(theta));
- // Rotate the box (rotation is offset by 90 degrees)
- rotate(theta + HALF_PI);
- // Display the character
- fill(0);
- text(currentChar, 0, 0);
- strokeWeight(0.5);
- line(0, -r, 0, r);
- if (i==message.length()-1) {
- // get how much we did move
- float endX = screenX(textWidth(currentChar)/2, 0);
- float endY = screenY(textWidth(currentChar)/2, 0);
- returnVector.x = endX - startX;
- returnVector.y = endY - startY;
- // how much did we rotate in total (should be equal to stop if we add
- // the right side of the last drawn thing, else it's less depanding on
- // the width of the previous drawn element)
- arclength += w/2;
- theta = PI + arclength / r;
- float rotation = theta + HALF_PI;
- returnVector.z = rotation;
- println(degrees(rotation));
- }
- popMatrix();
- // Move halfway again
- arclength += w/2;
- }
- }
- /*
- else if( direction == -1) { // counter clockwise (bend to left)
- // ?
- }
- */
- popMatrix();
- return returnVector;
- }
1