admin管理员组

文章数量:1353633

I have two points store in two variable, which forms a line. I want to find a point which is perpendicular to that line from one end point in that line.

Suppose I have two points P1(x1,y1) and P2(x2,y2) then i want to find a third point P3 such that line(P1-P2) is perpendicular to line(P2,P3) and intersect at P2.

I have two points store in two variable, which forms a line. I want to find a point which is perpendicular to that line from one end point in that line.

Suppose I have two points P1(x1,y1) and P2(x2,y2) then i want to find a third point P3 such that line(P1-P2) is perpendicular to line(P2,P3) and intersect at P2.

Share Improve this question edited Apr 6, 2015 at 3:19 Umashankar Prasad asked Apr 5, 2015 at 10:45 Umashankar PrasadUmashankar Prasad 211 silver badge6 bronze badges 3
  • There should be some more constraints (at least one more like the distance from P1P2) because the conditions you've mentioned works for any point lying on a line perpendicular to P1P2 and going through P2. – mushfek0001 Commented Apr 5, 2015 at 10:56
  • The distance between the perpendicular point and the line, is it arbitrary chosen? – MCHAppy Commented Apr 5, 2015 at 11:23
  • Did you find the solution here? – Victor Commented Dec 16, 2018 at 19:20
Add a ment  | 

5 Answers 5

Reset to default 5

First, the angle:

public static double angle (double x1, double y1, double x2, double y2) {
    double xdiff = x1 - x2;
    double ydiff = y1 - y2;
    //double tan = xdiff / ydiff;
    double atan = Math.atan2(ydiff, xdiff);
    return atan;
}

To get the perpendicular, you must add PI/2 to the angle of the line defined by your two points.

Once you have that angle, the formula is:

x = interceptPt.x + sin(perp_angle) * distance;
y = interceptPt.y + cos(perp_angle) * distance;

If you want to use Java I can remend to use JTS. Create a LineSegment and use the pointAlongOffset method. Given Points p1 and p2 the code would look like that:

// create LineSegment
LineSegment ls = new LineSegment(p1.getX(), p1.getY(), p2.getX(), p2.getY());
// perpendicular distance to line
double offsetDistance = 10;
// calculate Point right to start point
Coordinate startRight = ls.pointAlongOffset(0, offsetDistance);
// calculate Point left to start point
Coordinate startLeft = ls.pointAlongOffset(0, -offsetDistance);
// calculate Point right to end point
Coordinate endRight = ls.pointAlongOffset(1, offsetDistance);
// calculate Point left to end point
Coordinate endLeft = ls.pointAlongOffset(1, -offsetDistance);

ControlAltDel is already answered but he did a mistake, replaced cos to sin

x = interceptPt.x + cos(angle + 90) * distance;

y = interceptPt.y + sin(angle + 90) * distance;

x,y is point away from (interceptPt.x,interceptPt.y) at (distance) .

(interceptPt.x,interceptPt.y) is point in your line where perpendicular start to drawn.

angle = your line angle with horizontal axis

I got the answer at http://jsfiddle/eLxcB/2/

// Start and end point
var startX = 120
var startY = 150
var endX = 180
var endY = 130
R.circle(startX,startY,2);

// Calculate how far above or below the control point should be
var centrePointX = startX
var centrePointY = startY;

// Calculate slopes and Y intersects
var lineSlope = (endY - startY) / (endX - startX);
var perpendicularSlope = -1 / lineSlope;
var yIntersect = centrePointY - (centrePointX * perpendicularSlope);

// Draw a line between the two original points
R.path('M '+startX+' '+startY+', L '+endX+' '+endY);

// Plot some test points to show the perpendicular line has been found
R.circle(100, (perpendicularSlope * 100) + yIntersect, 2);

You can store your points in vec2d, then use some mathematical equations to get the perpendicular point.

vec2d getPerpendicularPoint(vec2d A, vec2d B, float distance)
{
   vec2d M = (A + B) / 2;
   vec2d p = A - B;
   vec2d n = (-p.y, p.x);
   int norm_length = sqrt((n.x * n.x) + (n.y * n.y));
   n.x /= norm_length;
   n.y /= norm_length;
   return (M + (distance * n));
}

本文标签: javaCalculate a point which is perpendicular to a lineStack Overflow