Commit 9181b04b authored by VAN TOLL Wouter's avatar VAN TOLL Wouter
Browse files

- Bugfix in isClockwise().

- Added more helper functions related to angles and point-to-line orientation.
parent 86c7358e
......@@ -59,7 +59,7 @@ public:
/// <summary>Normalizes this Vector2D to unit length, by dividing both x and y by the magnitude.</summary>
inline void normalize()
{
{
float mag = magnitude();
if (mag > 0)
{
......@@ -81,8 +81,8 @@ public:
/// <param name="other">Another vector.</param>
/// <returns>The dot product of the current Vector2D and 'other', i.e. x*other.x + y*other.y.</returns>
inline float dot(const Vector2D& other) const
{
return x_ * other.x_ + y_ * other.y_;
{
return x_ * other.x_ + y_ * other.y_;
}
/// <summary>Sets the x and y components of this Vector2D to the given values.</summary>
......@@ -102,7 +102,7 @@ public:
x_ += rhs.x(); y_ += rhs.y();
return *this;
}
/// <summary>Subtracts another Vector2D to the current Vector2D.</summary>
/// <param name="rhs">Another vector.</param>
/// <returns>A reference to the current Vector2D after the operation has been performed.</returns>
......@@ -115,8 +115,8 @@ public:
/// <summary>Checks and returns whether this Vector2D is the zero vector.</summary>
/// <returns>true if both x and y are 0; false otherwise.</returns>
inline bool isZero() const
{
return x_ == 0 && y_ == 0;
{
return x_ == 0 && y_ == 0;
}
};
......@@ -204,6 +204,12 @@ inline float cosAngle(const Vector2D& va, const Vector2D& vb)
}
inline bool isClockwise(const Vector2D &vector1, const Vector2D &vector2)
{
float cross = vector1.x() * vector2.y() - vector1.y() * vector2.x();
return cross < 0;
}
inline bool isCounterClockwise(const Vector2D &vector1, const Vector2D &vector2)
{
float cross = vector1.x() * vector2.y() - vector1.y() * vector2.x();
return cross > 0;
......@@ -217,11 +223,29 @@ inline float counterClockwiseAngle(const Vector2D& va, const Vector2D& vb)
return ang;
}
inline float clockwiseAngle(const Vector2D& va, const Vector2D& vb)
{
float ang = angle(va, vb);
if (isCounterClockwise(va, vb))
return -ang;
return ang;
}
inline bool isPointLeftOfLine(const Vector2D& p, const Vector2D& la, const Vector2D& lb)
{
return isClockwise(p - la, lb - la);
}
inline bool isPointRightOfLine(const Vector2D& p, const Vector2D& la, const Vector2D& lb)
{
return isCounterClockwise(p - la, lb - la);
}
#pragma endregion
template<typename T> T clamp(T v, T min, T max)
{
if (v < min)
if (v < min)
return min;
if (v > max)
return max;
......@@ -236,7 +260,7 @@ inline Vector2D clamp(const Vector2D& v, float maxLength)
return v.getnormalized() * maxLength;
}
inline bool getLineIntersection(const Vector2D& a, const Vector2D& b, const Vector2D& c, const Vector2D& d, Vector2D& result)
inline bool getLineIntersection(const Vector2D& a, const Vector2D& b, const Vector2D& c, const Vector2D& d, Vector2D& result)
{
// Line AB represented as a1x + b1y = c1
double a1 = b.y() - a.y();
......@@ -252,7 +276,7 @@ inline bool getLineIntersection(const Vector2D& a, const Vector2D& b, const Vect
if (fabs(determinant) < 0.00001) // The lines are parallel.
return false;
result = Vector2D(
(float)((b2*c1 - b1 * c2) / determinant),
(float)((a1*c2 - a2 * c1) / determinant)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment