Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef GFX_LINESEGMENT_H |
michael@0 | 7 | #define GFX_LINESEGMENT_H |
michael@0 | 8 | |
michael@0 | 9 | #include "gfxTypes.h" |
michael@0 | 10 | #include "gfxPoint.h" |
michael@0 | 11 | |
michael@0 | 12 | struct gfxLineSegment { |
michael@0 | 13 | gfxLineSegment(const gfxPoint &aStart, const gfxPoint &aEnd) |
michael@0 | 14 | : mStart(aStart) |
michael@0 | 15 | , mEnd(aEnd) |
michael@0 | 16 | {} |
michael@0 | 17 | |
michael@0 | 18 | bool PointsOnSameSide(const gfxPoint& aOne, const gfxPoint& aTwo) |
michael@0 | 19 | { |
michael@0 | 20 | // Solve the equation y - mStart.y - ((mEnd.y - mStart.y)/(mEnd.x - mStart.x))(x - mStart.x) for both points |
michael@0 | 21 | |
michael@0 | 22 | gfxFloat deltaY = (mEnd.y - mStart.y); |
michael@0 | 23 | gfxFloat deltaX = (mEnd.x - mStart.x); |
michael@0 | 24 | |
michael@0 | 25 | gfxFloat one = deltaX * (aOne.y - mStart.y) - deltaY * (aOne.x - mStart.x); |
michael@0 | 26 | gfxFloat two = deltaX * (aTwo.y - mStart.y) - deltaY * (aTwo.x - mStart.x); |
michael@0 | 27 | |
michael@0 | 28 | // If both results have the same sign, then we're on the correct side of the line. |
michael@0 | 29 | // 0 (on the line) is always considered in. |
michael@0 | 30 | |
michael@0 | 31 | if ((one >= 0 && two >= 0) || (one <= 0 && two <= 0)) |
michael@0 | 32 | return true; |
michael@0 | 33 | return false; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * Determines if two line segments intersect, and returns the intersection |
michael@0 | 38 | * point in aIntersection if they do. |
michael@0 | 39 | * |
michael@0 | 40 | * Coincident lines are considered not intersecting as they don't have an |
michael@0 | 41 | * intersection point. |
michael@0 | 42 | */ |
michael@0 | 43 | bool Intersects(const gfxLineSegment& aOther, gfxPoint& aIntersection) |
michael@0 | 44 | { |
michael@0 | 45 | gfxFloat denominator = (aOther.mEnd.y - aOther.mStart.y) * (mEnd.x - mStart.x ) - |
michael@0 | 46 | (aOther.mEnd.x - aOther.mStart.x ) * (mEnd.y - mStart.y); |
michael@0 | 47 | |
michael@0 | 48 | // Parallel or coincident. We treat coincident as not intersecting since |
michael@0 | 49 | // these lines are guaranteed to have corners that intersect instead. |
michael@0 | 50 | if (!denominator) { |
michael@0 | 51 | return false; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | gfxFloat anumerator = (aOther.mEnd.x - aOther.mStart.x) * (mStart.y - aOther.mStart.y) - |
michael@0 | 55 | (aOther.mEnd.y - aOther.mStart.y) * (mStart.x - aOther.mStart.x); |
michael@0 | 56 | |
michael@0 | 57 | gfxFloat bnumerator = (mEnd.x - mStart.x) * (mStart.y - aOther.mStart.y) - |
michael@0 | 58 | (mEnd.y - mStart.y) * (mStart.x - aOther.mStart.x); |
michael@0 | 59 | |
michael@0 | 60 | gfxFloat ua = anumerator / denominator; |
michael@0 | 61 | gfxFloat ub = bnumerator / denominator; |
michael@0 | 62 | |
michael@0 | 63 | if (ua <= 0.0 || ua >= 1.0 || |
michael@0 | 64 | ub <= 0.0 || ub >= 1.0) { |
michael@0 | 65 | //Intersection is outside of the segment |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | aIntersection = mStart + (mEnd - mStart) * ua; |
michael@0 | 70 | return true; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | gfxPoint mStart; |
michael@0 | 74 | gfxPoint mEnd; |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | #endif /* GFX_LINESEGMENT_H */ |