We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › basic trigonometry
Page Index Toggle Pages: 1
basic trigonometry (Read 892 times)
basic trigonometry
Sep 27th, 2007, 10:43am
 
Hi,

I'm actually trying to figure out a basic trigonometry problem. But as my math courses go back some years ago, I have some difficulties.

Basicly, i've a sketch that trace two circle according to a string length. And i wanted to know the exact coordinates of the PI/4 point on the little circle according to the radius ratio.

Well not easy to explain so this example must be clearer.

Quote:


/*
This program trace a circle according to the string length
constrain to be max = sketch width.
Then it trace another circle with a radius with a 1/3 ratio from another one.

What i'm trying to achieve is that the point is at the cross betwen
the little circle and the greyline (PI/4 or 3*PI/4 one ?).
*/


String s = "Test"; //try different string length
float slength = s.length();
float radius;

void setup() {
 size(200, 200);
 background(255);
 smooth();

 //trace the grid system
 line(width/2, 0, width/2, height);
 line(0, height/2, width, height/2);
 stroke(0,80);
 line(0, 0, width/2, height/2);
 
 //trace the circles
 radius = constrain((slength*20), 0, width);
 println(radius);
 println(radius/3);
 noStroke();
 fill(radius, 0, 0, radius);
 ellipse(width/2, height/2, radius, radius);
 fill(radius, 0, 0, radius/2);
 ellipse(width/2, height/2, radius/3, radius/3);
 
 //trace the point
 stroke(255, 0, 0);
 strokeWeight(5);
 
 /*
 What should be the coordinates of this point to be exactly
 at the intersection between the grey line (45°) and the dark
 little circle ?
 */
 point(((width/2)-(cos((3*PI)/4))-radius/3), (height/2)-(sin((3*PI)/4)));
 }
 
void draw() {
 //nothing there atm.
 }




Cheers,
thx by advance
Re: basic trigonometry
Reply #1 - Sep 27th, 2007, 12:11pm
 
Code:
point((width/2) + radius/6*cos(5*PI/4.0), (height/2) + radius/6*sin(5*PI/4.0)); 



Re: basic trigonometry
Reply #2 - Sep 27th, 2007, 1:38pm
 
Thx a lot JohnG !!!

You really answer so fast on this board, that's really helpfull.

Having this working i have another question. I want to bend a string along the little circle, the first letter of the string coordinates start at the red point and should be align aside the light grey line.

Here's the example:

Quote:


String s = "Test"; //try different string length
float slength = s.length();
float radius;
PFont font;

void setup() {
 size(200, 200);
 background(255);
 smooth();

 //trace the grid system
 line(width/2, 0, width/2, height);
 line(0, height/2, width, height/2);
 stroke(0,80);
 line(0, 0, width/2, height/2);
 
 //trace the circles
 radius = constrain((slength*20), 0, width);
 println(radius);
 println(radius/3);
 noStroke();
 fill(radius, 0, 0, radius);
 ellipse(width/2, height/2, radius, radius);
 fill(radius, 0, 0, radius/2);
 ellipse(width/2, height/2, radius/3, radius/3);
 
 //trace the point
 stroke(255, 0, 0);
 strokeWeight(5);
 //point((width/2)+radius/6*cos(3*PI/4.0), (height/2)+radius/6*sin(-3*PI/4.0));
 point((width/2) + radius/6*cos(5*PI/4.0), (height/2) + radius/6*sin(5*PI/4.0));
 font = createFont("Bookman Old Style", 48, true);
 textFont(font, 24);
 fill(0);
 }
 
void draw() {
 translate((width/2)+radius/6*cos(5*PI/4.0), (height/2)+radius/6*sin(5*PI/4.0));
 
 for (int i=0; i < s.length(); i++) {
   //rotate(radius/6); <<<<<<<< What's the rotation formulas there ??
   text(s.charAt(i), 0, 0);  
   translate(textWidth(s.charAt(i)), 0);  
   }

 }




Thx.
Re: basic trigonometry
Reply #3 - Sep 27th, 2007, 4:17pm
 
I've changed a few things in your code. On is that it'd actually be pretty difficult working out translations/rotations if you treated the red point as 0,0, but if you treat the middle of the circle as 0,0 everything becomes an awful lot easier.

Code:
void draw() {
// translate((width/2)+radius/6*cos(5*PI /4.0), (height/2)+radius/6*sin(5*PI/4.0));
translate(width/2,height/2); //easiest to treat the middle of the circle as 0,0
rotate(-PI/4.0); // initial rotation.

float circumference=2*PI*(radius/3);
// float cumulativeRotation=0;
for (int i=0; i < s.length(); i++)
{
float arcLength=textWidth(s.charAt(i)); // how wide is this letter
float rotation=TWO_PI*arcLength/circumference; //what angle makes that arcLength of a circle with the circumference it has.

text(s.charAt(i), -arcLength/2.0, 12-radius/3); //-arcLenght/2 so text is centred about it's horizontal
// 12 for textHeight to have it level with the circle, -radius/3 for the radius of the circle.
rotate(rotation); //only need tyo rotate *after* we've drawn the current letter, or first will be offset.
}
}
Re: basic trigonometry
Reply #4 - Sep 28th, 2007, 3:52pm
 
Sorry i didn't find some time to post an answer before, sorry for that as u gave me feedback so quickly. I've just find some minutes to try your code.

It works very well (even if changing the word result in strange behavior (try it with "HI" or "Everything") but i'll try to figure out.

The most important point is that it help me to understand some fundamental things and concept and helped me a lot to figure things on my own.

As we said in french, "chapeau bas".
(sorry i have no clue how to translate it).
Page Index Toggle Pages: 1