| @@ -50,23 +50,20 @@ public: | |||||
| /** Creates a copy of another line. */ | /** Creates a copy of another line. */ | ||||
| Line (const Line& other) noexcept | Line (const Line& other) noexcept | ||||
| : start (other.start), | |||||
| end (other.end) | |||||
| : start (other.start), end (other.end) | |||||
| { | { | ||||
| } | } | ||||
| /** Creates a line based on the coordinates of its start and end points. */ | /** Creates a line based on the coordinates of its start and end points. */ | ||||
| Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept | Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept | ||||
| : start (startX, startY), | |||||
| end (endX, endY) | |||||
| : start (startX, startY), end (endX, endY) | |||||
| { | { | ||||
| } | } | ||||
| /** Creates a line from its start and end points. */ | /** Creates a line from its start and end points. */ | ||||
| Line (const Point<ValueType> startPoint, | |||||
| const Point<ValueType> endPoint) noexcept | |||||
| : start (startPoint), | |||||
| end (endPoint) | |||||
| Line (Point<ValueType> startPoint, | |||||
| Point<ValueType> endPoint) noexcept | |||||
| : start (startPoint), end (endPoint) | |||||
| { | { | ||||
| } | } | ||||
| @@ -113,7 +110,7 @@ public: | |||||
| void setEnd (const Point<ValueType> newEnd) noexcept { end = newEnd; } | void setEnd (const Point<ValueType> newEnd) noexcept { end = newEnd; } | ||||
| /** Returns a line that is the same as this one, but with the start and end reversed, */ | /** Returns a line that is the same as this one, but with the start and end reversed, */ | ||||
| const Line reversed() const noexcept { return Line (end, start); } | |||||
| Line reversed() const noexcept { return Line (end, start); } | |||||
| /** Applies an affine transform to the line's start and end points. */ | /** Applies an affine transform to the line's start and end points. */ | ||||
| void applyTransform (const AffineTransform& transform) noexcept | void applyTransform (const AffineTransform& transform) noexcept | ||||
| @@ -142,6 +139,17 @@ public: | |||||
| */ | */ | ||||
| typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); } | typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); } | ||||
| /** Creates a line from a start point, length and angle. | |||||
| This angle 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. | |||||
| */ | |||||
| static Line fromStartAndAngle (Point<ValueType> startPoint, ValueType length, ValueType angle) noexcept | |||||
| { | |||||
| return Line (startPoint, startPoint.getPointOnCircumference (length, angle)); | |||||
| } | |||||
| //============================================================================== | |||||
| /** Casts this line to float coordinates. */ | /** Casts this line to float coordinates. */ | ||||
| Line<float> toFloat() const noexcept { return Line<float> (start.toFloat(), end.toFloat()); } | Line<float> toFloat() const noexcept { return Line<float> (start.toFloat(), end.toFloat()); } | ||||
| @@ -150,10 +158,10 @@ public: | |||||
| //============================================================================== | //============================================================================== | ||||
| /** Compares two lines. */ | /** Compares two lines. */ | ||||
| bool operator== (const Line& other) const noexcept { return start == other.start && end == other.end; } | |||||
| bool operator== (Line other) const noexcept { return start == other.start && end == other.end; } | |||||
| /** Compares two lines. */ | /** Compares two lines. */ | ||||
| bool operator!= (const Line& other) const noexcept { return start != other.start || end != other.end; } | |||||
| bool operator!= (Line other) const noexcept { return start != other.start || end != other.end; } | |||||
| //============================================================================== | //============================================================================== | ||||
| /** Finds the intersection between two lines. | /** Finds the intersection between two lines. | ||||
| @@ -161,7 +169,7 @@ public: | |||||
| @param line the line to intersect with | @param line the line to intersect with | ||||
| @returns the point at which the lines intersect, even if this lies beyond the end of the lines | @returns the point at which the lines intersect, even if this lies beyond the end of the lines | ||||
| */ | */ | ||||
| Point<ValueType> getIntersection (const Line& line) const noexcept | |||||
| Point<ValueType> getIntersection (Line line) const noexcept | |||||
| { | { | ||||
| Point<ValueType> p; | Point<ValueType> p; | ||||
| findIntersection (start, end, line.start, line.end, p); | findIntersection (start, end, line.start, line.end, p); | ||||
| @@ -180,13 +188,13 @@ public: | |||||
| don't intersect, the intersection coordinates returned will still | don't intersect, the intersection coordinates returned will still | ||||
| be valid | be valid | ||||
| */ | */ | ||||
| bool intersects (const Line& line, Point<ValueType>& intersection) const noexcept | |||||
| bool intersects (Line line, Point<ValueType>& intersection) const noexcept | |||||
| { | { | ||||
| return findIntersection (start, end, line.start, line.end, intersection); | return findIntersection (start, end, line.start, line.end, intersection); | ||||
| } | } | ||||
| /** Returns true if this line intersects another. */ | /** Returns true if this line intersects another. */ | ||||
| bool intersects (const Line& other) const noexcept | |||||
| bool intersects (Line other) const noexcept | |||||
| { | { | ||||
| Point<ValueType> ignored; | Point<ValueType> ignored; | ||||
| return findIntersection (start, end, other.start, other.end, ignored); | return findIntersection (start, end, other.start, other.end, ignored); | ||||
| @@ -257,7 +265,7 @@ public: | |||||
| @returns the point's distance from the line | @returns the point's distance from the line | ||||
| @see getPositionAlongLineOfNearestPoint | @see getPositionAlongLineOfNearestPoint | ||||
| */ | */ | ||||
| ValueType getDistanceFromPoint (const Point<ValueType> targetPoint, | |||||
| ValueType getDistanceFromPoint (Point<ValueType> targetPoint, | |||||
| Point<ValueType>& pointOnLine) const noexcept | Point<ValueType>& pointOnLine) const noexcept | ||||
| { | { | ||||
| const Point<ValueType> delta (end - start); | const Point<ValueType> delta (end - start); | ||||
| @@ -298,7 +306,7 @@ public: | |||||
| turn this number into a position, use getPointAlongLineProportionally(). | turn this number into a position, use getPointAlongLineProportionally(). | ||||
| @see getDistanceFromPoint, getPointAlongLineProportionally | @see getDistanceFromPoint, getPointAlongLineProportionally | ||||
| */ | */ | ||||
| ValueType findNearestProportionalPositionTo (const Point<ValueType> point) const noexcept | |||||
| ValueType findNearestProportionalPositionTo (Point<ValueType> point) const noexcept | |||||
| { | { | ||||
| const Point<ValueType> delta (end - start); | const Point<ValueType> delta (end - start); | ||||
| const double length = delta.x * delta.x + delta.y * delta.y; | const double length = delta.x * delta.x + delta.y * delta.y; | ||||
| @@ -312,7 +320,7 @@ public: | |||||
| /** Finds the point on this line which is nearest to a given point. | /** Finds the point on this line which is nearest to a given point. | ||||
| @see getDistanceFromPoint, findNearestProportionalPositionTo | @see getDistanceFromPoint, findNearestProportionalPositionTo | ||||
| */ | */ | ||||
| Point<ValueType> findNearestPointTo (const Point<ValueType> point) const noexcept | |||||
| Point<ValueType> findNearestPointTo (Point<ValueType> point) const noexcept | |||||
| { | { | ||||
| return getPointAlongLineProportionally (findNearestProportionalPositionTo (point)); | return getPointAlongLineProportionally (findNearestProportionalPositionTo (point)); | ||||
| } | } | ||||
| @@ -323,7 +331,7 @@ public: | |||||
| coordinate of this line at the given x (assuming the line extends infinitely | coordinate of this line at the given x (assuming the line extends infinitely | ||||
| in both directions). | in both directions). | ||||
| */ | */ | ||||
| bool isPointAbove (const Point<ValueType> point) const noexcept | |||||
| bool isPointAbove (Point<ValueType> point) const noexcept | |||||
| { | { | ||||
| return start.x != end.x | return start.x != end.x | ||||
| && point.y < ((end.y - start.y) | && point.y < ((end.y - start.y) | ||||
| @@ -380,19 +388,22 @@ private: | |||||
| intersection = p1.withX (p3.x + along * d2.x); | intersection = p1.withX (p3.x + along * d2.x); | ||||
| return along >= 0 && along <= static_cast<ValueType> (1); | return along >= 0 && along <= static_cast<ValueType> (1); | ||||
| } | } | ||||
| else if (d2.y == 0 && d1.y != 0) | |||||
| if (d2.y == 0 && d1.y != 0) | |||||
| { | { | ||||
| const ValueType along = (p3.y - p1.y) / d1.y; | const ValueType along = (p3.y - p1.y) / d1.y; | ||||
| intersection = p3.withX (p1.x + along * d1.x); | intersection = p3.withX (p1.x + along * d1.x); | ||||
| return along >= 0 && along <= static_cast<ValueType> (1); | return along >= 0 && along <= static_cast<ValueType> (1); | ||||
| } | } | ||||
| else if (d1.x == 0 && d2.x != 0) | |||||
| if (d1.x == 0 && d2.x != 0) | |||||
| { | { | ||||
| const ValueType along = (p1.x - p3.x) / d2.x; | const ValueType along = (p1.x - p3.x) / d2.x; | ||||
| intersection = p1.withY (p3.y + along * d2.y); | intersection = p1.withY (p3.y + along * d2.y); | ||||
| return along >= 0 && along <= static_cast<ValueType> (1); | return along >= 0 && along <= static_cast<ValueType> (1); | ||||
| } | } | ||||
| else if (d2.x == 0 && d1.x != 0) | |||||
| if (d2.x == 0 && d1.x != 0) | |||||
| { | { | ||||
| const ValueType along = (p3.x - p1.x) / d1.x; | const ValueType along = (p3.x - p1.x) / d1.x; | ||||
| intersection = p3.withY (p1.y + along * d1.y); | intersection = p3.withY (p1.y + along * d1.y); | ||||