Skip to content

Commit 95a4983

Browse files
committed
Pass points by value
1 parent c1bfa26 commit 95a4983

7 files changed

Lines changed: 17 additions & 19 deletions

File tree

content/geometry/Point3D.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@
1212

1313
template<class T> struct Point3D {
1414
typedef Point3D P;
15-
typedef const P& R;
1615
T x, y, z;
1716
explicit Point3D(T x=0, T y=0, T z=0) : x(x), y(y), z(z) {}
18-
bool operator<(R p) const {
17+
bool operator<(P p) const {
1918
return tie(x, y, z) < tie(p.x, p.y, p.z); }
20-
bool operator==(R p) const {
19+
bool operator==(P p) const {
2120
return tie(x, y, z) == tie(p.x, p.y, p.z); }
22-
P operator+(R p) const { return P(x+p.x, y+p.y, z+p.z); }
23-
P operator-(R p) const { return P(x-p.x, y-p.y, z-p.z); }
21+
P operator+(P p) const { return P(x+p.x, y+p.y, z+p.z); }
22+
P operator-(P p) const { return P(x-p.x, y-p.y, z-p.z); }
2423
P operator*(T d) const { return P(x*d, y*d, z*d); }
2524
P operator/(T d) const { return P(x/d, y/d, z/d); }
26-
T dot(R p) const { return x*p.x + y*p.y + z*p.z; }
27-
P cross(R p) const {
25+
T dot(P p) const { return x*p.x + y*p.y + z*p.z; }
26+
P cross(P p) const {
2827
return P(y*p.z - z*p.y, z*p.x - x*p.z, x*p.y - y*p.x);
2928
}
3029
T dist2() const { return x*x + y*y + z*z; }

content/geometry/SegmentDistance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Returns the shortest distance between point p and the line segment from point s
2121
#include "Point.h"
2222

2323
typedef Point<double> P;
24-
double segDist(P& s, P& e, P& p) {
24+
double segDist(P s, P e, P p) {
2525
if (s==e) return (p-s).dist();
2626
auto d = (e-s).dist2(), t = min(d,max(.0,(p-s).dot(e-s)));
2727
return ((p-s)*d-(e-s)*t).dist()/d;

content/geometry/circumcircle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ The circumcirle of a triangle is the circle intersecting all three vertices. ccR
1818
#include "Point.h"
1919

2020
typedef Point<double> P;
21-
double ccRadius(const P& A, const P& B, const P& C) {
21+
double ccRadius(P A, P B, P C) {
2222
return (B-A).dist()*(C-B).dist()*(A-C).dist()/
2323
abs((B-A).cross(C-A))/2;
2424
}
25-
P ccCenter(const P& A, const P& B, const P& C) {
25+
P ccCenter(P A, P B, P C) {
2626
P b = C-A, c = B-A;
2727
return A + (b*c.dist2()-c*b.dist2()).perp()/b.cross(c)/2;
2828
}

content/geometry/kdTree.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ typedef long long T;
1313
typedef Point<T> P;
1414
const T INF = numeric_limits<T>::max();
1515

16-
bool on_x(const P& a, const P& b) { return a.x < b.x; }
17-
bool on_y(const P& a, const P& b) { return a.y < b.y; }
16+
bool on_x(P a, P b) { return a.x < b.x; }
17+
bool on_y(P a, P b) { return a.y < b.y; }
1818

1919
struct Node {
2020
P pt; // if this is a leaf, the single point in it
2121
T x0 = INF, x1 = -INF, y0 = INF, y1 = -INF; // bounds
2222
Node *first = 0, *second = 0;
2323

24-
T distance(const P& p) { // min squared distance to a point
24+
T distance(P p) { // min squared distance to a point
2525
T x = (p.x < x0 ? x0 : p.x > x1 ? x1 : p.x);
2626
T y = (p.y < y0 ? y0 : p.y > y1 ? y1 : p.y);
2727
return (P(x,y) - p).dist2();
@@ -48,7 +48,7 @@ struct KDTree {
4848
Node* root;
4949
KDTree(const vector<P>& vp) : root(new Node({all(vp)})) {}
5050

51-
pair<T, P> search(Node *node, const P& p) {
51+
pair<T, P> search(Node *node, P p) {
5252
if (!node->first) {
5353
// uncomment if we should not find the point itself:
5454
// if (p == node->pt) return {INF, P()};
@@ -68,7 +68,7 @@ struct KDTree {
6868

6969
// find nearest point to a point, and its squared distance
7070
// (requires an arbitrary operator< for Point)
71-
pair<T, P> nearest(const P& p) {
71+
pair<T, P> nearest(P p) {
7272
return search(root, p);
7373
}
7474
};

content/geometry/lineDistance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ Using Point3D will always give a non-negative distance. For Point3D, call .dist
2121
#include "Point.h"
2222

2323
template<class P>
24-
double lineDist(const P& a, const P& b, const P& p) {
24+
double lineDist(P a, P b, P p) {
2525
return (double)(b-a).cross(p-a)/(b-a).dist();
2626
}

content/geometry/linearTransformation.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
#include "Point.h"
2020

2121
typedef Point<double> P;
22-
P linearTransformation(const P& p0, const P& p1,
23-
const P& q0, const P& q1, const P& r) {
22+
P linearTransformation(P p0, P p1, P q0, P q1, P r) {
2423
P dp = p1-p0, dq = q1-q0, num(dp.cross(dq), dp.dot(dq));
2524
return q0 + P((r-p0).cross(num), (r-p0).dot(num))/dp.dist2();
2625
}

content/geometry/sideOf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ template<class P>
1919
int sideOf(P s, P e, P p) { return sgn(s.cross(e, p)); }
2020

2121
template<class P>
22-
int sideOf(const P& s, const P& e, const P& p, double eps) {
22+
int sideOf(P s, P e, P p, double eps) {
2323
auto a = (e-s).cross(p-s);
2424
double l = (e-s).dist()*eps;
2525
return (a > l) - (a < -l);

0 commit comments

Comments
 (0)