d2/align, d2/image_rw: Revise migration-related messages to use pre-processor warnings.
[Ale.git] / d2 / point.h
blob10314ff84c170b71bfa097d80e055cdb53a762be
1 // Copyright 2002 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __d2point_h__
22 #define __d2point_h__
25 * Structure to describe a point
28 class point {
29 private:
30 ale_pos x[2];
32 public:
33 point() {
36 point(ale_pos x0, ale_pos x1) {
37 x[0] = x0;
38 x[1] = x1;
41 const ale_pos &operator[](unsigned int i) const {
42 #if 0
44 * This may be expensive.
46 assert (i < 2);
47 #endif
49 return x[i];
52 ale_pos &operator[](unsigned int i) {
53 #if 0
55 * This may be expensive.
57 assert (i < 2);
58 #endif
60 return x[i];
63 point operator+(point p) const {
64 return point(p[0] + x[0], p[1] + x[1]);
67 point operator-(point p) const {
68 return point(x[0] - p[0], x[1] - p[1]);
71 point operator-() const {
72 return point(-x[0], -x[1]);
75 point operator+=(point p) {
76 (*this) = (*this) + p;
78 return *this;
81 point operator-=(point p) {
82 (*this) = (*this) - p;
84 return *this;
87 point mult(ale_pos d) const {
88 return point(x[0] * d, x[1] * d);
91 point operator*(point p) const {
93 * element-wise multiplication
95 return point(x[0] * p[0], x[1] * p[1]);
98 point operator *=(ale_pos d) {
99 (*this) = mult(d);
100 return *this;
103 point operator/(ale_pos d) const {
104 return point(x[0] / d, x[1] / d);
107 ale_pos normsq() const {
108 return x[0] * x[0] + x[1] * x[1];
111 ale_pos norm() const {
112 return sqrt(normsq());
115 ale_pos absmaxnorm() const {
116 ale_pos a = fabs(x[0]);
117 ale_pos b = fabs(x[1]);
119 return (a > b) ? a : b;
122 ale_pos lengthtosq(point p) const {
123 point diff = operator-(p);
125 return diff[0] * diff[0] + diff[1] * diff[1];
127 ale_pos lengthto(point p) const {
128 return sqrt(lengthtosq(p));
131 ale_pos dproduct(point p) const {
132 return (x[0] * p[0] + x[1] * p[1]);
135 ale_pos anglebetw(point p, point q) {
137 * by the law of cosines, the cosine is equal to:
139 * (lengthtosq(p) + lengthtosq(q) - p.lengthtosq(q))
140 * / (2 * lengthto(p) * lengthto(q))
143 ale_pos to_p = lengthtosq(p);
144 ale_pos to_q = lengthtosq(q);
146 ale_pos cos_of = (double) (to_p + to_q - p.lengthtosq(q))
147 / (2 * sqrt(to_p) * sqrt(to_q));
149 return acos(cos_of);
152 static point posinf() {
153 ale_pos a = +1;
154 ale_pos z = +0;
156 a = a / z;
158 assert (isinf(a) && a > 0);
160 return point(a, a);
163 static point neginf() {
164 point n = -posinf();
166 assert (isinf(n[0]) && n[0] < 0);
168 return n;
171 void accumulate_max(point p) {
172 for (int d = 0; d < 2; d++)
173 if (p[d] > x[d])
174 x[d] = p[d];
177 void accumulate_min(point p) {
178 for (int d = 0; d < 2; d++)
179 if (p[d] < x[d])
180 x[d] = p[d];
183 static point undefined() {
184 double a = 0;
186 point p(0, 0);
188 return p / a;
191 int defined() const {
192 return (!isnan(x[0])
193 && !isnan(x[1]));
196 int finite() const {
197 return (::finite(x[0])
198 && ::finite(x[1]));
201 static int defined(const point &p) {
202 return p.defined();
206 inline point operator*(const point &p, double d) {
207 return p.mult(d);
209 inline point operator*(double d, const point &p) {
210 return p.mult(d);
212 inline point operator*(float d, const point &p) {
213 return p.mult(d);
215 #endif