我有两个点存储在两个变量中,它们形成一条线。我想从该线的一个终点找到一条垂直于该线的点。 (P1-P2)垂直于直线(P2,P3)并且相交于直线(P2,P3),并且相交(P1,P2)在P2。计算一条垂直于线条的点
1
A
回答
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);
相关问题
- 1. 计算垂直于另一条线的中点的线
- 2. 画一条垂直线
- 3. 对于其余的垂直空间垂直居中一条线
- 4. 迭代通过垂直于另一条线的N个点
- 5. 在谷歌地图中画一条垂直于两点的线
- 6. 确定一条垂直于另一条线的线,该线由python
- 7. 垂直中心多条线
- 8. 从一点到两点所代表的一条直线的垂直距离
- 9. 向ggplot添加一个垂直线条
- 10. 在Core Plot中画一条垂直线?
- 11. 延长chart.js水平条形图以包括一条垂直线
- 12. 用导航条绘制一条小垂直线
- 13. 垂直对齐线条上方的div
- 14. 基于rows.js的垂直滚动条jquery
- 15. 无法在一条线上对齐两个div的垂直线
- 16. 垂直滚动条
- 17. 垂直进度条
- 18. 垂直滚动条
- 19. 使用DrawingBrush创建两条垂直线
- 20. WPF DataGrid垂直线条粗细
- 21. Java图像分析 - 计算垂直线
- 22. 垂直对齐浮动图像旁边的多条线条
- 23. 垂直计算列
- 24. 如何计算垂直线段的终点?
- 25. PBM_SETMARQUEE不适用于垂直进程条?
- 26. Android的垂直动作条
- 27. 从一个起点和三维角度计算一条线
- 28. 一个条件的垂直总和
- 29. 如何在matlab中绘制一条直线的垂直平分线?
- 30. 计算一个点的COORDS垂直的线,在角度考虑两分,角计算第三点的COORDS
应该有一些更多的约束(至少一个更像P1P2的距离),因为你已经提到躺在线上的任何一点工作条件垂直于P1P2并穿过P2。 – mushfek0001 2015-04-05 10:56:26
垂直点与线之间的距离是否是任意选择的? – MCHAppy 2015-04-05 11:23:29