wrong direction problem
in
Programming Questions
•
1 year ago
I thought i was there but later i discovered that the x direction is not the direction i thought it was.
The green line is drawn like this:
line(0, 0, 100, 0);
I want it to be in the direction of the text (but still written like line(0, 0, 100, 0);.
Also the W01 (center of image) starts in the direction upwards. I want it to start in this direction -->
I can do that with
//rotate(HALF_PI); // default direction should be >>
after the first pushMatrix in drawCurved, i only don't know if that's a good fix (it meight make the green line problem harder).
I tried several things but i always end up in breaking something else.
If someone could help then that would be neat, keep in mind that the changes should be made in the drawCurved function, not in void draw.
- PFont f;
- float textHeight;
- PVector pos;
- PVector addV;
- String[] messages = {
- "W01 | 9+", "W32 | 27+", "A BIGGER TEXT", "Bend to left instand of right"
- };
- float[] stops = {
- PI, HALF_PI, HALF_PI, -HALF_PI
- };
- 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/2, height/2, 0);
- noLoop();
- }
- void draw() {
- background(255);
- for (int i = 0 ; i < messages.length; i++) {
- pushMatrix();
- // Start in the center and draw the circle
- translate(pos.x, pos.y);
- if (i == 0) {
- stroke(255, 0, 0);
- line(0, -height/2, 0, height/2);
- line(-width/2, 0, width/2, 0);
- }
- rotate(pos.z);
- // show start as red dot
- fill(255, 0, 0);
- ellipse(0, 0, 15, 15);
- fill(0);
- stroke(0);
- fill(random(255), random(255), random(255));
- addV = drawCurved(messages[i], stops[i]);
- pos.x += 50; //it goes the wrong way
- pos.x += addV.x;
- pos.y += addV.y;
- pos.z += addV.z;
- stroke(0, 255, 0);
- line(0,0, 100, 0);
- popMatrix();
- }
- noLoop();
- }
- PVector drawCurved(String message, float stop) {
- PVector returnVector = new PVector();
- float startX = screenX(0, 0);
- float startY = screenY(0, 0);
- pushMatrix();
- //rotate(HALF_PI); // default direction should be >>
- // 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;
- // for debug
- 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(w/2, 0);
- float endY = screenY(w/2, 0);
- returnVector.x = endX - startX;
- returnVector.y = endY - startY;
- /*
- arclength += w/2;
- theta = PI + arclength / r;
- */
- float rotation = theta - PI;
- returnVector.z = rotation;
- }
- popMatrix();
- // Move halfway again
- arclength += w/2;
- }
- popMatrix();
- return returnVector;
- }
1