检查 3 个点是否在同一条线上

发布于 2024-09-25 21:22:54 字数 67 浏览 3 评论 0原文

我想知道一段代码,它实际上可以告诉我 2D 空间中的 3 个点是否在同一条线上。伪代码也足够了,但 Python 更好。

I want to know a piece of a code which can actually tell me if 3 points in a 2D space are on the same line or not. A pseudo-code is also sufficient but Python is better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

枕头说它不想醒 2024-10-02 21:22:54

您可以检查 ABC 三角形的面积是否为 0:

[ Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By) ] / 2

当然,您实际上不需要除以 2。

You can check if the area of the ABC triangle is 0:

[ Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By) ] / 2

Of course, you don't actually need to divide by 2.

_蜘蛛 2024-10-02 21:22:54

这是 C++,但您可以将其改编为 python:

bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) {
  return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);
}

基本上,我们正在检查点 1 和点 2 以及点 1 和点 3 之间的斜率是否匹配。斜率是 y 的变化除以 x 的变化,因此我们有:

y1 - y2     y1 - y3
-------  =  --------
x1 - x2     x1 - x3

交叉相乘得到 (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);

请注意,如果您使用双打,您可以检查 epsilon:

bool collinear(double x1, double y1, double x2, double y2, double x3, double y3) {
  return fabs((y1 - y2) * (x1 - x3) - (y1 - y3) * (x1 - x2)) <= 1e-9;
}

This is C++, but you can adapt it to python:

bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) {
  return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);
}

Basically, we are checking that the slopes between point 1 and point 2 and point 1 and point 3 match. Slope is change in y divided by change in x, so we have:

y1 - y2     y1 - y3
-------  =  --------
x1 - x2     x1 - x3

Cross multiplying gives (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);

Note, if you are using doubles, you can check against an epsilon:

bool collinear(double x1, double y1, double x2, double y2, double x3, double y3) {
  return fabs((y1 - y2) * (x1 - x3) - (y1 - y3) * (x1 - x2)) <= 1e-9;
}
泛滥成性 2024-10-02 21:22:54

y - y0 = a(x-x0) (1) 而 a = (y1 - y0)/(x1 - x0)A(x0, y0) ) B(x1,y1) C(x2,y2)。查看C是否满足(1)。您只需替换适当的值即可。

详细信息

y - y0 = a(x-x0) (1) while a = (y1 - y0)/(x1 - x0) and A(x0, y0) B(x1, y1) C(x2, y2). See whether C statisfies (1). You just replace the appropriate values.

Details

嘿嘿嘿 2024-10-02 21:22:54

阅读,并用它来查找通过前两点的直线方程。按照说明查找 mb。然后对于第三点,计算 mx + b - y。如果结果为零,则第三个点与前两个点在同一直线上。

Read this, and use it to find the equation of a line through the first two points. Follow the instructions to find m and b. Then for your third point, calculate mx + b - y. If the result is zero, the third point is on the same line as the first two.

公布 2024-10-02 21:22:54

规则 1:在任何线性二维空间中,两个点始终在同一条线上。

取 2 个点并建立一个方程来表示穿过它们的一条线。
然后检查第三个点是否也在该线上。

祝你好运。

Rule 1: In any linear 2d space, two points are always on the same line.

Take 2 points and build an equation that represents a line through them.
Then check if the third point is also on that line.

Good luck.

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