1 #include "LineSegment.h"
2 #include "BoundingBox.h"
3 
4 namespace jvgs
5 {
6     namespace math
7     {
LineSegment(const Vector2D & start,const Vector2D & end)8         LineSegment::LineSegment(const Vector2D &start, const Vector2D &end)
9         {
10             this->start = start;
11             this->end = end;
12             boundingBox = 0;
13         }
14 
~LineSegment()15         LineSegment::~LineSegment()
16         {
17             if(boundingBox)
18                 delete boundingBox;
19         }
20 
getStart() const21         const Vector2D &LineSegment::getStart() const
22         {
23             return start;
24         }
25 
getEnd() const26         const Vector2D &LineSegment::getEnd() const
27         {
28             return end;
29         }
30 
getLine() const31         Line LineSegment::getLine() const
32         {
33             return Line(start, end - start);
34         }
35 
getLength() const36         float LineSegment::getLength() const
37         {
38             return start.getDistance(end);
39         }
40 
getPoint(float t) const41         Vector2D LineSegment::getPoint(float t) const
42         {
43             return start * (1.0f - t) + end * t;
44         }
45 
getBoundingBox()46         BoundingBox *LineSegment::getBoundingBox()
47         {
48             if(!boundingBox) {
49                 /* Calculate the segment bounding box x. */
50                 float minX, maxX, minY, maxY;
51                 if(start.getX() < end.getX()) {
52                     minX = start.getX();
53                     maxX = end.getX();
54                 } else {
55                     minX = end.getX();
56                     maxX = start.getX();
57                 }
58 
59                 /* Calculate the segment bounding box x. */
60                 if(start.getY() < end.getY()) {
61                     minY = start.getY();
62                     maxY = end.getY();
63                 } else {
64                     minY = end.getY();
65                     maxY = start.getY();
66                 }
67 
68                 boundingBox = new BoundingBox(Vector2D(minX, minY),
69                         Vector2D(maxX, maxY));
70             }
71 
72             return boundingBox;
73         }
74     }
75 }
76