如何在C中找到二维等边三角形的坐标?
我有 2 个点的坐标 (x,y)。我想构建第三个点,使这三个点形成一个等边三角形。
如何计算第三点?
谢谢
I have the coordinates (x,y) of 2 points. I want to build the third point so that these 3 points make an equilateral triangle.
How can I calculate the third point?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将第二个点绕第一个点旋转 60° 来找到第三个点的位置。
像这样的事情:
You could rotate the second point 60° around first to find the location of the third point.
Something like this:
我们将这两个点称为 A 和 B。将 AB 平分,将此点称为 C。求 AB 的斜率 (YA-YB / XA-XB),称之为 m。求垂直于该点的垂线 (-1/m) 并将其称为 m2。然后计算一条线段 CD,其长度为 sin(60) * length(AB),斜率为 m2(将有两个这样的点,AB 的每一侧各一个)。 ABD 就是你的等边三角形。
显然,这是一种“建设性”方法。您还应该能够通过求解一组线性方程来做到这一点。我还没有尝试找出适合这种情况的正确方程组,但这种方法在数值上往往更稳定,并且特殊情况较少(例如,使用构造性版本,必须处理 0 的斜率)特别)。
Let's call your two points A and B. Bisect AB, call this point C. Find the slope of AB (YA-YB / XA-XB), call it m. Find the perpendicular to that (-1/m) and call it m2. Then compute a segment CD whose length is sin(60) * length(AB), at the slope m2 (there will be two such points, one to each side of AB). ABD is then your equilateral triangle.
That, obviously, is a "constructive" method. You should also be able to do it by solving a set of linear equations. I haven't tried to figure out the right system of equations for this case, but this approach tends to be somewhat more stable numerically, and has fewer special cases (e.g., with the constructive version, a slope of 0 has to be dealt with specially).
对于 BlueRaja 的挑战,请转到帖子末尾:
使用平移和旋转回答:
说点是 P(x1,y1) 和 Q(x2,y2)。
由于它是图形,因此您可以使用变换来理解要点。
首先平移轴,使 P 为原点。
接下来将 Q 绕 P 旋转 60 度(或 -60 度以获得另一个可能的点)。
当 P 为原点时,这将为您提供第三个点(即 R)的坐标。
翻译回来就可以了。
您可以使用标准图形 API,它可以为您解决精度等问题。没有头痛。
当然,你可以做数学计算,实际上得出一个公式并使用它,这可能会更快,但问题可能会因为偏离主题而结束;-)
接受 BlueRaja 的挑战:这里有一个方法,它不使用三角学。
给定点 P(x1,y1) 和 Q(x2,y2)
假设我们需要 (R) 找到的点是 (x3,y3)。
令 T 为 PQ 的中点。
我们知道三角形 PQR 的面积(因为它是等边的并且我们知道边)
并且我们知道三角形 PRT 的面积(之前面积的 1/2)。
现在三角形的面积可以写成以坐标为条目的行列式:
我们有两个这样的方程(它们是线性的),求解 x3 和 y3。
For BlueRaja's challenge go to end of post:
Answer using translation and rotation:
Says points are P(x1,y1) and Q(x2,y2).
Since it is graphics, you can use tranforms to get the point.
First translate axes so P is the origin.
Next rotate Q around P by 60 degrees (or -60 to get the other possible point).
This gives you the coordinates of the third point say R, when P is the origin.
Translate back and there you have it.
You can use standard graphics API which take care of precision etc issues for you. No headaches.
Of course you could do the math and actually come up with a formula and use that and that might be faster, but then the question might get closed as off-topic ;-)
To take up BlueRaja's challenge: Here is a method which does not use trigonometry.
Given points P(x1,y1) and Q(x2,y2)
Say the point we need (R) to find is (x3,y3).
Let T be midpoint of PQ.
We know the area of triangle PQR (as it is equilateral and we know the side)
and we know the area of triangle PRT (1/2 the earlier area).
Now area of a triangle can be written as a determinant having the co-ordinates as entries:
We have two such equations (which are linear), solve for x3 and y3.
阅读完这些帖子(特别是 vkit 的帖子)后,我编写了这段简单的代码,它将在一个方向上实现这一点(请记住有两点)。对其他情况的修改应该是微不足道的。
After reading the posts (specially vkit's) I produced this simple piece of code which will do the trick for one direction (remember that there are two points). The modification for the other case shold be trivial.