如何计算垂直线段的端点?

发布于 2024-08-14 16:46:54 字数 442 浏览 7 评论 0原文

我知道线段的端点以及我想要创建的垂直端盖的距离/大小,但我需要计算垂直线的端点。我一直在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弱骨蛰伏 2024-08-21 16:46:54

如果 B1 是 2 个红点之间的蓝点,B2 是另一个蓝点,则执行此操作的方法是:

  • 查找 B1 - B2
  • 标准化该向量
  • 然后将该向量放大红点之间距离的一半
  • 旋转 90将此
  • 向量添加到 B1(这是 R1)
  • 从 B1 中减去此向量(这是 R2)

以上所有内容都相当简单 - 最棘手的部分是弄清楚如何在文本中写出来!

这可能会有所帮助 - 矩阵旋转 90 度:

[ 0  -1 ]
[ 1   0 ]

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:

  • Find B1 - B2
  • Normalise this vector
  • Then scale this vector up by half the distance between the red points
  • Rotate by 90 degrees
  • Add this vector to B1 (this is R1)
  • Subtract this vector from B1 (This is R2)

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:

[ 0  -1 ]
[ 1   0 ]
骑趴 2024-08-21 16:46:54

解决这个问题的简单方法不是考虑斜率 m,而是考虑 x 和 y 的变化,我将其称为 dx, dy(来自微积分符号)。
原因是一方面,处理垂直线的斜率是无限的,并且在任何情况下,您都不需要使用三角函数,此代码会更快、更简单。

dx = x2 - x1;
dy = y2 - y1;

我在这里假设点 2 是所需线的交点。

好的,所以垂直线的斜率是第一条线的负倒数。
有两种方法可以做到这一点:

dx2 = -dy
dy2 = dx

或者

dx2 = dy
dy2 = -dx

这对应于两个方向,一个向右转,另一个向左转。

但是,dx 和 dy 会缩放到原始线段的长度。你的垂线有不同的长度。

这是两点之间的长度:

double length(double x1, double y1, double x2, double y2) {
 return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}

做你想做的,去一侧或另一侧,是:

double scale = length(whatever length you want to go)/sqrt(dx*dx+dy*dy);
double dx2 = -dy * scale;
double dy2 = dx * scale

然后另一侧再次相同。
我刚刚意识到我的示例有点 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.

dx = x2 - x1;
dy = y2 - y1;

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:

dx2 = -dy
dy2 = dx

or

dx2 = dy
dy2 = -dx

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:

double length(double x1, double y1, double x2, double y2) {
 return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}

Do what you want, to go to one side or the other, is:

double scale = length(whatever length you want to go)/sqrt(dx*dx+dy*dy);
double dx2 = -dy * scale;
double dy2 = dx * scale

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.

孤独岁月 2024-08-21 16:46:54

您知道蓝线的斜率,我们将其称为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/subtract delta_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文