Skip to content

Commit 19c70ac

Browse files
committed
Added option for long double in Halfplane tests
1 parent 4df6e0e commit 19c70ac

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

fuzz-tests/geometry/HalfPlane.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,57 @@ typedef Point<double> P;
1717
ostream &operator<<(ostream &os, P p) { return cout << "(" << p.x << "," << p.y << ")"; }
1818

1919
namespace sjtu {
20-
const double EPS = 1e-8;
21-
inline int sign(double a) { return a < -EPS ? -1 : a > EPS; }
20+
typedef double T;
21+
const T EPS = 1e-8;
22+
inline int sign(T a) { return a < -EPS ? -1 : a > EPS; }
2223
struct Point {
23-
double x, y;
24+
T x, y;
2425
Point() {}
25-
Point(double _x, double _y) : x(_x), y(_y) {}
26+
Point(T _x, T _y) : x(_x), y(_y) {}
2627
Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }
2728
Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }
28-
Point operator*(double d) const { return Point(x * d, y * d); }
29-
Point operator/(double d) const { return Point(x / d, y / d); }
29+
Point operator*(T d) const { return Point(x * d, y * d); }
30+
Point operator/(T d) const { return Point(x / d, y / d); }
3031
bool operator<(const Point &p) const {
3132
int c = sign(x - p.x);
3233
if (c)
3334
return c == -1;
3435
return sign(y - p.y) == -1;
3536
}
36-
double dot(const Point &p) const { return x * p.x + y * p.y; }
37-
double det(const Point &p) const { return x * p.y - y * p.x; }
38-
double alpha() const { return atan2(y, x); }
39-
double distTo(const Point &p) const {
40-
double dx = x - p.x, dy = y - p.y;
37+
T dot(const Point &p) const { return x * p.x + y * p.y; }
38+
T det(const Point &p) const { return x * p.y - y * p.x; }
39+
T alpha() const { return atan2(y, x); }
40+
T distTo(const Point &p) const {
41+
T dx = x - p.x, dy = y - p.y;
4142
return hypot(dx, dy);
4243
}
43-
double alphaTo(const Point &p) const {
44-
double dx = x - p.x, dy = y - p.y;
44+
T alphaTo(const Point &p) const {
45+
T dx = x - p.x, dy = y - p.y;
4546
return atan2(dy, dx);
4647
}
4748
// clockwise
48-
Point rotAlpha(const double &alpha, const Point &o = Point(0, 0)) const {
49-
double nx = cos(alpha) * (x - o.x) + sin(alpha) * (y - o.y);
50-
double ny = -sin(alpha) * (x - o.x) + cos(alpha) * (y - o.y);
49+
Point rotAlpha(const T &alpha, const Point &o = Point(0, 0)) const {
50+
T nx = cos(alpha) * (x - o.x) + sin(alpha) * (y - o.y);
51+
T ny = -sin(alpha) * (x - o.x) + cos(alpha) * (y - o.y);
5152
return Point(nx, ny) + o;
5253
}
5354
Point rot90() const { return Point(-y, x); }
5455
Point unit() { return *this / abs(); }
5556
void read() { scanf("%lf%lf", &x, &y); }
56-
double abs() { return hypot(x, y); }
57-
double abs2() { return x * x + y * y; }
57+
T abs() { return hypot(x, y); }
58+
T abs2() { return x * x + y * y; }
5859
void write() { cout << "(" << x << "," << y << ")" << endl; }
5960
};
6061
#define cross(p1, p2, p3) ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y))
6162
#define crossOp(p1, p2, p3) sign(cross(p1, p2, p3))
6263

6364
Point isSS(Point p1, Point p2, Point q1, Point q2) {
64-
double a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);
65+
T a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);
6566
return (p1 * a2 + p2 * a1) / (a1 + a2);
6667
}
6768
struct Border {
6869
Point p1, p2;
69-
double alpha;
70+
T alpha;
7071
void setAlpha() { alpha = atan2(p2.y - p1.y, p2.x - p1.x); }
7172
void read() {
7273
p1.read();
@@ -88,7 +89,7 @@ bool operator<(const Border &a, const Border &b) {
8889

8990
bool operator==(const Border &a, const Border &b) { return sign(a.alpha - b.alpha) == 0; }
9091

91-
void add(double x, double y, double nx, double ny) {
92+
void add(T x, T y, T nx, T ny) {
9293
border[n].p1 = Point(x, y);
9394
border[n].p2 = Point(nx, ny);
9495
border[n].setAlpha();
@@ -123,7 +124,7 @@ void convexIntersection() {
123124
++qh;
124125
}
125126

126-
double calcArea() {
127+
T calcArea() {
127128
static Point ps[MAX_N_BORDER];
128129
int cnt = 0;
129130

@@ -136,7 +137,7 @@ double calcArea() {
136137
ps[cnt++] = isBorder(que[i], que[next]);
137138
}
138139

139-
double area = 0;
140+
T area = 0;
140141
for (int i = 0; i < cnt; ++i) {
141142
area += ps[i].det(ps[(i + 1) % cnt]);
142143
}
@@ -145,7 +146,7 @@ double calcArea() {
145146
return area;
146147
}
147148

148-
double halfPlaneIntersection(vector<Line> cur) {
149+
T halfPlaneIntersection(vector<Line> cur) {
149150
n = 0;
150151
for (auto i: cur)
151152
add(i[0].x, i[0].y, i[1].x, i[1].y);
@@ -202,7 +203,7 @@ void testEmpty() {
202203
assert(sz(res) == 0);
203204
}
204205
void testRandom() {
205-
int lim = 10;
206+
int lim = 1e2;
206207
for (int i = 0; i < 1000; i++) {
207208
vector<Line> t;
208209
for (int i = 0; i < 6; i++) {
@@ -227,6 +228,9 @@ void testRandom() {
227228
assert(diff < EPS);
228229
}
229230
}
231+
void testConvex() {
232+
233+
}
230234

231235
int main() {
232236
test1();
@@ -235,7 +239,7 @@ int main() {
235239
testPoint();
236240
testEmpty();
237241
testRandom();
238-
// Failure case for MIT
242+
// Failure case for MIT's half plane code
239243
// vector<Line> t({{P(9, 8), P(9, 1)}, {P(3, 3), P(3, 5)}, {P(7, 6), P(0, 8)}});
240244
// addInf(t);
241245
// cout << polygonArea2(halfPlaneIntersection(t)) << endl;

fuzz-tests/utils/genConvex.h

Whitespace-only changes.

0 commit comments

Comments
 (0)