have translate and rotate have no effect on the value it returns
in
Programming Questions
•
1 year ago
The function drawCurved returns a pvector, where x should be the distance moved inx seen from the startingpoint of where you begin drawing, y the distance moved in y, and z the rotation.
So no matter where you draw on your canvas, if you draw "this text" on a arc of 45 degrees then it should always give the same 3 values. At the moment if i translate or rotate those values change, i don't want this. (it's like calling textWidth("some text") and having it return -87 because you rotated 180 degrees).
The rotation is correct atm, it gives always the correct value.
The one who can tell me how to fix the problem with x and y would make me so happy!
So no matter where you draw on your canvas, if you draw "this text" on a arc of 45 degrees then it should always give the same 3 values. At the moment if i translate or rotate those values change, i don't want this. (it's like calling textWidth("some text") and having it return -87 because you rotated 180 degrees).
The rotation is correct atm, it gives always the correct value.
The one who can tell me how to fix the problem with x and y would make me so happy!
- PFont f;
- float textHeight;
- PVector pos;
- PVector addV;
- String[] messages = {
- "111---------", "222---------", "333======", "444<<<<<<<<<<"
- };
- float[] stops = {
- radians(40), radians(-140), radians(135), radians(360)
- };
- //debug
- float x1 = 0;
- float x2 = 0;
- float y1 = 0;
- float y2 = 0;
- 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 < 3; i++) {//messages.length
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(pos.z);
- // Start in the center and draw the cross
- if (i == 0) {
- stroke(255, 0, 0);
- line(0, -height/2, 0, height/2);
- line(-width/2, 0, width/2, 0);
- }
- // show start as red dot
- noStroke();
- fill(255, 0, 0);
- ellipse(0, 0, 15, 15);
- fill(0);
- stroke(0);
- rotate(map(mouseX, 0, width, 0, TWO_PI));
- addV = drawCurved(messages[i], stops[i]);
- // move to the end of the last drawn curve
- //println(addV.x);
- //translate(addV.x, addV.y);
- // rotate to match the last drawn character
- //rotate(addV.z);
- // show end as a green dot
- // fill(0, 255, 0);
- // ellipse(0, 0, 15, 15);
- drawXY();
- // create some spacing between each 'arc'
- // strokeWeight(2);
- // stroke(0, 255, 0);
- // line(0, 0, 100, 0);
- // translate the spacing
- //translate(100, 0);
- popMatrix();
- }
- // debug
- stroke(0);
- line(x1, y1, x2, y2);
- fill(0);
- text("x dist "+(x2 - x1), 20, 20);
- text("y dist "+(y2 - y1), 20, 50);
- // noLoop();
- }
- void mousePressed() {
- x1 = mouseX;
- y1 = mouseY;
- }
- void mouseDragged() {
- x2 = mouseX;
- y2 = mouseY;
- }
- void drawXY() {
- pushStyle();
- strokeWeight(2);
- noFill();
- stroke(255, 0, 0);
- line(0, 0, 100, 0);
- stroke(0, 255, 0);
- line(0, 0, 0, 100);
- popStyle();
- }
- PVector drawCurved(String message, float stop) {
- PVector returnVector = new PVector();
- float startX = screenX(0, 0);
- float startY = screenY(0, 0);
- // ellipse(0, 0, 20, 20);
- pushMatrix();
- // get the length of the message
- float l = textWidth(message);
- // the radius
- float r = l / (TWO_PI*norm(stop, 0, TWO_PI));
- // translate in y so default dir is >>
- translate(0, r);
- ellipse(0, 0, 5, 5); // show the origin of which we are rotating around
- // We must keep track of our position along the curve
- float arclength = 0;
- // for debug, show a circle on which we draw the letters on
- 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
- // -HALF_PI for correct start direction
- float theta = -HALF_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, it's rotating on the correct position so for fake italic you can do it here
- 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 + HALF_PI;
- println("rotation(degr): "+degrees(rotation));
- println("x: "+returnVector.x);
- println("y: "+returnVector.y);
- println();
- returnVector.z = rotation;
- }
- popMatrix();
- // Move halfway again
- arclength += w/2;
- }
- popMatrix();
- return returnVector;
- }
1