我正在检查直线交叉点,需要确定交叉点(x
,y
)是否在线段l2
的边界框内(由点p1
和p2
组成)
以下打印输出说明了我的问题:
交叉点为(100,300)
print("x",x,">=",math.min(l2.p1.x,l2.p2.x),x >= math.min(l2.p1.x,l2.p2.x))
print("x",x,"<=",math.max(l2.p1.x,l2.p2.x),x <= math.max(l2.p1.x,l2.p2.x))
print("y",y,">=",math.min(l2.p1.y,l2.p2.y),y >= math.min(l2.p1.y,l2.p2.y))
print("y",y,"<=",math.max(l2.p1.y,l2.p2.y),y <= math.max(l2.p1.y,l2.p2.y))
是的,是的:
x 100 >= 100 true
x 100 <= 100 false
y 300 >= 140 true
y 300 <= 300 false
发生了什么事,怎么能成为固定装置?
(Lua版本5.2.3)
答案 0 :(得分:0)
向浮点运算问好:http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
例如
> string.format("%.32f", 1/3)
0.33333333333333331482961625624739
> 1/3 == 0.3333333
false
所以这取决于你对X和lp *的计算方式。 在比较浮点数时应使用公差。
> math.abs(1/3 - 0.33) == 0
false
> math.abs(1/3 - 0.33333) < 1/10^6
false
> math.abs(1/3 - 0.33333) < 1/10^5
true