如何计算垂直线段的端点?
我知道线段的端点以及我想要创建的垂直端盖的距离/大小,但我需要计算垂直线的端点。我一直在用 45-45-90 三角形和点积将头撞在墙上,但我似乎无法将它们组合在一起。
我知道蓝色点和到红色点的距离,我需要找到红色点。
在标记为重复之前,我尝试了 这个问题,但它导致端盖始终是垂直的。
http://rauros.net/files/caps.png http://rauros.net/files/caps .png
I know the end points of a line segment and the distance/size of the perpendicular end caps I'd like to create but I need to calcuate the end points of the perpendicular line. I've been banging my head against the wall using either 45-45-90 triangles and dot products but I just can't seem to make it come together.
I know the points in blue and the distance to the points in red, I need to find the points in red.
Before marking as duplicate, I tried the answer posted in this question but it resulted in end caps which were always vertical.
http://rauros.net/files/caps.png http://rauros.net/files/caps.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 B1 是 2 个红点之间的蓝点,B2 是另一个蓝点,则执行此操作的方法是:
以上所有内容都相当简单 - 最棘手的部分是弄清楚如何在文本中写出来!
这可能会有所帮助 - 矩阵旋转 90 度:
If B1 is the blue point between the 2 red points, and B2 is the other blue point then the way to do this is:
All of the above is fairly straightforward - the trickiest bit would be figuring out how to write it out in text!
This might be helpful though - matrix to rotate by 90 degrees:
解决这个问题的简单方法不是考虑斜率 m,而是考虑 x 和 y 的变化,我将其称为 dx, dy(来自微积分符号)。
原因是一方面,处理垂直线的斜率是无限的,并且在任何情况下,您都不需要使用三角函数,此代码会更快、更简单。
我在这里假设点 2 是所需线的交点。
好的,所以垂直线的斜率是第一条线的负倒数。
有两种方法可以做到这一点:
或者
这对应于两个方向,一个向右转,另一个向左转。
但是,dx 和 dy 会缩放到原始线段的长度。你的垂线有不同的长度。
这是两点之间的长度:
做你想做的,去一侧或另一侧,是:
然后另一侧再次相同。
我刚刚意识到我的示例有点 C++,因为我使用了 sqrt,但差异很小。请注意,通过组合平方根,您可以更有效地编写代码。
The easy way around this one is not to think in terms of slope m, but rather the change in x and y, which I call dx, dy (from the calculus notation).
The reason is for one thing, that dealing with a slope for a vertical line is infinite, and in any case, you don't need to use trig functions, this code will be faster and simpler.
I am assuming here that point 2 is the intersection of the desired line.
Ok, so the perpendicular line has a slope with the negative reciprocal of the first.
There are two ways to do that:
or
this corresponds to the two directions, one turning right, and the other left.
However, dx and dy are scaled to the length of the original line segment. Your perpendicular has a different length.
Here's the length between two points:
Do what you want, to go to one side or the other, is:
and then the same again for the other side.
I just realized my example is somewhat c++, since I used sqrt, but the differences are trivial. Note that you can write the code more efficiently, combining the square roots.
您知道蓝线的斜率,我们将其称为
m
。垂直于蓝线的线将具有斜率-1/m
。要找到 x 坐标,您需要一些三角函数,
sine \theta = d / delta_x
,其中 \theta 是 x 轴蓝线的角度,d 是到其中之一的距离红点与蓝点。然后将delta_x
加/减到您希望线垂直的蓝点的 x 坐标。现在您可以使用点斜率公式来计算 y 坐标。You know the slope of the blue line, let's call it
m
. And a line perpendicular to the blue line will have slope-1/m
.to find the x-coordinate you need some trig,
sine \theta = d / delta_x
, where \theta is the angle of the blue line for the x-axis and d is the distance to one of the red points from the blue point. Then add/subtractdelta_x
to the x-coordinate of the blue point you want the line to be perpendicular to. Now you can use the point-slope formula to figure out the y coordinate.