add support to SDLBackend for rendering 24bit data
[openc2e.git] / physics.cpp
blobb98bcad398b8402b321a15da2114a02eb280cfd9
1 /*
2 * physics.cpp
3 * openc2e
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.
20 #include "physics.h"
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 = ";
26 switch (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;
33 sanity_check();
36 Line::Line(Point s, Point e) {
37 if (s.x > e.x)
38 std::swap(s, e);
39 start = s;
40 end = e;
42 if (s.x == e.x) {
43 type = VERTICAL;
44 x_icept = s.x;
45 } else if (s.y == e.y) {
46 type = HORIZONTAL;
47 y_icept = s.y;
48 slope = 0;
49 } else {
50 type = NORMAL;
51 slope = (end.y - start.y) / (end.x - start.x);
52 /* y = mx + b
53 * b = y - mx
55 y_icept = start.y - slope * start.x;
56 /* 0 = mx + b
57 * x = -b/m
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;
67 return false;
68 // XXX: set where to something useful
69 if (l.type == VERTICAL) {
70 if (!(l.containsY(start.y) && containsX(l.start.x)))
71 return false;
72 where.x = l.start.x;
73 where.y = start.y;
74 return true;
76 if (l.type == NORMAL) {
77 /* mx + b = y
78 * mx = y - b
79 * x = (y - b) / m
81 double x = (start.y - l.y_icept) / l.slope;
82 if (l.containsX(x) && containsX(x)) {
83 where = Point(x, start.y);
84 return true;
86 else return false;
90 if (type == VERTICAL) {
91 if (l.type == VERTICAL)
92 //return l.start.x == start.x;
93 return false;
94 // XXX: set where to something useful
95 if (l.type == HORIZONTAL) {
96 if (!(l.containsX(start.x) && containsY(l.start.y)))
97 return false;
98 where.x = start.x;
99 where.y = l.start.y;
100 return true;
102 if (!l.containsX(start.x))
103 return false;
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);
113 double x, y;
115 if (slope == l.slope)
116 return false; // XXX handle parallel overlap sanely
118 /* y = m1 * x + b1
119 * y = m2 * x + b2
121 * m1 * x + b1 = m2 * x + b2
122 * Solving for x:
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)) {
130 where = Point(x,y);
131 return true;
133 return false;
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);
145 /* vim: set noet: */