path - (MATH ISSUE) Creating a SPIRAL out of points: How do I change "relative" position to absolute position -


recently had idea make pendulum out of points using processing, , little learning solved easily:

int contador = 0; int curvatura = 2; float pendulo;  void setup(){   size(300,300); }  void draw(){   background(100);   contador = (contador + 1) % 360; //"contador" goes 0 359   pendulo = sin(radians(contador))*curvatura;  //"pendulo" equals sin of contador, goes 1 -1 repeatedly, multiplied emphasize or reduce curvature of line.   tallo(width/2,height/3);   println(pendulo); }   void tallo (int x, int y){ //the funtion draw dotted line pushmatrix();  translate(x,y);  float _y = 0.0;  for(int = 0; < 25; i++){ //creates points sequence.    ellipse(0,0,5,5);    _y+=5;    rotate(radians(pendulo)); //rotate them on each iteration, makes spiral. }  popmatrix(); } 

so, in brief, did function changed every point position rotate fuction, , had draw ellipses in origin coordinates real thing changes position , creates pendulum ilussion.

[capture example, need 2 more points if gentile :)]

[capture example]

[capture example]

everything ok far. problem appeared when tried replace ellipses path made of vertices. problem obvious: path never (visually) made because vertices 0,0 move along 0 coordinates.

so, in order make path possible, need absolute values each vertex; , there's question: how them?

what know have remove transform functions, create variables x , y position , update them inside for, what? that's why cleared maths issue, which operation have add in x , y variables in order make path , curvature possible?

void tallo (int x, int y){ pushmatrix();  translate(x,y);  //now start changes. let's declare variables coordinates float _x = 0.0; float _y = 0.0;  beginshape(); for(int = 0; < 25; i++){ //creates dots.   vertex(_x,_y); //changing vertices , calling new variables, ok.   //rotate(radians(pendulo)); <--- here problem. how convert x , y coordinates?   //_x = _x + ????;   _y = _y + 5 /* + ???? */; } endshape();  popmatrix(); } 

we need have in mind pendulo's x , y values changes in each iteration of for, doesn't has add same quantity each time. addition must progressive. otherwise, see straight line rotating instead of curve accentuating curvature (if increase curvatura's value number greater 20, notice spiral)

so, rotating coordinates great solution it, it's kind of muddle think mathematical solution x , y coordinates spiral, secondary's knowledges aren't enough. know have create variable inside in order progression, operation should have?

i glad know, maths

you use simple trigonometry. know angle , hypotenuse, use cos relative x position, , sin y. position relative central point.

but before explain in detail , draw explanations, let me propose solution: pvectors

void setup() {     size(400,400);     framerate(60);     center = new pvector(width/2, height/3); //defined here because width , height set after size() }  void draw() {     background(255);     fill(0);     stroke(0);     angle = arc_magn*sin( (float) framecount/60 );     draw_pendulum( center );  }  pvector center; float angle = 0; float arc_magn = half_pi; float wire_length = 150; float rotation_angle = pi/20 /60 ; //we divide 60 first part rotation in 1 second  void draw_pendulum(pvector origin){      pvector temp_vect = pvector.fromangle( angle + half_pi);     temp_vect.setmag(wire_length);     pvector final_pos = new pvector(origin.x+temp_vect.x, origin.y+temp_vect.y );     ellipse( final_pos.x, final_pos.y, 40, 40);     line(origin.x, origin.y, final_pos.x, final_pos.y);  } 

you use pvector class static method fromangle( float angle ) returns unity vector of given angle, use .setmag() define it's length. pvector methods take care of trigonometry you.

if still want know math behind it, can make example.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -