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 › Math question
Page Index Toggle Pages: 1
Math question (Read 543 times)
Math question
Jan 19th, 2009, 9:55am
 
I think it might be simple but I cant get it.

I have a line formed by joining two points (x1, y1) and (x2,y2).

Now I want to draw a line perpendicular to this of length 10 pixels. If I consider the direction of line as moving from x1,y1 to x2,y2, then the point should be to the left of this line.

I uploaded a image for reference:
http://www.ssdesigninteractive.com/math_problem.jpg
Re: Math question
Reply #1 - Jan 19th, 2009, 10:30am
 
ok, I found something here:
http://mathforum.org/library/drmath/view/55038.html

Using the equation given here, I am able to draw perpendicular lines, but not at exact distance that I want. Its all concentrated to the center.

Any suggestions? Here is my code so far (click on screen to generate fresh spirals):

Quote:
//
float len = random(220);
float radius1 = random(20, 130);
float radius2 = random(10, 100);
float position = random(101);
float num = random(5, 10);
float prevX = 0.0;
float prevY = 0.0;
float x = 0.0;
float y = 0.0;
//
//
float step = 1 * PI / 180.0;
float theta = 0;
int i=0;
float count = 0;

//
void setup(){
  size(500, 500, P3D);
  background(255);
  stroke(0, 20);
}

void draw(){
    count+=1;
    translate(width/2, height/2);
      prevX = x;
      prevY = y;
      x = len * cos(theta) - position * cos(len * theta / radius2);
      y = len * sin(theta) - position * sin(len * theta / radius2);
      theta += step;
      i++;
      if(prevX!=0){
        stroke(0);
        strokeWeight(4);
        line(prevX, prevY, x,y);
        // This is where i am calculating the perpendiculars
        float d = 10;
        float x3 = ((d)*(y-prevY))/sqrt(pow((x-prevX), 2)+pow((y-prevY),2));
        float y3 = ((d)*(x-prevX))/sqrt(pow((x-prevX), 2)+pow((y-prevY),2));
        //////////////////////////////////////////////
        stroke(color(#ff3300));
        strokeWeight(1);
        line(x, y, x3, y3);
      }
}
void mousePressed(){
  background(255);
  initL();
}

void initL(){
  len = random(220);
  radius1 = random(20, 130);
  radius2 = random(10, 100);
  position = random(101);
  num = random(5, 10);
  prevX = 0;
  prevY = 0;
  x = 0;
  y = 0;
}



Re: Math question
Reply #2 - Jan 19th, 2009, 10:42am
 
Code:
PVector point1, point2;
boolean bFirst = true;

void setup()
{
 size(800, 800);
 point1 = new PVector(width/5, width/3);
 point2 = new PVector(4*width/5, 2*width/3);
}

void draw()
{
 background(#ABCDEF);
 
 // Draw main vector
 stroke(#002244);
 strokeWeight(3);
 line(point1.x, point1.y, point2.x, point2.y);
 ellipse(point1.x, point1.y, 7, 7);
 
 // Compute perpendicular vector: (y, -x)
 PVector perp = new PVector(point2.y - point1.y, point1.x - point2.x);
 // Length 1
 perp.normalize();
 // Length to n pixels
 perp.mult(100);  // Put 10 for 10 pixels
 // Move to point1
 perp.add(point1);
 // Draw it
 stroke(#AA2244);
 strokeWeight(2);
 line(point1.x, point1.y, perp.x, perp.y);
}

void mousePressed()
{
if (bFirst)
{
point1.set(mouseX, mouseY, 0);
}
else
{
point2.set(mouseX, mouseY, 0);
}
bFirst = !bFirst;
}
Re: Math question
Reply #3 - Jan 19th, 2009, 11:04am
 
Hi,
Thanks. That does look like what I want.
I will need to digest this a bit as I have not used PVector often Sad
Re: Math question
Reply #4 - Jan 21st, 2009, 10:26am
 
Thanks PhiLho,
That solved my problem Smiley

Works wonderfully.

Regards
Page Index Toggle Pages: 1