1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Oracle Corporation.
18 : * Portions created by the Initial Developer are Copyright (C) 2005
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Matt Woodrow <mwoodrow@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef GFX_LINESEGMENT_H
39 : #define GFX_LINESEGMENT_H
40 :
41 : #include "gfxTypes.h"
42 : #include "gfxPoint.h"
43 :
44 : struct THEBES_API gfxLineSegment {
45 0 : gfxLineSegment(const gfxPoint &aStart, const gfxPoint &aEnd)
46 : : mStart(aStart)
47 0 : , mEnd(aEnd)
48 0 : {}
49 :
50 0 : bool PointsOnSameSide(const gfxPoint& aOne, const gfxPoint& aTwo)
51 : {
52 : // Solve the equation y - mStart.y - ((mEnd.y - mStart.y)/(mEnd.x - mStart.x))(x - mStart.x) for both points
53 :
54 0 : gfxFloat deltaY = (mEnd.y - mStart.y);
55 0 : gfxFloat deltaX = (mEnd.x - mStart.x);
56 :
57 0 : gfxFloat one = deltaX * (aOne.y - mStart.y) - deltaY * (aOne.x - mStart.x);
58 0 : gfxFloat two = deltaX * (aTwo.y - mStart.y) - deltaY * (aTwo.x - mStart.x);
59 :
60 : // If both results have the same sign, then we're on the correct side of the line.
61 : // 0 (on the line) is always considered in.
62 :
63 0 : if ((one >= 0 && two >= 0) || (one <= 0 && two <= 0))
64 0 : return true;
65 0 : return false;
66 : }
67 :
68 : /**
69 : * Determines if two line segments intersect, and returns the intersection
70 : * point in aIntersection if they do.
71 : *
72 : * Coincident lines are considered not intersecting as they don't have an
73 : * intersection point.
74 : */
75 0 : bool Intersects(const gfxLineSegment& aOther, gfxPoint& aIntersection)
76 : {
77 : gfxFloat denominator = (aOther.mEnd.y - aOther.mStart.y) * (mEnd.x - mStart.x ) -
78 0 : (aOther.mEnd.x - aOther.mStart.x ) * (mEnd.y - mStart.y);
79 :
80 : // Parallel or coincident. We treat coincident as not intersecting since
81 : // these lines are guaranteed to have corners that intersect instead.
82 0 : if (!denominator) {
83 0 : return false;
84 : }
85 :
86 : gfxFloat anumerator = (aOther.mEnd.x - aOther.mStart.x) * (mStart.y - aOther.mStart.y) -
87 0 : (aOther.mEnd.y - aOther.mStart.y) * (mStart.x - aOther.mStart.x);
88 :
89 : gfxFloat bnumerator = (mEnd.x - mStart.x) * (mStart.y - aOther.mStart.y) -
90 0 : (mEnd.y - mStart.y) * (mStart.x - aOther.mStart.x);
91 :
92 0 : gfxFloat ua = anumerator / denominator;
93 0 : gfxFloat ub = bnumerator / denominator;
94 :
95 0 : if (ua <= 0.0 || ua >= 1.0 ||
96 : ub <= 0.0 || ub >= 1.0) {
97 : //Intersection is outside of the segment
98 0 : return false;
99 : }
100 :
101 0 : aIntersection = mStart + (mEnd - mStart) * ua;
102 0 : return true;
103 : }
104 :
105 : gfxPoint mStart;
106 : gfxPoint mEnd;
107 : };
108 :
109 : #endif /* GFX_LINESEGMENT_H */
|