gpt4 book ai didi

C++ float 比较

转载 作者:行者123 更新时间:2023-11-28 08:16:42 24 4
gpt4 key购买 nike

假设您有一个矩形,左下角点为 0,0,右上角点为 100,100。现在两条线与矩形相交。我必须找出交点的坐标。我已经做到了。现在的问题是我无法判断它是否在矩形内。我用了双重比较。但我认为它给了我错误的答案。假设交点为 ( x , y )。我使用此检查进行比较:if( x >= 0.0 && x <= 100.0 && y >= 0.0 && y <= 100.0 )。我该怎么办?

//this function generates line
line genline( int x1 , int y1 , int x2 , int y2 ){
line l ;
l.A = y2 - y1 ;
l.B = x1 - x2 ;
l.C = l.A * x1 + l.B * y1 ;
return l ;
}
//this function checks intersection
bool intersect( line m ,line n ) {
int det = m.A * n.B - m.B * n.A ;
if( det == 0 ){
return false ;
}
else {
double x = ( n.B * m.C - m.B * n.C ) / ( det * 1.0 ) ;
double y = ( m.A * n.C - n.A * m.C ) / ( det * 1.0 ) ;
if( x >= 0.0 && x <= L && y >= 0.0 && y <= W ) { return true ; }
else{ return false ; }
}
}

编辑:两条线都被拉伸(stretch)到无穷远。

最佳答案

您的数学看起来是正确的。顺便说一句,如果一条线与某个东西相交,它总是在那个东西里面。

关于C++ float 比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7494130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com