您如何确定一个点位于线段上的其他两个点之间?

How can you determine a point is between two other points on a line segment?(您如何确定一个点位于线段上的其他两个点之间?)
本文介绍了您如何确定一个点位于线段上的其他两个点之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设您有一个二维平面,上面有 2 个点(称为 a 和 b),每个点用一个 x 整数和一个 y 整数表示.

Let's say you have a two dimensional plane with 2 points (called a and b) on it represented by an x integer and a y integer for each point.

如何确定另一个点 c 是否在 a 和 b 定义的线段上?

How can you determine if another point c is on the line segment defined by a and b?

我最常使用 python,但任何语言的示例都会有所帮助.

I use python most, but examples in any language would be helpful.

推荐答案

检查 (ba) 和 (ca) 的 叉积 是否为 0,正如 Darius Bacon 所说,告诉您积分是否a、b 和 c 对齐.

Check if the cross product of (b-a) and (c-a) is 0, as tells Darius Bacon, tells you if the points a, b and c are aligned.

但是,由于您想知道 c 是否介于 a 和 b 之间,您还必须检查 (ba) 和 (ca) 的 点积 是否 并且小于 a 和 b 之间距离的平方.

But, as you want to know if c is between a and b, you also have to check that the dot product of (b-a) and (c-a) is positive and is less than the square of the distance between a and b.

在未优化的伪代码中:

def isBetween(a, b, c):
    crossproduct = (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y)

    # compare versus epsilon for floating point values, or != 0 if using integers
    if abs(crossproduct) > epsilon:
        return False

    dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y)*(b.y - a.y)
    if dotproduct < 0:
        return False

    squaredlengthba = (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y)
    if dotproduct > squaredlengthba:
        return False

    return True

这篇关于您如何确定一个点位于线段上的其他两个点之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)