5 * Created by Alyssa Milburn on Tue 08 Feb 2005.
6 * Copyright (c) 2005 Alyssa Milburn. All rights reserved.
7 * Copyright (c) 2005 Bryan Donlan. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
23 #include <iostream> // XXX debug
24 #include <cmath> // sqrt
25 #include <algorithm> // swap
26 #include "openc2e.h" // FRIEND_SERIALIZE
28 // OS X header files use Point and Line, so..
34 Point() { x
= y
= 0; }
35 Point(float _x
, float _y
) : x(_x
), y(_y
) {}
36 Point(const Point
&p
) : x(p
.x
), y(p
.y
) { }
38 bool operator==(const Point
&p
) { return x
== p
.x
&& y
== p
.y
; }
39 bool operator!=(const Point
&p
) { return !(*this == p
); }
40 Point
&operator=(const Point
&r
) {
47 enum linetype
{ NORMAL
, HORIZONTAL
, VERTICAL
};
51 FRIEND_SERIALIZE(Line
);
53 double x_icept
, y_icept
, slope
;
62 x_icept
= y_icept
= 0;
76 Line(Point s
, Point e
);
78 Line
&operator=(const Line
&l
) {
88 bool intersect(const Line
&l
, Point
&where
) const;
90 linetype
getType() const { return type
; }
91 double xIntercept() const { return x_icept
; }
92 double yIntercept() const { return y_icept
; }
93 double getSlope() const { return slope
; }
94 const Point
&getStart() const { return start
; }
95 const Point
&getEnd() const { return end
; }
97 // TODO: this code hasn't really been tested - fuzzie
98 bool containsPoint(Point p
) const {
99 if (type
== VERTICAL
) {
100 bool is_x
= fabs(start
.x
- p
.x
) < 1;
101 bool is_y
= containsY(p
.y
);
103 //bool is_v = (start.x > (p.x + 0.5)) && (start.x < (p.x - 0.5));
104 //bool is_h = (start.y > (p.y + 0.5)) && (start.y < (p.y - 0.5));
105 return (is_x
&& is_y
);
106 } else if (type
== HORIZONTAL
) {
107 bool is_y
= fabs(start
.y
- p
.y
) < 1;
108 bool is_x
= containsX(p
.x
);
112 Point point_on_line
= pointAtX(p
.x
);
113 return containsX(p
.x
) && fabs(point_on_line
.y
- p
.y
) < 1;
117 Point
pointAtX(double x
) const
119 assert(type
!= VERTICAL
);
121 return Point(x
, (x
- start
.x
) * slope
+ start
.y
);
123 return Point(x
, start
.y
);
126 Point
pointAtY(double y
) const
128 assert(type
!= HORIZONTAL
);
130 return Point((y
- start
.y
) / slope
+ start
.x
, y
);
132 return Point(start
.x
, y
);
135 bool containsX(double x
) const {
136 return x
>= start
.x
&& x
<= end
.x
;
139 bool containsY(double y
) const {
141 return y
<= start
.y
&& y
>= end
.y
;
143 return y
>= start
.y
&& y
<= end
.y
;
146 void sanity_check() const;
149 template <class T
= double>
162 Vector(const Point
&s
, const Point
&e
) {
167 T
getMagnitude() const { return sqrt(x
*x
+y
*y
); }
169 Line
extendFrom(const Point
&p
) const {
170 return Line(p
, Point(p
.x
+ x
, p
.y
+ y
));
173 Vector
scaleToMagnitude(T m
) const {
174 return Vector(x
/getMagnitude()*m
, y
/getMagnitude()*m
);
177 Vector
scale(T multiplier
) const {
178 return Vector(x
* multiplier
, y
* multiplier
);
181 bool extendIntersect(const Point
&start
, Line barrier
,
182 Vector
&residual
) const {
183 Line l
= extendFrom(start
);
185 if (!l
.intersect(barrier
, i
))
187 residual
= Vector(i
, Point(start
.x
+ x
, start
.y
+ y
));
191 T
getX() const { return x
; }
192 T
getY() const { return y
; }
194 Vector
operator*(T m
) const {
198 Vector
operator+(const Vector
&v
) const {
199 return Vector(x
+ v
.x
, y
+ v
.y
);
202 Vector
operator-(const Vector
&v
) const {
203 return Vector(x
- v
.x
, y
- v
.y
);
206 bool operator==(const Vector
&v
) const {
207 return (x
== v
.x
) && (y
== v
.y
);
210 static Vector
unitVector(T angle
) {
211 return Vector(cos(angle
), sin(angle
));
216 Point
operator+(const Vector
<T
> &v
, const Point
&p
) {
217 return Point(v
.x
+ p
.x
, v
.y
+ p
.y
);
221 Point
operator+(const Point
&p
, const Vector
<T
> &v
) {