直线下的二维点
我有大量的点 (x,y)
例如 (15, 176) (65, 97) (72, 43) (102, 6) (191, 189) (90 , 163) (44, 168) (39, 47) (123, 37)
我需要找到符合以下条件的所有点(0,0) 到 (max-x, max-y)
。在此示例中,这些点将是 (0,0 ) 和 (191,189)
,因此我绘制了所有点,我需要找到来自 ( 的线下方的所有点0,0) 到 (191,189)
是否有任何标准算法可以做到这一点。
I have huge set of points (x,y)
for example (15, 176) (65, 97) (72, 43) (102, 6) (191, 189) (90, 163) (44, 168) (39, 47) (123, 37)
I need to find all the points which fits under (0,0) to (max-x, max-y)
. In this example these points will be (0,0 ) and (191,189)
, so I draw all the points, I need to find all the points which are under the line which is from (0,0) to (191,189)
Is there any standard algorithm to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 y * X < ,则点 (x, y) 位于从 (0, 0) 到 (X, Y) 的直线下方。 Y * x。
A point (x, y) is under the line from (0, 0) to (X, Y) if y * X < Y * x.
在线段(0,0)->(191,189)下方等于
这是您必须检查每个点的 2 个条件。
您需要 1 次整数比较、2 次 int 到浮点转换、1 次浮点乘法、每点 1 次浮点比较的成本
Being under the line segment (0,0)->(191,189) is equal to
Sow these are the 2 conditions you would have to check on each point.
You have costs of 1 integer comparison, 2 int to float casts, 1 float multiplication, 1 float comparison per point
(我假设“适合”意味着“在”之下,如“覆盖”中的那样。)
一种方法是使用例如 Bresenham 的线算法来确定线上的点(http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm),然后使用
std::set_intersection
(http://www.cplusplus.com/reference/algorithm/set_intersection/)找出哪些点既在直线上又在您拥有的原始点集中。(I'm assuming that by 'fits under' you mean 'under' as in 'overlaid by'.)
Well one approach would be to determine the points on the line using e.g. Bresenham's line algorithm (http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm) and then use
std::set_intersection
(http://www.cplusplus.com/reference/algorithm/set_intersection/) to find which points are both in the line and also in the original set of points you had.对于任何直线 y=mx+c,其中 m 是斜率,c 是 y 截距,
请使用以下值
a = y-mx-c 对于 x=0,y=0
b = y-mx-c 对于一般点
如果 a 和 b 具有相同的符号,则表示这些点在同一侧
否则它们位于我保留 (0,0) 的线的另一侧,
以便找到 a ,以便可以比较一般点的位置与原点。您也可以在其中输入任何其他点。
希望完全可以帮助您的问题
For any line y=mx+c where m is the slope and c is the y intercept
use the values
a = y-mx-c for x=0,y=0
b = y-mx-c for the general point
if a and b have the same sign means the points are on the same side
otherwise they are on the opposite side of the line
I have kept (0,0) for finding a so that one can compare the general point's position w.r.t. origin. You could input any other point in it too..
Hope that completely helps your problem