1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/thebes/gfxLineSegment.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,77 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef GFX_LINESEGMENT_H 1.10 +#define GFX_LINESEGMENT_H 1.11 + 1.12 +#include "gfxTypes.h" 1.13 +#include "gfxPoint.h" 1.14 + 1.15 +struct gfxLineSegment { 1.16 + gfxLineSegment(const gfxPoint &aStart, const gfxPoint &aEnd) 1.17 + : mStart(aStart) 1.18 + , mEnd(aEnd) 1.19 + {} 1.20 + 1.21 + bool PointsOnSameSide(const gfxPoint& aOne, const gfxPoint& aTwo) 1.22 + { 1.23 + // Solve the equation y - mStart.y - ((mEnd.y - mStart.y)/(mEnd.x - mStart.x))(x - mStart.x) for both points 1.24 + 1.25 + gfxFloat deltaY = (mEnd.y - mStart.y); 1.26 + gfxFloat deltaX = (mEnd.x - mStart.x); 1.27 + 1.28 + gfxFloat one = deltaX * (aOne.y - mStart.y) - deltaY * (aOne.x - mStart.x); 1.29 + gfxFloat two = deltaX * (aTwo.y - mStart.y) - deltaY * (aTwo.x - mStart.x); 1.30 + 1.31 + // If both results have the same sign, then we're on the correct side of the line. 1.32 + // 0 (on the line) is always considered in. 1.33 + 1.34 + if ((one >= 0 && two >= 0) || (one <= 0 && two <= 0)) 1.35 + return true; 1.36 + return false; 1.37 + } 1.38 + 1.39 + /** 1.40 + * Determines if two line segments intersect, and returns the intersection 1.41 + * point in aIntersection if they do. 1.42 + * 1.43 + * Coincident lines are considered not intersecting as they don't have an 1.44 + * intersection point. 1.45 + */ 1.46 + bool Intersects(const gfxLineSegment& aOther, gfxPoint& aIntersection) 1.47 + { 1.48 + gfxFloat denominator = (aOther.mEnd.y - aOther.mStart.y) * (mEnd.x - mStart.x ) - 1.49 + (aOther.mEnd.x - aOther.mStart.x ) * (mEnd.y - mStart.y); 1.50 + 1.51 + // Parallel or coincident. We treat coincident as not intersecting since 1.52 + // these lines are guaranteed to have corners that intersect instead. 1.53 + if (!denominator) { 1.54 + return false; 1.55 + } 1.56 + 1.57 + gfxFloat anumerator = (aOther.mEnd.x - aOther.mStart.x) * (mStart.y - aOther.mStart.y) - 1.58 + (aOther.mEnd.y - aOther.mStart.y) * (mStart.x - aOther.mStart.x); 1.59 + 1.60 + gfxFloat bnumerator = (mEnd.x - mStart.x) * (mStart.y - aOther.mStart.y) - 1.61 + (mEnd.y - mStart.y) * (mStart.x - aOther.mStart.x); 1.62 + 1.63 + gfxFloat ua = anumerator / denominator; 1.64 + gfxFloat ub = bnumerator / denominator; 1.65 + 1.66 + if (ua <= 0.0 || ua >= 1.0 || 1.67 + ub <= 0.0 || ub >= 1.0) { 1.68 + //Intersection is outside of the segment 1.69 + return false; 1.70 + } 1.71 + 1.72 + aIntersection = mStart + (mEnd - mStart) * ua; 1.73 + return true; 1.74 + } 1.75 + 1.76 + gfxPoint mStart; 1.77 + gfxPoint mEnd; 1.78 +}; 1.79 + 1.80 +#endif /* GFX_LINESEGMENT_H */