Coverage Report

Created: 2018-09-25 14:53

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