Bug 1567650 [wpt PR 17950] - [ElementTiming] Replace responseEnd with loadTime, a...
[gecko.git] / gfx / thebes / gfxLineSegment.h
blobe3e83910b26284badfb322e13ee19c123920ec12
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef GFX_LINESEGMENT_H
7 #define GFX_LINESEGMENT_H
9 #include "gfxTypes.h"
10 #include "gfxPoint.h"
12 struct gfxLineSegment {
13 gfxLineSegment(const gfxPoint& aStart, const gfxPoint& aEnd)
14 : mStart(aStart), mEnd(aEnd) {}
16 bool PointsOnSameSide(const gfxPoint& aOne, const gfxPoint& aTwo) {
17 // Solve the equation
18 // y - mStart.y - ((mEnd.y - mStart.y)/(mEnd.x - mStart.x))(x - mStart.x)
19 // for both points
21 gfxFloat deltaY = (mEnd.y - mStart.y);
22 gfxFloat deltaX = (mEnd.x - mStart.x);
24 gfxFloat one = deltaX * (aOne.y - mStart.y) - deltaY * (aOne.x - mStart.x);
25 gfxFloat two = deltaX * (aTwo.y - mStart.y) - deltaY * (aTwo.x - mStart.x);
27 // If both results have the same sign, then we're on the correct side of the
28 // line. 0 (on the line) is always considered in.
30 if ((one >= 0 && two >= 0) || (one <= 0 && two <= 0)) return true;
31 return false;
34 /**
35 * Determines if two line segments intersect, and returns the intersection
36 * point in aIntersection if they do.
38 * Coincident lines are considered not intersecting as they don't have an
39 * intersection point.
41 bool Intersects(const gfxLineSegment& aOther, gfxPoint& aIntersection) {
42 gfxFloat denominator =
43 (aOther.mEnd.y - aOther.mStart.y) * (mEnd.x - mStart.x) -
44 (aOther.mEnd.x - aOther.mStart.x) * (mEnd.y - mStart.y);
46 // Parallel or coincident. We treat coincident as not intersecting since
47 // these lines are guaranteed to have corners that intersect instead.
48 if (!denominator) {
49 return false;
52 gfxFloat anumerator =
53 (aOther.mEnd.x - aOther.mStart.x) * (mStart.y - aOther.mStart.y) -
54 (aOther.mEnd.y - aOther.mStart.y) * (mStart.x - aOther.mStart.x);
56 gfxFloat bnumerator = (mEnd.x - mStart.x) * (mStart.y - aOther.mStart.y) -
57 (mEnd.y - mStart.y) * (mStart.x - aOther.mStart.x);
59 gfxFloat ua = anumerator / denominator;
60 gfxFloat ub = bnumerator / denominator;
62 if (ua <= 0.0 || ua >= 1.0 || ub <= 0.0 || ub >= 1.0) {
63 // Intersection is outside of the segment
64 return false;
67 aIntersection = mStart + (mEnd - mStart) * ua;
68 return true;
71 gfxPoint mStart;
72 gfxPoint mEnd;
75 #endif /* GFX_LINESEGMENT_H */