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.
22 void Line::dump() const {
23 std::cout
<< "pst = (" << start
.x
<< "," << start
.y
<< ") end=(" << end
.x
<< "," << end
.y
<< ")" << std::endl
;
24 std::cout
<< "xi = " << x_icept
<< " yi = " << y_icept
<< " m=" << slope
<< std::endl
;
25 std::cout
<< "type = ";
27 case NORMAL
: std::cout
<< "NORMAL"; break;
28 case HORIZONTAL
: std::cout
<< "HORIZ"; break;
29 case VERTICAL
: std::cout
<< "VERT"; break;
30 default: std::cout
<< "?? (" << type
<< ")"; break;
32 std::cout
<< std::endl
;
36 Line::Line(Point s
, Point e
) {
45 } else if (s
.y
== e
.y
) {
51 slope
= (end
.y
- start
.y
) / (end
.x
- start
.x
);
55 y_icept
= start
.y
- slope
* start
.x
;
59 x_icept
= -y_icept
/slope
;
63 bool Line::intersect(const Line
&l
, Point
&where
) const {
64 if (type
== HORIZONTAL
) {
65 if (l
.type
== HORIZONTAL
)
66 //return l.start.y == start.y;
68 // XXX: set where to something useful
69 if (l
.type
== VERTICAL
) {
70 if (!(l
.containsY(start
.y
) && containsX(l
.start
.x
)))
76 if (l
.type
== NORMAL
) {
81 double x
= (start
.y
- l
.y_icept
) / l
.slope
;
82 if (l
.containsX(x
) && containsX(x
)) {
83 where
= Point(x
, start
.y
);
90 if (type
== VERTICAL
) {
91 if (l
.type
== VERTICAL
)
92 //return l.start.x == start.x;
94 // XXX: set where to something useful
95 if (l
.type
== HORIZONTAL
) {
96 if (!(l
.containsX(start
.x
) && containsY(l
.start
.y
)))
102 if (!l
.containsX(start
.x
))
104 where
= l
.pointAtX(start
.x
);
105 return containsY(where
.y
);
108 if (l
.type
!= NORMAL
)
109 return l
.intersect(*this, where
);
111 assert(l
.type
== NORMAL
&& type
== NORMAL
);
115 if (slope
== l
.slope
)
116 return false; // XXX handle parallel overlap sanely
121 * m1 * x + b1 = m2 * x + b2
123 * b1 - b2 = (m2 - m1) x
124 * x = (b1 - b2) / (m2 - m1)
126 x
= (y_icept
- l
.y_icept
) / (l
.slope
- slope
);
127 y
= slope
* x
+ y_icept
;
129 if (containsX(x
) && l
.containsX(x
)) {
136 void Line::sanity_check() const {
137 if (type
== NORMAL
) {
138 double xp
= pointAtY(yIntercept()).x
;
139 double yp
= pointAtX(xIntercept()).y
;
140 assert(fabs(xp
) < 1);
141 assert(fabs(yp
) < 1);