2015-04-05 92 views
1

我有两个点存储在两个变量中,它们形成一条线。我想从该线的一个终点找到一条垂直于该线的点。 (P1-P2)垂直于直线(P2,P3)并且相交于直线(P2,P3),并且相交(P1,P2)在P2。计算一条垂直于线条的点

+0

应该有一些更多的约束(至少一个更像P1P2的距离),因为你已经提到躺在线上的任何一点工作条件垂直于P1P2并穿过P2。 – mushfek0001 2015-04-05 10:56:26

+0

垂直点与线之间的距离是否是任意选择的? – MCHAppy 2015-04-05 11:23:29

回答

0

首先,角度:

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; 
} 

为了得到垂直,你必须添加PI/2至您的两点定义的直线的角度。

一旦你的角度,计算公式为:

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

我得到的答案在http://jsfiddle.net/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); 
+0

这是一个JavaScript代码,为什么'java'标签? – MCHAppy 2015-04-05 11:31:26

+0

刚刚得到一个例子,所以我把它转换成Java程序。但我分享了一些原始的例子 – 2015-04-06 03:21:19

0

您可以将您点vec2d,然后用一些数学公式来获得垂直点。

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)); 
} 
0

如果你想使用Java,我可以推荐使用JTS。创建一个LineSegment并使用pointAlongOffset方法。由于点P1和P2的代码看起来像:

// 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);