MATH: Make implementation of Segment2d more robust

- prevent division by zero
This commit is contained in:
Christian Krause 2014-10-05 02:55:31 +02:00 committed by Paweł Kołodziejski
parent 6098b45776
commit f2195d8dbf
2 changed files with 11 additions and 2 deletions

View File

@ -169,14 +169,14 @@ bool Segment2d::intersectsSegment(const Segment2d &other, Vector2d *pos) {
float nume_b = ((_end.getX() - _begin.getX()) * (other._begin.getY() - _begin.getY())) -
((_end.getY() - _begin.getY()) * (other._begin.getX() - _begin.getX()));
if (denom == 0.0f) {
if (denom == 0.0f || d == 0.0f ) {
return false;
}
float ua = nume_a / denom;
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;
}

View File

@ -27,6 +27,15 @@
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) {
return x * x;
}