mirror of
https://github.com/libretro/scummvm.git
synced 2025-01-23 11:04:44 +00:00
MATH: Refactor Line2d
- use standard linear equation Ax + Bx = C - simplify most calculations - remove default constructor - remove unused function
This commit is contained in:
parent
f2195d8dbf
commit
082e792460
@ -25,40 +25,29 @@
|
||||
|
||||
namespace Math {
|
||||
|
||||
Line2d::Line2d() :
|
||||
_a(0), _b(0), _c(0) {
|
||||
/* Linear equation used to describe the line:
|
||||
*
|
||||
* Ax + By = C
|
||||
*
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
Line2d::Line2d(const Vector2d &direction, const Vector2d &point) {
|
||||
Vector2d d = direction;
|
||||
if (fabsf(d.getX()) > 0.0001f) {
|
||||
_a = d.getY() / d.getX();
|
||||
_b = -1;
|
||||
} else {
|
||||
_a = 1;
|
||||
_b = 0;
|
||||
}
|
||||
|
||||
if (_b == 0) {
|
||||
_c = -point.getX();
|
||||
} else {
|
||||
_c = point.getY() - (d.getY() / d.getX()) * point.getX();
|
||||
}
|
||||
_a = direction.getX();
|
||||
_b = direction.getY();
|
||||
_c = point.getY() * (point.getX() + direction.getX()) - point.getX() * (point.getY() + direction.getY());
|
||||
}
|
||||
|
||||
Line2d Line2d::getPerpendicular(const Vector2d &point) const {
|
||||
Vector2d v(1, _b / _a);
|
||||
|
||||
return Line2d(v, point);
|
||||
return Line2d(Vector2d(_a, _b), point);
|
||||
}
|
||||
|
||||
Vector2d Line2d::getDirection() const {
|
||||
return Vector2d(1, _a);
|
||||
return Vector2d(-_b, _a);
|
||||
}
|
||||
|
||||
float Line2d::getDistanceTo(const Vector2d &point, Vector2d *intersection) const {
|
||||
float dist = fabsf(_a * point.getX() + _b * point.getY() + _c) / sqrt(_a * _a + _b * _b);
|
||||
float dist = fabsf(_a * point.getX() + _b * point.getY() - _c) / sqrt(_a * _a + _b * _b);
|
||||
|
||||
if (intersection) {
|
||||
intersectsLine(getPerpendicular(point), intersection);
|
||||
@ -67,28 +56,24 @@ float Line2d::getDistanceTo(const Vector2d &point, Vector2d *intersection) const
|
||||
}
|
||||
|
||||
bool Line2d::intersectsLine(const Line2d &line, Vector2d *pos) const {
|
||||
// if (*this == line) {
|
||||
// return false;
|
||||
// }
|
||||
float a1 = _a;
|
||||
float b1 = _b;
|
||||
float c1 = _c;
|
||||
|
||||
float a = _a;
|
||||
float b = _b;
|
||||
float c = _c;
|
||||
|
||||
float d = line._a;
|
||||
float e = line._b;
|
||||
float f = line._c;
|
||||
float a2 = line._a;
|
||||
float b2 = line._b;
|
||||
float c2 = line._c;
|
||||
|
||||
float x, y;
|
||||
|
||||
const float det = a * e - b * d;
|
||||
const float det = a1 * b2 - b1 * a2;
|
||||
|
||||
if (fabsf(det) < 0.0001f) {
|
||||
if (fabsf(det) < epsilon) {
|
||||
return false;
|
||||
}
|
||||
|
||||
x = (-c * e + b * f) / det;
|
||||
y = (-a * f + c * d) / det;
|
||||
x = (c1 * b2 - c2 * b1) / det;
|
||||
y = (a1 * c2 - a2 * c1) / det;
|
||||
|
||||
if (pos)
|
||||
*pos = Vector2d(x, y);
|
||||
@ -97,19 +82,15 @@ bool Line2d::intersectsLine(const Line2d &line, Vector2d *pos) const {
|
||||
}
|
||||
|
||||
bool Line2d::containsPoint(const Vector2d &point) const {
|
||||
float n = _a * point.getX() + _b * point.getY() + _c;
|
||||
return (n < 0.0001 && n > -0.0001);
|
||||
}
|
||||
|
||||
float Line2d::getYatX(float x) const {
|
||||
return -(_a * x + _c) / _b;
|
||||
float n = _a * point.getX() + _b * point.getY() - _c;
|
||||
return (fabsf(n) < epsilon);
|
||||
}
|
||||
|
||||
Common::StreamDebug &operator<<(Common::StreamDebug &dbg, const Math::Line2d &line) {
|
||||
if (fabsf(line._a) < 0.0001f) {
|
||||
dbg.nospace() << "Line2d: <y = " << (-line._a / line._b) << " * x + " << -line._c / line._b << ">";
|
||||
if (fabsf(line._a) < epsilon) {
|
||||
dbg.nospace() << "Line2d: <y = " << (-line._a / line._b) << " * x + " << line._c / line._b << ">";
|
||||
} else {
|
||||
dbg.nospace() << "Line2d: <x = " << (-line._b / line._a) << " * y + " << -line._c / line._a << ">";
|
||||
dbg.nospace() << "Line2d: <x = " << (-line._b / line._a) << " * y + " << line._c / line._a << ">";
|
||||
}
|
||||
|
||||
return dbg.space();
|
||||
|
@ -29,7 +29,6 @@ namespace Math {
|
||||
|
||||
class Line2d {
|
||||
public:
|
||||
Line2d();
|
||||
Line2d(const Vector2d &direction, const Vector2d &point);
|
||||
|
||||
Line2d getPerpendicular(const Vector2d &point = Vector2d()) const;
|
||||
@ -39,8 +38,6 @@ public:
|
||||
bool intersectsLine(const Line2d &line, Vector2d *pos) const;
|
||||
bool containsPoint(const Vector2d &point) const;
|
||||
|
||||
float getYatX(float x) const;
|
||||
|
||||
friend Common::StreamDebug &operator<<(Common::StreamDebug &dbg, const Line2d &line);
|
||||
|
||||
private:
|
||||
|
Loading…
x
Reference in New Issue
Block a user