doc: Make table of contents output more flexible. This re-introduces the indentation...
[Ale.git] / d2 / point.h
blob1a6df3b19822df01e3ec0632b5e43e9f3af9ec91
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 2 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[](int i) const {
42 assert (i >= 0);
43 assert (i < 2);
45 return x[i];
48 ale_pos &operator[](int i) {
49 assert (i >= 0);
50 assert (i < 2);
52 return x[i];
55 point operator+(point p) const {
56 return point(p[0] + x[0], p[1] + x[1]);
59 point operator-(point p) const {
60 return point(x[0] - p[0], x[1] - p[1]);
63 point operator-() const {
64 return point(-x[0], -x[1]);
67 point operator+=(point p) {
68 (*this) = (*this) + p;
70 return *this;
73 point operator-=(point p) {
74 (*this) = (*this) - p;
76 return *this;
79 point mult(ale_real d) const {
80 return point(x[0] * d, x[1] * d);
83 point operator*(point p) const {
85 * element-wise multiplication
87 return point(x[0] * p[0], x[1] * p[1]);
90 point operator *=(ale_real d) {
91 (*this) = mult(d);
92 return *this;
95 point operator/(ale_real d) const {
96 return point(x[0] / d, x[1] / d);
99 ale_pos normsq() const {
100 return x[0] * x[0] + x[1] * x[1];
103 ale_pos norm() const {
104 return sqrt(normsq());
107 ale_pos absmaxnorm() const {
108 ale_pos a = fabs(x[0]);
109 ale_pos b = fabs(x[1]);
111 return (a > b) ? a : b;
114 ale_pos lengthtosq(point p) const {
115 point diff = operator-(p);
117 return diff[0] * diff[0] + diff[1] * diff[1];
119 ale_pos lengthto(point p) const {
120 return sqrt(lengthtosq(p));
123 ale_pos dproduct(point p) const {
124 return (x[0] * p[0] + x[1] * p[1]);
127 ale_pos anglebetw(point p, point q) {
129 * by the law of cosines, the cosine is equal to:
131 * (lengthtosq(p) + lengthtosq(q) - p.lengthtosq(q))
132 * / (2 * lengthto(p) * lengthto(q))
135 ale_pos to_p = lengthtosq(p);
136 ale_pos to_q = lengthtosq(q);
138 ale_pos cos_of = (to_p + to_q - p.lengthtosq(q))
139 / (2 * sqrt(to_p) * sqrt(to_q));
141 return acos(cos_of);
144 static point posinf() {
145 double a = +1;
146 double z = +0;
148 a = a / z;
150 assert (isinf(a) == +1);
152 return point(a, a);
155 static point neginf() {
156 point n = -posinf();
158 assert (isinf(n[0]) == -1);
160 return n;
163 void accumulate_max(point p) {
164 for (int d = 0; d < 2; d++)
165 if (p[d] > x[d])
166 x[d] = p[d];
169 void accumulate_min(point p) {
170 for (int d = 0; d < 2; d++)
171 if (p[d] < x[d])
172 x[d] = p[d];
175 static point undefined() {
176 double a = 0;
178 point p(0, 0);
180 return p / a;
183 int defined() const {
184 return (!isnan(x[0])
185 && !isnan(x[1]));
188 int finite() const {
189 return (::finite(x[0])
190 && ::finite(x[1]));
193 static int defined(const point &p) {
194 return p.defined();
199 inline point operator*(const point &p, double d) {
200 return p.mult(d);
202 inline point operator*(double d, const point &p) {
203 return p.mult(d);
205 inline point operator*(float d, const point &p) {
206 return p.mult(d);
208 #endif