| 1 |
// |
| 2 |
// AutoCoast: Automatic Coastline scenery generator for Flight Simulator |
| 3 |
// |
| 4 |
// Copyright (c) 2004, Takuya Murakami. All rights reserved. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions |
| 8 |
// are met: |
| 9 |
// |
| 10 |
// 1. Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
// notice, this list of conditions and the following disclaimer in the |
| 15 |
// documentation and/or other materials provided with the distribution. |
| 16 |
// |
| 17 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
// ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
| 21 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 |
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 |
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
// vector.hpp : 2D vector class |
| 30 |
|
| 31 |
#include <math.h> |
| 32 |
#include "vector.hpp" |
| 33 |
|
| 34 |
// set vector between two gpc_vertex. |
| 35 |
void Vector::set(gpc_vertex *f, gpc_vertex *t) |
| 36 |
{ |
| 37 |
if (t) { |
| 38 |
x = t->x - f->x; |
| 39 |
y = t->y - f->y; |
| 40 |
} else { |
| 41 |
x = f->x; |
| 42 |
y = f->y; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
Vector Vector::operator+ (const Vector &v) const |
| 47 |
{ |
| 48 |
Vector vv(x + v.x, y + v.y); |
| 49 |
return vv; |
| 50 |
} |
| 51 |
|
| 52 |
Vector Vector::operator- (const Vector &v) const |
| 53 |
{ |
| 54 |
Vector vv(x - v.x, y - v.y); |
| 55 |
return vv; |
| 56 |
} |
| 57 |
|
| 58 |
Vector Vector::operator* (double m) const |
| 59 |
{ |
| 60 |
Vector vv(x * m, y * m); |
| 61 |
return vv; |
| 62 |
} |
| 63 |
|
| 64 |
Vector Vector::operator/ (double m) const |
| 65 |
{ |
| 66 |
Vector vv(x / m, y / m); |
| 67 |
return vv; |
| 68 |
} |
| 69 |
|
| 70 |
// Normalize vector |
| 71 |
Vector Vector::normalize(void) const |
| 72 |
{ |
| 73 |
double sz = sqrt(x * x + y * y); |
| 74 |
Vector vv(x / sz, y / sz); |
| 75 |
return vv; |
| 76 |
} |
| 77 |
|
| 78 |
// calculate innter product |
| 79 |
double Vector::InnerProduct(const Vector &v) const |
| 80 |
{ |
| 81 |
return x * v.x + y * v.y; |
| 82 |
} |
| 83 |
|
| 84 |
// calculate outer product |
| 85 |
double Vector::OuterProduct(const Vector &v) const |
| 86 |
{ |
| 87 |
return x * v.y - y * v.x; |
| 88 |
} |
| 89 |
|
| 90 |
// calculate angle between two vectors. |
| 91 |
double Vector::Angle(const Vector &target) const |
| 92 |
{ |
| 93 |
Vector v1 = normalize(); |
| 94 |
Vector v2 = target.normalize(); |
| 95 |
|
| 96 |
double inp = v1.InnerProduct(v2); |
| 97 |
double deg = acos(inp); |
| 98 |
|
| 99 |
double oup = v1.OuterProduct(v2); |
| 100 |
if (oup < 0) { |
| 101 |
deg = -deg; |
| 102 |
} |
| 103 |
|
| 104 |
return deg; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|