MATH: Make implementation of Segment2d more robust
- prevent division by zero
This commit is contained in:
parent
6098b45776
commit
f2195d8dbf
2 changed files with 11 additions and 2 deletions
|
@ -169,14 +169,14 @@ bool Segment2d::intersectsSegment(const Segment2d &other, Vector2d *pos) {
|
||||||
float nume_b = ((_end.getX() - _begin.getX()) * (other._begin.getY() - _begin.getY())) -
|
float nume_b = ((_end.getX() - _begin.getX()) * (other._begin.getY() - _begin.getY())) -
|
||||||
((_end.getY() - _begin.getY()) * (other._begin.getX() - _begin.getX()));
|
((_end.getY() - _begin.getY()) * (other._begin.getX() - _begin.getX()));
|
||||||
|
|
||||||
if (denom == 0.0f) {
|
if (denom == 0.0f || d == 0.0f ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
float ua = nume_a / denom;
|
float ua = nume_a / denom;
|
||||||
float ub = nume_b / d;
|
float ub = nume_b / d;
|
||||||
|
|
||||||
if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
|
if (ua < 0.0f || ua > 1.0f || ub < 0.0f || ub > 1.0f) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,15 @@
|
||||||
|
|
||||||
namespace Math {
|
namespace Math {
|
||||||
|
|
||||||
|
/* Math::epsilon is a constant with a small value which is used for comparing
|
||||||
|
* floating point numbers.
|
||||||
|
*
|
||||||
|
* The value is based on the previous hard-coded numbers in
|
||||||
|
* Line2d.cpp. Smaller numbers could be used unless they are
|
||||||
|
* smaller than the float granularity.
|
||||||
|
*/
|
||||||
|
static const float epsilon = 0.0001f;
|
||||||
|
|
||||||
inline float square(float x) {
|
inline float square(float x) {
|
||||||
return x * x;
|
return x * x;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue