tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gfxLineSegment.h (2711B)


      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/. */
      5 
      6 #ifndef GFX_LINESEGMENT_H
      7 #define GFX_LINESEGMENT_H
      8 
      9 #include "gfxTypes.h"
     10 #include "gfxPoint.h"
     11 
     12 struct gfxLineSegment {
     13  gfxLineSegment() {}
     14  gfxLineSegment(const gfxPoint& aStart, const gfxPoint& aEnd)
     15      : mStart(aStart), mEnd(aEnd) {}
     16 
     17  bool PointsOnSameSide(const gfxPoint& aOne, const gfxPoint& aTwo) {
     18    // Solve the equation
     19    // y - mStart.y - ((mEnd.y - mStart.y)/(mEnd.x - mStart.x))(x - mStart.x)
     20    // for both points
     21 
     22    gfxFloat deltaY = (mEnd.y - mStart.y);
     23    gfxFloat deltaX = (mEnd.x - mStart.x);
     24 
     25    gfxFloat one = deltaX * (aOne.y - mStart.y) - deltaY * (aOne.x - mStart.x);
     26    gfxFloat two = deltaX * (aTwo.y - mStart.y) - deltaY * (aTwo.x - mStart.x);
     27 
     28    // If both results have the same sign, then we're on the correct side of the
     29    // line. 0 (on the line) is always considered in.
     30 
     31    if ((one >= 0 && two >= 0) || (one <= 0 && two <= 0)) return true;
     32    return false;
     33  }
     34 
     35  /**
     36   * Determines if two line segments intersect, and returns the intersection
     37   * point in aIntersection if they do.
     38   *
     39   * Coincident lines are considered not intersecting as they don't have an
     40   * intersection point.
     41   */
     42  bool Intersects(const gfxLineSegment& aOther, gfxPoint& aIntersection) {
     43    gfxFloat denominator =
     44        (aOther.mEnd.y - aOther.mStart.y).value * (mEnd.x - mStart.x).value -
     45        (aOther.mEnd.x - aOther.mStart.x).value * (mEnd.y - mStart.y).value;
     46 
     47    // Parallel or coincident. We treat coincident as not intersecting since
     48    // these lines are guaranteed to have corners that intersect instead.
     49    if (!denominator) {
     50      return false;
     51    }
     52 
     53    gfxFloat anumerator = (aOther.mEnd.x - aOther.mStart.x).value *
     54                              (mStart.y - aOther.mStart.y).value -
     55                          (aOther.mEnd.y - aOther.mStart.y).value *
     56                              (mStart.x - aOther.mStart.x).value;
     57 
     58    gfxFloat bnumerator =
     59        (mEnd.x - mStart.x).value * (mStart.y - aOther.mStart.y).value -
     60        (mEnd.y - mStart.y).value * (mStart.x - aOther.mStart.x).value;
     61 
     62    gfxFloat ua = anumerator / denominator;
     63    gfxFloat ub = bnumerator / denominator;
     64 
     65    if (ua <= 0.0 || ua >= 1.0 || ub <= 0.0 || ub >= 1.0) {
     66      // Intersection is outside of the segment
     67      return false;
     68    }
     69 
     70    aIntersection = mStart + (mEnd - mStart) * ua;
     71    return true;
     72  }
     73 
     74  gfxPoint mStart;
     75  gfxPoint mEnd;
     76 };
     77 
     78 #endif /* GFX_LINESEGMENT_H */