d2::image::get_*bl(): Always return a single value.
[Ale.git] / d2 / point.h
blob3b829f25fca6b2e7a4b75ed9af1f28fb32858d85
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 assert (i < 2);
44 return x[i];
47 ale_pos &operator[](unsigned int i) {
48 assert (i < 2);
50 return x[i];
53 point operator+(point p) const {
54 return point(p[0] + x[0], p[1] + x[1]);
57 point operator-(point p) const {
58 return point(x[0] - p[0], x[1] - p[1]);
61 point operator-() const {
62 return point(-x[0], -x[1]);
65 point operator+=(point p) {
66 (*this) = (*this) + p;
68 return *this;
71 point operator-=(point p) {
72 (*this) = (*this) - p;
74 return *this;
77 point mult(ale_real d) const {
78 return point(x[0] * d, x[1] * d);
81 point operator*(point p) const {
83 * element-wise multiplication
85 return point(x[0] * p[0], x[1] * p[1]);
88 point operator *=(ale_real d) {
89 (*this) = mult(d);
90 return *this;
93 point operator/(ale_real d) const {
94 return point(x[0] / d, x[1] / d);
97 ale_pos normsq() const {
98 return x[0] * x[0] + x[1] * x[1];
101 ale_pos norm() const {
102 return sqrt(normsq());
105 ale_pos absmaxnorm() const {
106 ale_pos a = fabs(x[0]);
107 ale_pos b = fabs(x[1]);
109 return (a > b) ? a : b;
112 ale_pos lengthtosq(point p) const {
113 point diff = operator-(p);
115 return diff[0] * diff[0] + diff[1] * diff[1];
117 ale_pos lengthto(point p) const {
118 return sqrt(lengthtosq(p));
121 ale_pos dproduct(point p) const {
122 return (x[0] * p[0] + x[1] * p[1]);
125 ale_pos anglebetw(point p, point q) {
127 * by the law of cosines, the cosine is equal to:
129 * (lengthtosq(p) + lengthtosq(q) - p.lengthtosq(q))
130 * / (2 * lengthto(p) * lengthto(q))
133 ale_pos to_p = lengthtosq(p);
134 ale_pos to_q = lengthtosq(q);
136 ale_pos cos_of = (to_p + to_q - p.lengthtosq(q))
137 / (2 * sqrt(to_p) * sqrt(to_q));
139 return acos(cos_of);
142 static point posinf() {
143 double a = +1;
144 double z = +0;
146 a = a / z;
148 assert (isinf(a) && a > 0);
150 return point(a, a);
153 static point neginf() {
154 point n = -posinf();
156 assert (isinf(n[0]) && n[0] < 0);
158 return n;
161 void accumulate_max(point p) {
162 for (int d = 0; d < 2; d++)
163 if (p[d] > x[d])
164 x[d] = p[d];
167 void accumulate_min(point p) {
168 for (int d = 0; d < 2; d++)
169 if (p[d] < x[d])
170 x[d] = p[d];
173 static point undefined() {
174 double a = 0;
176 point p(0, 0);
178 return p / a;
181 int defined() const {
182 return (!isnan(x[0])
183 && !isnan(x[1]));
186 int finite() const {
187 return (::finite(x[0])
188 && ::finite(x[1]));
191 static int defined(const point &p) {
192 return p.defined();
196 inline point operator*(const point &p, double d) {
197 return p.mult(d);
199 inline point operator*(double d, const point &p) {
200 return p.mult(d);
202 inline point operator*(float d, const point &p) {
203 return p.mult(d);
205 #endif