line2.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <ionCore.h>
5 
6 
7 namespace ion
8 {
9 
12  template <typename T>
13  class line2
14  {
15 
16  public:
17 
20 
21  line2(vec2<T> const & start, vec2<T> const & end)
22  : Start(start), End(end)
23  {}
24 
26  {
27  return (Start + End) / 2;
28  }
29 
31  {
32  return End - Start;
33  }
34 
36  {
37  return (End - Start).Rotate90CW().GetNormalized();
38  }
39 
40  T Length() const
41  {
42  return Distance(Start, End);
43  }
44 
45  friend T Length(line2<T> const & Line)
46  {
47  return Distance(Line.Start, Line.End);
48  }
49 
50  vec2<T> const ClosestPointTo(vec2<T> const & Point) const
51  {
52  if (End == Start)
53  {
54  return LengthSq(Point - Start);
55  }
56 
57  float const t = Clamp(Dot(Point - Start, End - Start) / LengthSq(End - Start), 0.f, 1.f);
58  return Start + t * (End - Start);
59  }
60 
61  float GetDistanceFrom(vec2<T> const & Point) const
62  {
63  return Distance(Point, ClosestPointTo(Point));
64  }
65 
66  float GetDistanceSqFrom(vec2<T> const & Point) const
67  {
68  return DistanceSq(Point, ClosestPointTo(Point));
69  }
70 
71  bool IntersectsWith(line2<T> const & Other, vec2<T> & Intersection) const
72  {
73  // Implementation adapted from Paul Bourke's intersection code
74  // http://paulbourke.net/geometry/pointlineplane/
75  // http://paulbourke.net/geometry/pointlineplane/pdb.c
76 
77  T const denom = (Other.End.Y - Other.Start.Y) * (End.X - Start.X) - (Other.End.X - Other.Start.X) * (End.Y - Start.Y);
78  T const numera = (Other.End.X - Other.Start.X) * (Start.Y - Other.Start.Y) - (Other.End.Y - Other.Start.Y) * (Start.X - Other.Start.X);
79  T const numerb = (End.X - Start.X) * (Start.Y - Other.Start.Y) - (End.Y - Start.Y) * (Start.X - Other.Start.X);
80 
81  // Are the line coincident?
82  if (Equals(numera, (T) 0) && Equals(numerb, (T) 0) && Equals(denom, (T) 0))
83  {
84  Intersection = (Start + End) / 2;
85  return true;
86  }
87 
88  // Are the lines parallel?
89  if (Equals(denom, (T) 0))
90  {
91  return false;
92  }
93 
94  // Is the intersection along the the segments?
95  T const mua = numera / denom;
96  T const mub = numerb / denom;
97 
98  if (mua < 0 || mua > 1 || mub < 0 || mub > 1)
99  {
100  return false;
101  }
102 
103  Intersection = Start + mua * (End - Start);
104  return true;
105  }
106 
107  float GetDistanceFrom(line2<T> const & Other) const
108  {
109  vec2<T> Intersect;
110 
111  if (IntersectsWith(Other, Intersect))
112  {
113  return 0.f;
114  }
115 
116  return Min(GetDistanceFrom(Other.Start), GetDistanceFrom(Other.End), Other.GetDistanceFrom(Start), Other.GetDistanceFrom(End));
117  }
118 
119  float GetDistanceSqFrom(line2<T> const & Other) const
120  {
121  vec2<T> Intersect;
122 
123  if (IntersectsWith(Other, Intersect))
124  {
125  return 0.f;
126  }
127 
128  return Min(GetDistanceSqFrom(Other.Start), GetDistanceSqFrom(Other.End), Other.GetDistanceSqFrom(Start), Other.GetDistanceSqFrom(End));
129  }
130 
131  };
132 
136 
137 }
2D vector
Definition: vec2.h:17
float GetDistanceSqFrom(vec2< T > const &Point) const
Definition: line2.h:66
T Length() const
Definition: line2.h:40
bool IntersectsWith(line2< T > const &Other, vec2< T > &Intersection) const
Definition: line2.h:71
line2< double > line2d
Definition: line2.h:134
vec2< T > End
Definition: line2.h:19
float GetDistanceFrom(vec2< T > const &Point) const
Definition: line2.h:61
vec2< T > GetNormal() const
Definition: line2.h:35
vec2< T > Start
Definition: line2.h:18
vec2< T > const ClosestPointTo(vec2< T > const &Point) const
Definition: line2.h:50
line2< float > line2f
Definition: line2.h:133
T X
Definition: vec2.h:20
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
line2< int > line2i
Definition: line2.h:135
vec2< T > GetMiddle() const
Definition: line2.h:25
float GetDistanceFrom(line2< T > const &Other) const
Definition: line2.h:107
T Y
Definition: vec2.h:21
2D line segment
Definition: line2.h:13
T Clamp(T const v, T const min, T const max)
Definition: ionUtils.h:45
vec2< T > GetVector() const
Definition: line2.h:30
line2(vec2< T > const &start, vec2< T > const &end)
Definition: line2.h:21
float GetDistanceSqFrom(line2< T > const &Other) const
Definition: line2.h:119
friend T Length(line2< T > const &Line)
Definition: line2.h:45