1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Straightened out some confusion with the Point::getAngleToPoint and Line::getAngle methods, where the description didn't actually match the behaviour. The behaviour and descriptions of these functions have now changed, but should now match up!

This commit is contained in:
jules 2011-12-09 12:41:38 +00:00
parent 39f12d3cc6
commit 907cfc5221
3 changed files with 38 additions and 23 deletions

View file

@ -518,6 +518,13 @@ namespace TypeHelpers
*/
#define PARAMETER_TYPE(a) typename TypeHelpers::ParameterType<a>::type
#endif
/** These templates are designed to take a type, and if it's a double, they return a double
type; for anything else, they return a float type.
*/
template <typename Type> struct SmallestFloatType { typedef float type; };
template <> struct SmallestFloatType <double> { typedef double type; };
}

View file

@ -137,10 +137,10 @@ public:
/** Returns the line's angle.
This value is the number of radians clockwise from the 3 o'clock direction,
This value is the number of radians clockwise from the 12 o'clock direction,
where the line's start point is considered to be at the centre.
*/
ValueType getAngle() const noexcept { return start.getAngleToPoint (end); }
typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); }
//==============================================================================
/** Compares two lines. */
@ -214,8 +214,8 @@ public:
if (length <= 0)
return start;
return Point<ValueType> (start.x + (ValueType) ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
start.y + (ValueType) ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length));
return Point<ValueType> (start.x + static_cast <ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
start.y + static_cast <ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length));
}
/** Returns the location of the point which is a given distance along this line
@ -257,7 +257,7 @@ public:
if (prop >= 0 && prop <= 1.0)
{
pointOnLine = start + delta * (ValueType) prop;
pointOnLine = start + delta * static_cast <ValueType> (prop);
return targetPoint.getDistanceFrom (pointOnLine);
}
}
@ -291,9 +291,9 @@ public:
const double length = delta.x * delta.x + delta.y * delta.y;
return length <= 0 ? 0
: jlimit ((ValueType) 0, (ValueType) 1,
(ValueType) (((point.x - start.x) * delta.x
+ (point.y - start.y) * delta.y) / length));
: jlimit (ValueType(), static_cast <ValueType> (1),
static_cast <ValueType> ((((point.x - start.x) * delta.x
+ (point.y - start.y) * delta.y) / length)));
}
/** Finds the point on this line which is nearest to a given point.
@ -365,40 +365,40 @@ private:
{
const ValueType along = (p1.y - p3.y) / d2.y;
intersection = p1.withX (p3.x + along * d2.x);
return along >= 0 && along <= (ValueType) 1;
return along >= 0 && along <= static_cast <ValueType> (1);
}
else if (d2.y == 0 && d1.y != 0)
{
const ValueType along = (p3.y - p1.y) / d1.y;
intersection = p3.withX (p1.x + along * d1.x);
return along >= 0 && along <= (ValueType) 1;
return along >= 0 && along <= static_cast <ValueType> (1);
}
else if (d1.x == 0 && d2.x != 0)
{
const ValueType along = (p1.x - p3.x) / d2.x;
intersection = p1.withY (p3.y + along * d2.y);
return along >= 0 && along <= (ValueType) 1;
return along >= 0 && along <= static_cast <ValueType> (1);
}
else if (d2.x == 0 && d1.x != 0)
{
const ValueType along = (p3.x - p1.x) / d1.x;
intersection = p3.withY (p1.y + along * d1.y);
return along >= 0 && along <= (ValueType) 1;
return along >= 0 && along <= static_cast <ValueType> (1);
}
}
intersection = (p2 + p3) / (ValueType) 2;
intersection = (p2 + p3) / static_cast <ValueType> (2);
return false;
}
const ValueType along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
intersection = p1 + d1 * along1;
if (along1 < 0 || along1 > (ValueType) 1)
if (along1 < 0 || along1 > static_cast <ValueType> (1))
return false;
const ValueType along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
return along2 >= 0 && along2 <= (ValueType) 1;
return along2 >= 0 && along2 <= static_cast <ValueType> (1);
}
};

View file

@ -125,26 +125,33 @@ public:
/** Returns the straight-line distance between this point and another one. */
ValueType getDistanceFrom (const Point& other) const noexcept { return juce_hypot (x - other.x, y - other.y); }
/** This type will be double if the Point's type is double, otherwise it will be float. */
typedef typename TypeHelpers::SmallestFloatType<ValueType>::type FloatType;
/** Returns the angle from this point to another one.
The return value is the number of radians clockwise from the 3 o'clock direction,
The return value is the number of radians clockwise from the 12 o'clock direction,
where this point is the centre and the other point is on the circumference.
*/
ValueType getAngleToPoint (const Point& other) const noexcept { return (ValueType) std::atan2 (other.x - x, other.y - y); }
FloatType getAngleToPoint (const Point& other) const noexcept
{ return static_cast<FloatType> (std::atan2 (other.x - x, y - other.y)); }
/** Taking this point to be the centre of a circle, this returns a point on its circumference.
@param radius the radius of the circle.
@param angle the angle of the point, in radians clockwise from the 12 o'clock position.
*/
Point getPointOnCircumference (const float radius, const float angle) const noexcept { return Point<float> (x + radius * std::sin (angle),
y - radius * std::cos (angle)); }
Point<FloatType> getPointOnCircumference (const float radius, const float angle) const noexcept
{ return Point<FloatType> (static_cast <FloatType> (x + radius * std::sin (angle)),
static_cast <FloatType> (y - radius * std::cos (angle))); }
/** Taking this point to be the centre of an ellipse, this returns a point on its circumference.
@param radiusX the horizontal radius of the circle.
@param radiusY the vertical radius of the circle.
@param angle the angle of the point, in radians clockwise from the 12 o'clock position.
*/
Point getPointOnCircumference (const float radiusX, const float radiusY, const float angle) const noexcept { return Point<float> (x + radiusX * std::sin (angle),
y - radiusY * std::cos (angle)); }
Point<FloatType> getPointOnCircumference (const float radiusX, const float radiusY, const float angle) const noexcept
{ return Point<FloatType> (static_cast <FloatType> (x + radiusX * std::sin (angle)),
static_cast <FloatType> (y - radiusY * std::cos (angle))); }
/** Uses a transform to change the point's co-ordinates.
This will only compile if ValueType = float!
@ -153,8 +160,9 @@ public:
void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); }
/** Returns the position of this point, if it is transformed by a given AffineTransform. */
Point transformedBy (const AffineTransform& transform) const noexcept { return Point (transform.mat00 * x + transform.mat01 * y + transform.mat02,
transform.mat10 * x + transform.mat11 * y + transform.mat12); }
Point transformedBy (const AffineTransform& transform) const noexcept
{ return Point (transform.mat00 * x + transform.mat01 * y + transform.mat02,
transform.mat10 * x + transform.mat11 * y + transform.mat12); }
/** Casts this point to a Point<int> object. */
Point<int> toInt() const noexcept { return Point<int> (static_cast <int> (x), static_cast<int> (y)); }