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 › Perpendiculars to a chord
Page Index Toggle Pages: 1
Perpendiculars to a chord (Read 2730 times)
Perpendiculars to a chord
Jun 3rd, 2010, 10:32am
 
Hi,
I posted this accidentally in programs - My apologies.

I don't know how best to present the code that I have already. I am creating math chords inside a circle - It's for a simple cound reactive experiment. A chord is a line that connects two points of a circle and always stays within that circle (I think at least):

http://www.mathopenref.com/chord.html

I did this simply by drawing lines between points that move around the circle.

The code for the class that contains the point (Pvector) that moves around the circle is below.

But first I wanted to put my question forward which is that I want to attempt to make perpendiculars to the chord as is demonstrated here:

http://demonstrations.wolfram.com/PerpendicularsToAChord/

The code is there and the equation, and a demo of the movements it creates (like pistons) but unfortunately this is way way way over my head or at least I think it is. I wondered if anyone has any clue or advice as to how I would tackle this and turn it into processing code.

thank you so much.

the current class code for a Pvector object moves around the circle circumference is here: (in the main code tab I join a number of these Pvectors up using beginShape and endShape and lines that use the x and y values of the pVector objects).

Code:

class Tortoise{
 float radius;
 float x; float y;
 float scalar;
 float theta;
 float cenX; float cenY;
 PVector v;
 
 Tortoise(float irad, float ix, float iy){
   radius = irad; x = ix; y = iy;
   scalar = 375;
   theta = 0.0;
   cenX = width/2; cenY = height/2;
   v = new PVector(x,y);
 }
 
 float[] components(float scalar, float bearing){
   float[] comp = new float[2];
   comp[0] = scalar * sin(bearing);
   comp[1] = scalar * cos(bearing) * -1;
   return comp;
 }
 
 void change_bearing(float dTheta){
   float raw = theta + dTheta;
   theta = raw;
 }
 
 void turn(int KC, float turnRate){
   float[] nCoords = new float[2];
   if(KC == LEFT){change_bearing(-turnRate);}
   else if(KC == RIGHT){change_bearing(turnRate);}
   nCoords = components(scalar,theta);
   x = cenX + nCoords[0];
   y = cenY + nCoords[1];
   v.set(new float[] {x,y});
 }
 
 float getX(){return x;}
 float getY(){return y;}
 float get_pvector_theta(){return v.heading2D();}
 
 void paint(){
   stroke(255,0,0);
  // ellipse(x,y,radius,radius);
 }
}

Re: Perpendiculars to a chord
Reply #1 - Jun 3rd, 2010, 12:42pm
 
I think you didn't accidently posted it there-- PhiLho moved it to Programs because there it belongs.
Re: Perpendiculars to a chord
Reply #2 - Jun 3rd, 2010, 1:20pm
 
he posted it two times, i deleted the other post.
Re: Perpendiculars to a chord
Reply #3 - Jun 3rd, 2010, 1:33pm
 
This Topic was moved here from Syntax Questions by PhiLho .
Moved again here, as I don't see a syntax question above. Unless I miss something...
Re: Perpendiculars to a chord
Reply #4 - Jun 3rd, 2010, 2:09pm
 
If I've understood the website correcly it looks like the source code you link to is for Mathematica...  I'm not familiar with that but it doesn't look like there's much useful code there as it's using Mathematica specific methods.

Looking at my handy Useful Mathematical & Physical Formulae book (stupid Flash based site so I can't link to it directly, but you can browse it and some other interesting titles online*) I see that "A line perpendicular to another of gradient n will have gradient = -1/n".  I bet there are plenty of people on this forum who'll laugh that I had to look that up, but my memory is used to store origami folding sequences, not mathematical formulae Smiley

Anyway - if you can get the angle of your chord (try atan2 it should be easy enough to display another line perpendicular to it...  Looks like an interesting project to turn into a Processing sketch!


* Sorry - I know it's probably bad form to plug a product on here, but I have no connection to them except that they have some nice little books on offer; as well as some odd hippy titles...
Re: Perpendiculars to a chord
Reply #5 - Jun 3rd, 2010, 3:14pm
 
blindfish wrote on Jun 3rd, 2010, 2:09pm:
I bet there are plenty of people on this forum who'll laugh that I had to look that up, but my memory is used to store origami folding sequences, not mathematical formulae Smiley


Smiley
Re: Perpendiculars to a chord
Reply #6 - Jun 3rd, 2010, 3:48pm
 
I just thought it over, but indeed this problem isn't really processing. It's math. Good old basic trigometry & math.

I guess the problem is that these perpendicular lines have to intersect those A and B points at 0 and Pi like in the Wolfram example??
Re: Perpendiculars to a chord
Reply #7 - Jun 6th, 2010, 6:39am
 
Yep - just the kind of maths that I no longer remember from my school days and have to dredge up with an embarrassing amount of effort (still I can fold this little chap from memory Smiley )

I couldn't resist the challenge so here's a demo.  Forget the -1/n formula for the perpendicular; in this case it's much simpler to just add 90 degrees to the angle of the chord!  TBH the only tricky part is calculating the intersection point between the perpendicular line and the chord - I just cheated and borrowed an implementation of the appropriate formula from the Processing Hacks section...

I'm sure my code could be optimised, and I've used some tricks to handle object drawing and mouse interaction, but it should give you the genenal idea...  All the relevant code is in the Circle class.
Re: Perpendiculars to a chord
Reply #8 - Jun 7th, 2010, 5:15am
 
Hi blindfish,

Thanks you so much for that! It is so helpful! (Also I'm very impressed at the Origami figure Smiley ).

I would have been seriously lost if you hadn't helped me with that!
I will obviously reference you and credit you in the sketch when it's finished. I will post the final piece here or to your email address if you prefer. It's only a small personal project but thank you so much for the help!
Page Index Toggle Pages: 1