views:

506

answers:

6

Hi All,

How can I draw a perpendicular on a line segment from a given point? My line segment is defined as (x1, y1), (x2, y2), If I draw a perpendicular from a point (x3,y3) and it meets to line on point (x4,y4). I want to find out this (x4,y4).

Thankx.

+10  A: 

From wiki:

In algebra, for any linear equation y=mx + b, the perpendiculars will all have a slope of (-1/m), the opposite reciprocal of the original slope. It is helpful to memorize the slogan "to find the slope of the perpendicular line, flip the fraction and change the sign." Recall that any whole number a is itself over one, and can be written as (a/1)

To find the perpendicular of a given line which also passes through a particular point (x, y), solve the equation y = (-1/m)x + b, substituting in the known values of m, x, and y to solve for b.

The slope of the line, m, through (x1, y1) and (x2, y2) is m = (y1 - y2) / (x1 - x2)

Mitch Wheat
+2  A: 

Find out the slopes for both the lines, say slopes are m1 and m2 then m1*m2=-1 is the condition for perpendicularity.

Prasoon Saurav
you don't know the equation of both lines, until you have worked out the intersection point.
Mitch Wheat
taking the intersection point as x4,y4,we will have two linear equations in x4 and y4 and we can solve that easily.
Prasoon Saurav
+6  A: 

You know both the point and the slope, so the equation for the new line is:

y-y3=m*(x-x3)

Since the line is perpendicular, the slope is the negative reciprocal. You now have two equations and can solve for their intersection.

y-y3=-(1/m)*(x-x3)
y-y1=m*(x-x1)
Dan Lorenc
I just know one line segment (x1,y1)(x2,y2) and a point (x3, y3). I need a way to find (x4,y4).
Zinx
So, find the slope between point 1 and point 2, and take the negative reciprocal. Remember that a line is `y=mx+b` -- `m` here is the slope you just found, b you can solve for using p3. You now have all that's needed for a line.
Thanatos
+3  A: 

You will often find that using vectors makes the solution clearer...

Here is a routine from my own library:

public class Line2  {

Real2 from;
Real2 to;
Vector2 vector;
Vector2 unitVector = null;


    public Real2 getNearestPointOnLine(Real2 point) {
     unitVector = to.subtract(from).getUnitVector();
     Vector2 lp = new Vector2(point.subtract(this.from));
     double lambda = unitVector.dotProduct(lp);
     Real2 vv = unitVector.multiplyBy(lambda);
     return from.plus(vv);
    }

}

You will have to implement Real2 (a point) and Vector2 and dotProduct() but these should be simple:

The code then looks something like:

Point2 p1 = new Point2(x1, y1);
Point2 p2 = new Point2(x2, y2);
Point2 p3 = new Point2(x3, y3);
Line2 line = new Line2(p1, p2);
Point2 p4 = getNearestPointOnLine(p3);

The library (org.xmlcml.euclid) is at: http://sourceforge.net/projects/cml/

and there are unit tests which will exercise this method and show you how to use it.

@Test
public final void testGetNearestPointOnLine() {
 Real2 p = l1112.getNearestPointOnLine(new Real2(0., 0.));
 Real2Test.assertEquals("point", new Real2(0.4, -0.2), p, 0.0000001);
}
peter.murray.rust
+2  A: 

Hi

Compute the slope of the line joining points (x1,y1) and (x2,y2) as m=(y2-y1)/(x2-x1)

Equation of the line joining (x1,y1) and (x2,y2) using point-slope form of line equation, would be y-y2 = m(x-x2)

Slope of the line joining (x3,y3) and (x4,y4) would be -(1/m)

Again, equation of the line joining (x3,y3) and (x4,y4) using point-slope form of line equation, would be y-y3 = -(1/m)(x-x3)

Solve these two line equations as you solve a linear equation in two variables and the values of x and y you get would be your (x4,y4)

I hope this helps.

cheers

Andriyev
+3  A: 

I solved the equations for you:

k = ((y2-y1) * (x3-x1) - (x2-x1) * (y3-y1)) / ((y2-y1)^2 + (x2-x1)^2)
x4 = x3 - k * (y2-y1)
y4 = y3 + k * (x2-x1)

Where ^2 means squared

Ray Hidayat
I hope I haven't made a mistake transferring these from paper to computer!
Ray Hidayat
Thanks bro, its working fine. Thanks a lot.
Zinx
Excellent, you're welcome!
Ray Hidayat