Skip to content

Commit fa6334e

Browse files
committed
Changed sth
1 parent f7bdb79 commit fa6334e

1 file changed

Lines changed: 73 additions & 8 deletions

File tree

src/alfred/math/flat-geometry.hpp

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,81 @@
55
#include <cmath>
66
#include <iostream>
77

8+
// Recommended eps is 0.25 * given precision
9+
template <long double eps>
10+
struct Double {
11+
long double val;
12+
13+
constexpr Double(long double v = 0) : val(v) {}
14+
static constexpr int sgn(long double x) {
15+
return (x > eps) - (x < -eps);
16+
}
17+
constexpr long double val(void) const { return val; }
18+
constexpr operator long double(void) const { return val; }
19+
constexpr bool operator==(const Double &rhs) const { return sgn(val - rhs.val) == 0; }
20+
constexpr bool operator!=(const Double &rhs) const { return sgn(val - rhs.val) != 0; }
21+
constexpr bool operator<(const Double &rhs) const { return sgn(val - rhs.val) < 0; }
22+
constexpr bool operator>(const Double &rhs) const { return sgn(val - rhs.val) > 0; }
23+
constexpr bool operator<=(const Double &rhs) const { return sgn(val - rhs.val) <= 0; }
24+
constexpr bool operator>=(const Double &rhs) const { return sgn(val - rhs.val) >= 0; }
25+
constexpr Double operator+(void) const { return *this; }
26+
constexpr Double operator-(void) const { return Double(-val); }
27+
constexpr Double &operator+=(const Double &rhs) {
28+
val += rhs.val;
29+
return *this;
30+
}
31+
constexpr Double &operator-=(const Double &rhs) {
32+
val -= rhs.val;
33+
return *this;
34+
}
35+
constexpr Double &operator*=(const Double &rhs) {
36+
val *= rhs.val;
37+
return *this;
38+
}
39+
constexpr Double &operator/=(const Double &rhs) {
40+
val /= rhs.val;
41+
return *this;
42+
}
43+
friend constexpr Double operator+(Double l, const Double &r) { return l += r; }
44+
friend constexpr Double operator-(Double l, const Double &r) { return l -= r; }
45+
friend constexpr Double operator*(Double l, const Double &r) { return l *= r; }
46+
friend constexpr Double operator/(Double l, const Double &r) { return l /= r; }
47+
friend std::ostream &operator<<(std::ostream &os, const Double &x) { return os << x.val; }
48+
friend std::istream &operator>>(std::istream &is, Double &x) { return is >> x.val; }
49+
friend Double sqrt(const Double &x) { return Double(std::sqrt(x.val)); }
50+
friend Double abs(const Double &x) { return Double(std::abs(x.val)); }
51+
};
52+
853
template <class T>
954
struct Vec {
1055
T x, y;
1156
Vec(T _x = 0, T _y = 0) : x(_x), y(_y) {}
12-
inline T norm(void) const { return x * x + y * y; }
13-
inline Vec operator*(T &&d) const { return Vec(x * d, y * d); }
14-
inline Vec operator/(T &&d) const { return Vec(x / d, y / d); }
15-
inline Vec operator+(Vec &&v) const { return Vec(x + v.x, y + v.y); }
16-
inline Vec operator-(Vec &&v) const { return Vec(x - v.x, y - v.y); }
17-
friend std::ostream &operator<<(std::ostream &out, Vec &&P) {
57+
constexpr T norm(void) const { return x * x + y * y; }
58+
constexpr Vec operator+(void) const noexcept { return *this; }
59+
constexpr Vec operator-(void) const noexcept { return Vec(-x, -y); }
60+
constexpr Vec &operator+=(const Vec &rhs) noexcept {
61+
return x += rhs.x, y += rhs.y, *this;
62+
}
63+
constexpr Vec &operator-=(const Vec &rhs) noexcept {
64+
return x -= rhs.x, y -= rhs.y, *this;
65+
}
66+
constexpr Vec &operator*=(T d) noexcept {
67+
return x *= d, y *= d, *this;
68+
}
69+
constexpr Vec &operator/=(T d) noexcept {
70+
return x /= d, y /= d, *this;
71+
}
72+
friend constexpr Vec operator+(Vec l, const Vec &r) noexcept { return l += r; }
73+
friend constexpr Vec operator-(Vec l, const Vec &r) noexcept { return l -= r; }
74+
friend constexpr Vec operator*(Vec l, const Vec &r) noexcept { return l *= r; }
75+
friend constexpr Vec operator/(Vec l, const Vec &r) noexcept { return l /= r; }
76+
friend constexpr bool operator==(const Vec &l, const Vec &r) noexcept {
77+
return l.x == r.x && l.y == r.y;
78+
}
79+
friend constexpr bool operator!=(const Vec &l, const Vec &r) noexcept {
80+
return l.x != r.x || l.y != r.y;
81+
}
82+
friend std::ostream &operator<<(std::ostream &out, const Vec &P) {
1883
return out << "(" << P.x << ", " << P.y << ")";
1984
}
2085
friend std::istream &operator>>(std::istream &in, Vec &P) {
@@ -26,12 +91,12 @@ template <class T>
2691
using Point = Vec<T>;
2792

2893
template <class T>
29-
inline T dot(Vec<T> &&a, Vec<T> &&b) {
94+
inline T dot(const Vec<T> &a, const Vec<T> &b) {
3095
return a.x * b.x + a.y * b.y;
3196
}
3297

3398
template <class T>
34-
inline T cross(Vec<T> &&a, Vec<T> &&b) {
99+
inline T cross(const Vec<T> &a, const Vec<T> &b) {
35100
return a.x * b.y - a.y * b.x;
36101
}
37102

0 commit comments

Comments
 (0)