qtgui: parent the agent injector/brain viewer correctly, and fix some onSelect functi...
[openc2e.git] / physics.h
blob9297f429a7ff3c6bb8af1bb216501d5c79c60e64
1 /*
2 * physics.h
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 #ifndef PHYSICS_H
21 #define PHYSICS_H 1
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..
29 #define Point MyPoint
30 #define Line MyLine
32 struct Point {
33 float x, y;
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) {
41 x = r.x;
42 y = r.y;
43 return *this;
47 enum linetype { NORMAL, HORIZONTAL, VERTICAL };
49 class Line {
50 protected:
51 FRIEND_SERIALIZE(Line);
52 Point start, end;
53 double x_icept, y_icept, slope;
54 linetype type;
55 public:
57 void dump() const;
59 Line() {
60 start = Point(0,0);
61 end = Point(1,1);
62 x_icept = y_icept = 0;
63 slope = 1;
64 type = NORMAL;
67 Line(const Line &l) {
68 start = l.start;
69 end = l.end;
70 x_icept = l.x_icept;
71 y_icept = l.y_icept;
72 slope = l.slope;
73 type = l.type;
76 Line(Point s, Point e);
78 Line &operator=(const Line &l) {
79 start = l.start;
80 end = l.end;
81 x_icept = l.x_icept;
82 y_icept = l.y_icept;
83 slope = l.slope;
84 type = l.type;
85 return *this;
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);
102 // TODO
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);
110 return is_x && is_y;
111 } else {
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);
120 if (type == NORMAL)
121 return Point(x, (x - start.x) * slope + start.y);
122 else
123 return Point(x, start.y);
126 Point pointAtY(double y) const
128 assert(type != HORIZONTAL);
129 if (type == NORMAL)
130 return Point((y - start.y) / slope + start.x, y);
131 else
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 {
140 if (start.y > end.y)
141 return y <= start.y && y >= end.y;
142 else
143 return y >= start.y && y <= end.y;
146 void sanity_check() const;
149 template <class T = double>
150 class Vector {
151 public:
152 T x, y;
153 Vector() {
154 x = y = 0;
157 Vector(T _x, T _y) {
158 x = _x;
159 y = _y;
162 Vector(const Point &s, const Point &e) {
163 x = e.x - s.x;
164 y = e.y - s.y;
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);
184 Point i;
185 if (!l.intersect(barrier, i))
186 return false;
187 residual = Vector(i, Point(start.x + x, start.y + y));
188 return true;
191 T getX() const { return x; }
192 T getY() const { return y; }
194 Vector operator*(T m) const {
195 return scale(m);
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));
215 template <class T>
216 Point operator+(const Vector<T> &v, const Point &p) {
217 return Point(v.x + p.x, v.y + p.y);
220 template <class T>
221 Point operator+(const Point &p, const Vector<T> &v) {
222 return v + p;
225 #endif
226 /* vim: set noet: */