[Scummvm-git-logs] scummvm master -> 082e7924606d351c7194120a67093d9bb8a5de70

aquadran aquadran at gmail.com
Tue Dec 8 18:27:18 UTC 2020


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
082e792460 MATH: Refactor Line2d


Commit: 082e7924606d351c7194120a67093d9bb8a5de70
    https://github.com/scummvm/scummvm/commit/082e7924606d351c7194120a67093d9bb8a5de70
Author: Christian Krause (chkr at plauener.de)
Date: 2020-12-08T19:26:25+01:00

Commit Message:
MATH: Refactor Line2d

- use standard linear equation Ax + Bx = C
- simplify most calculations
- remove default constructor
- remove unused function

Changed paths:
    math/line2d.cpp
    math/line2d.h


diff --git a/math/line2d.cpp b/math/line2d.cpp
index 9a89485b79..2c1665b784 100644
--- a/math/line2d.cpp
+++ b/math/line2d.cpp
@@ -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();
diff --git a/math/line2d.h b/math/line2d.h
index 5aa5a44a6b..310ecb871b 100644
--- a/math/line2d.h
+++ b/math/line2d.h
@@ -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:




More information about the Scummvm-git-logs mailing list