| 1 |
// -*- related-file-name: "../../liblcdf/point.cc" -*- |
| 2 |
#ifndef LCDF_POINT_HH |
| 3 |
#define LCDF_POINT_HH |
| 4 |
#include <math.h> |
| 5 |
|
| 6 |
struct Point { |
| 7 |
|
| 8 |
double x; |
| 9 |
double y; |
| 10 |
|
| 11 |
Point() { } |
| 12 |
Point(double xx, double yy) : x(xx), y(yy) { } |
| 13 |
// Point(const Point &) use compiler default |
| 14 |
Point(const Point &p, double dx, double dy) : x(p.x + dx), y(p.y + dy) { } |
| 15 |
// ~Point() use compiler default |
| 16 |
|
| 17 |
inline double squared_length() const noexcept; |
| 18 |
inline double length() const noexcept; |
| 19 |
inline double magnitude() const noexcept; |
| 20 |
static inline double distance(const Point &, const Point &) noexcept; |
| 21 |
static inline double dot(const Point &, const Point &) noexcept; |
| 22 |
static Point midpoint(const Point &, const Point &) noexcept; |
| 23 |
|
| 24 |
inline double angle() const noexcept; |
| 25 |
|
| 26 |
void shift(double dx, double dy) { x += dx; y += dy; } |
| 27 |
|
| 28 |
inline Point shifted(double dx, double dy) const noexcept; |
| 29 |
Point rotated(double) const noexcept; |
| 30 |
inline Point normal() const noexcept; |
| 31 |
|
| 32 |
bool on_line(const Point &, const Point &, double) const noexcept; |
| 33 |
bool on_segment(const Point &, const Point &, double) const noexcept; |
| 34 |
|
| 35 |
inline Point &operator+=(const Point &) noexcept; |
| 36 |
inline Point &operator-=(const Point &) noexcept; |
| 37 |
inline Point &operator*=(double) noexcept; |
| 38 |
inline Point &operator/=(double) noexcept; |
| 39 |
|
| 40 |
// Point operator+(Point, const Point &); |
| 41 |
// Point operator-(Point, const Point &); |
| 42 |
// Point operator*(Point, double); |
| 43 |
// Point operator/(Point, double); |
| 44 |
// Point operator-(const Point &); |
| 45 |
|
| 46 |
// bool operator==(const Point &, const Point &); |
| 47 |
// bool operator!=(const Point &, const Point &); |
| 48 |
|
| 49 |
}; |
| 50 |
|
| 51 |
inline double |
| 52 |
Point::squared_length() const noexcept |
| 53 |
{ |
| 54 |
return x*x + y*y; |
| 55 |
} |
| 56 |
|
| 57 |
inline double |
| 58 |
Point::length() const noexcept |
| 59 |
{ |
| 60 |
return sqrt(x*x + y*y); |
| 61 |
} |
| 62 |
|
| 63 |
inline double |
| 64 |
Point::magnitude() const noexcept |
| 65 |
{ |
| 66 |
return length(); |
| 67 |
} |
| 68 |
|
| 69 |
inline double |
| 70 |
Point::angle() const noexcept |
| 71 |
{ |
| 72 |
return atan2(y, x); |
| 73 |
} |
| 74 |
|
| 75 |
inline Point |
| 76 |
Point::shifted(double dx, double dy) const noexcept |
| 77 |
{ |
| 78 |
return Point(x + dx, y + dy); |
| 79 |
} |
| 80 |
|
| 81 |
inline Point |
| 82 |
Point::normal() const noexcept |
| 83 |
{ |
| 84 |
double l = length(); |
| 85 |
return (l ? Point(x/l, y/l) : *this); |
| 86 |
} |
| 87 |
|
| 88 |
inline Point & |
| 89 |
Point::operator+=(const Point &p) noexcept |
| 90 |
{ |
| 91 |
x += p.x; |
| 92 |
y += p.y; |
| 93 |
return *this; |
| 94 |
} |
| 95 |
|
| 96 |
inline Point & |
| 97 |
Point::operator-=(const Point &p) noexcept |
| 98 |
{ |
| 99 |
x -= p.x; |
| 100 |
y -= p.y; |
| 101 |
return *this; |
| 102 |
} |
| 103 |
|
| 104 |
inline Point & |
| 105 |
Point::operator*=(double d) noexcept |
| 106 |
{ |
| 107 |
x *= d; |
| 108 |
y *= d; |
| 109 |
return *this; |
| 110 |
} |
| 111 |
|
| 112 |
inline Point & |
| 113 |
Point::operator/=(double d) noexcept |
| 114 |
{ |
| 115 |
x /= d; |
| 116 |
y /= d; |
| 117 |
return *this; |
| 118 |
} |
| 119 |
|
| 120 |
inline bool |
| 121 |
operator==(const Point &a, const Point &b) noexcept |
| 122 |
{ |
| 123 |
return a.x == b.x && a.y == b.y; |
| 124 |
} |
| 125 |
|
| 126 |
inline bool |
| 127 |
operator!=(const Point &a, const Point &b) noexcept |
| 128 |
{ |
| 129 |
return a.x != b.x || a.y != b.y; |
| 130 |
} |
| 131 |
|
| 132 |
inline Point |
| 133 |
operator+(Point a, const Point &b) noexcept |
| 134 |
{ |
| 135 |
a += b; |
| 136 |
return a; |
| 137 |
} |
| 138 |
|
| 139 |
inline Point |
| 140 |
operator-(Point a, const Point &b) noexcept |
| 141 |
{ |
| 142 |
a -= b; |
| 143 |
return a; |
| 144 |
} |
| 145 |
|
| 146 |
inline Point |
| 147 |
operator-(const Point &a) noexcept |
| 148 |
{ |
| 149 |
return Point(-a.x, -a.y); |
| 150 |
} |
| 151 |
|
| 152 |
inline Point |
| 153 |
operator*(Point a, double scale) noexcept |
| 154 |
{ |
| 155 |
a *= scale; |
| 156 |
return a; |
| 157 |
} |
| 158 |
|
| 159 |
inline Point |
| 160 |
operator*(double scale, Point a) noexcept |
| 161 |
{ |
| 162 |
a *= scale; |
| 163 |
return a; |
| 164 |
} |
| 165 |
|
| 166 |
inline Point |
| 167 |
operator/(Point a, double scale) noexcept |
| 168 |
{ |
| 169 |
a /= scale; |
| 170 |
return a; |
| 171 |
} |
| 172 |
|
| 173 |
inline double |
| 174 |
Point::distance(const Point &a, const Point &b) noexcept |
| 175 |
{ |
| 176 |
return (a - b).length(); |
| 177 |
} |
| 178 |
|
| 179 |
inline double |
| 180 |
Point::dot(const Point &a, const Point &b) noexcept |
| 181 |
{ |
| 182 |
return a.x*b.x + a.y*b.y; |
| 183 |
} |
| 184 |
|
| 185 |
#endif |