Loading...
Logo
Processing Forum

text on a curve

in Programming Questions  •  1 year ago  
I thought that on the processing site there was a example of how to draw type on a curve but i can't find it.
Do i look over it?

If it's not there, a example of how to do it would be welcome.

Replies(7)

Re: text on a curve

1 year ago
even if someone finds it i don't know if it helps.

I have stuff like this:


the length varies and i want to bend it on a 90 degree arc. To the left aswell to the right.
I believe the arc function is useless for this. For bezier i don't know. Althought i never succeeded in drawning a perfect 90 degree arc with beziers in processing.

Please help, i need to make a silk screen tomorow.

Re: text on a curve

1 year ago
can someone move this topic to the programming questions section?


Here;s my first attempt, the image above is illsutrator to explain what i think the best method will be.
Every thin that get's drawn should be drawn from the center. Afcorse this is ugly with very big block but i only deal with small block and typography.
The first one in this case should have been also rotated half the width of the block but i leaved it like this so it's easier to explain why it should happen (look at it).

I have a formula in my code to calculate the 'circumfence' for a 90 degree arc. Is this correct?
And could someone help me with the next step of drawning that arc, i'm stuck.



Copy code
  1. float[] line;
  2. float spacing = 5;
  3. float rh = 8; // rect height
  4. void setup() {
  5.   size(800, 600);
  6.   frameRate(0.3);
  7.   smooth();
  8.   fill(0);
  9.   noStroke();
  10.  
  11.   rectMode(CENTER);
  12. }
  13. void draw() {
  14.   background(255);
  15.   line = new float[int(random(5, 20))];
  16.   for (int i = 0; i < line.length; i++) {
  17.     line[i] = random(4, 10);
  18.   }
  19.   pushMatrix();
  20.   translate(50, 50);
  21.   drawStraight(line);
  22.   popMatrix();
  23.   float l = getLength(line);
  24.   // radius
  25.   float r = (l/4) / TWO_PI; // devide by 4 cause i want a 90 degree arc (Is this math correct?)
  26.  
  27.  
  28. }
  29. void drawStraight(float[] line) {
  30.   pushMatrix();
  31.   for (int i = 0; i < line.length; i++) {
  32.     float rw = line[i]; // rect width
  33.     float x = rw/2;
  34.     float y = rh/2;
  35.     rect(x, y, rw, rh);
  36.     translate(rw+spacing, 0);
  37.   }
  38.   popMatrix();
  39. }
  40. float getLength(float[] line) {
  41.   float l = 0;
  42.   for (int i = 0; i < line.length; i++) {
  43.     float rw = line[i]; // rect width
  44.     l += rw+spacing;
  45.   }
  46.   return l;
  47. }

Re: text on a curve

1 year ago
Check out:

http://wiki.processing.org/w/Text_on_curves

&

Examples > Books > Processing Handbook > Units 31-42 > 37 Typography 3 > _03


Re: text on a curve

1 year ago
Or this ...?


Re: text on a curve

1 year ago
thanks, but i see the one from the processing book is really bad, it doesn't look even close to a arc no matter where you have the mouse.

The wiki is to complicated, i have no ideas how to use it for my needs.


I was thinking i have just to rotate every time the width of the object to draw and after that the width of the spacing. Rotating from the bottom of the dashed line. (And the first time rotate half the width of the first object).

I managed to get some arc in some way, only half of the arc is 180degrees off (also spacing of red blocks isn't correct).
See:

Could someone take a look at my code?

Also keep in mind i want to be able to draw things continues like this:



In case it helps, i made this but i don't want straight lines anymore:


Copy code
  1. float[] line;
  2. float spacing = 5;
  3. float rh = 8; // rect height
  4. void setup() {
  5.   size(800, 600);
  6.   frameRate(0.3);
  7.   smooth();
  8.   noStroke();

  9.   rectMode(CENTER);
  10. }
  11. void draw() {
  12.   background(255);
  13.   line = new float[int(random(5, 20))];
  14.   for (int i = 0; i < line.length; i++) {
  15.     line[i] = random(4, 10);
  16.   }
  17.   
  18.   
  19.   pushMatrix();
  20.   translate(50, height/2);
  21.   fill(0);
  22.   drawStraight(line);

  23.   // start where the other one ended
  24.   float l = getLength(line);
  25.   translate(l, 0);
  26.   fill(255, 0, 0);

  27.   // radius
  28.   float r = l / (TWO_PI/4); // devide by 4 cause i want a 90 degree arc (Is this math correct?)
  29.   println(r);
  30.   drawArc(line, r);
  31.   popMatrix();
  32. }

  33. // In case someone looks and is good at this stuff, being able to set how far it should go would be even more nice
  34. // like void drawArc(float[] line, float radius, float start, float stop) { 
  35. // (and being able to bend to the other side with negative values or something?)

  36. void drawArc(float[] line, float radius) { 


  37.   pushMatrix();
  38.   translate(0, -radius);
  39.   rotate(HALF_PI);
  40.   // rotate a litle bit for the first one to be correct
  41.   float w = line[0]; // rect width
  42.   float theta = atan2(-radius, w/2);
  43.     
  44.   rotate(theta);
  45.   
  46.   
  47.   for (int i = 0; i < line.length; i++) {
  48.     // width
  49.     w = line[i]; // rect width
  50.     theta = atan2(-radius, w);
  51.     
  52.     rotate(theta);
  53.     rect(w/2, -radius + (-rh/2), w, rh);

  54.     //spacing
  55.     theta = atan2(-radius, spacing);
  56.     rotate(theta);
  57.   }
  58.   popMatrix();
  59. }


  60. void drawStraight(float[] line) {
  61.   pushMatrix();
  62.   for (int i = 0; i < line.length; i++) {
  63.     float rw = line[i]; // rect width
  64.     float x = rw/2;
  65.     float y = rh/2;
  66.     rect(x, y, rw, rh);
  67.     translate(rw+spacing, 0);
  68.   }
  69.   popMatrix();
  70. }


  71. float getLength(float[] line) {
  72.   float l = 0;
  73.   for (int i = 0; i < line.length; i++) {
  74.     float rw = line[i]; // rect width
  75.     l += rw+spacing;
  76.   }
  77.   return l;
  78. }
edit: -----
David also thanks, i saw your message after i posted this one. That thing is nice, the only hard thing with it is that you don't know the stop position. Like you can't easily have the radius adjusted so the total form is 90 degrees.


Re: text on a curve

1 year ago
" The wiki is to complicated"
Well, the operation isn't simple by itself... It went a bit overboard with the parametric functions, but the base maths and geometrical operations (translate + rotate) are easily reusable.

Re: text on a curve

1 year ago
Is there on of the links that you recomendent over the other (keeping reuse in mind).
Atm i got this but i don't mind starting over, i go silk screen after the vacation i guess.
Copy code
  1. // Learning Processing
  2. // Daniel Shiffman
  3. // http://www.learningprocessing.com

  4. // Example 17-8: Characters along a curve 

  5. PFont f;


  6. float textHeight;

  7. PVector pos;


  8. void setup() {
  9.   size(800, 800);
  10.   f = createFont("Arial", 20);
  11.   textFont(f);
  12.   // The text must be centered!
  13.   textAlign(CENTER, CENTER);
  14.   smooth();

  15.   textHeight = textAscent()+textDescent();
  16. }

  17. void draw() {
  18.   background(255);

  19.   pushMatrix();
  20.   // Start in the center and draw the circle
  21.   translate(width/2, height/2);
  22.   noFill();
  23.   stroke(0);
  24.   //ellipse(0, 0, r*2, r*2);
  25.   // show start as red dot
  26.   fill(255, 0, 0);
  27.   ellipse(0, 0, 15, 15);

  28.   fill(0);  
  29.   pos = drawCurved("W01 | 9+", 1);
  30.   
  31.   popMatrix();
  32.   

  33.   // green dot to show end of previous one
  34.   fill(0, 255, 0);
  35.   ellipse(pos.x, pos.y, 7, 7);
  36.   
  37.   translate(pos.x, pos.y);
  38.   println(pos.z);
  39.   rotate(pos.z);

  40.   drawCurved("W32 | 27+", 1);
  41.   


  42.   
  43.   
  44.   noLoop();
  45. }


  46. PVector drawCurved(String message, int direction) {
  47.   
  48.   PVector returnVector = new PVector();

  49.   pushMatrix();

  50.   if (direction == 1) { // clockwise (bend to right)

  51.     // get the length of the message
  52.     float l = textWidth(message);
  53.     // the radius
  54.     float r = l / (TWO_PI/4); // devide by 4 cause i want a 90 degree arc (Is this math correct?)
  55.     // r += textHeight/2; // since we want to draw from the center of the curve

  56.     translate(r, 0);
  57.     // We must keep track of our position along the curve
  58.     float arclength = 0;

  59.     // For every box
  60.     for (int i = 0; i < message.length(); i ++ ) {

  61.       // The character and its width
  62.       char currentChar = message.charAt(i);
  63.       // Instead of a constant width, we check the width of each character.
  64.       float w = textWidth(currentChar); 
  65.       // Each box is centered so we move half the width
  66.       arclength += w/2;

  67.       // Angle in radians is the arclength divided by the radius
  68.       // Starting on the left side of the circle by adding PI
  69.       float theta = PI + arclength / r;

  70.       pushMatrix();

  71.       // Polar to Cartesian conversion allows us to find the point along the curve. See Chapter 13 for a review of this concept.
  72.       translate(r*cos(theta), r*sin(theta)); 
  73.       // Rotate the box (rotation is offset by 90 degrees)
  74.       rotate(theta + HALF_PI); 

  75.       // Display the character
  76.       fill(0);
  77.       text(currentChar, 0, 0);
  78.       
  79.       if(i==message.length()-1) {
  80.         returnVector.x = screenX(textWidth(currentChar)/2, 0);
  81.         returnVector.y = screenY(textWidth(currentChar)/2, 0);
  82.         returnVector.z = theta + HALF_PI; 
  83.         returnVector.z += HALF_PI;
  84.       }

  85.       popMatrix();

  86.       // Move halfway again
  87.       arclength += w/2;
  88.     }
  89.   }
  90.  /*
  91.   else if( direction == -1) { // counter clockwise (bend to left)
  92.     // ?
  93.   }
  94.   */
  95.   popMatrix();

  96.   return returnVector;
  97. }