8 namespace ColliderBit {
13 inline double addInQuad(
const double& a,
const double&
b) {
return sqrt(a*a + b*b); }
34 P2() : _x(0.), _y(0.) { }
36 P2(
double x,
double y) : _x(x), _y(y) { }
46 P2&
setx(
double x) { _x = x;
return *
this; }
47 P2&
sety(
double y) { _y = y;
return *
this; }
48 P2&
setxy(
double x,
double y) { _x = x; _y = y;
return *
this; }
83 ss <<
"(" << p2.
getx() <<
", " << p2.
gety() <<
")";
109 void init(
double x1,
double y1,
double x2,
double y2,
double extendFrac=-1.) {
110 P2 rawpt1, rawpt2, extendEnds;
111 if (x1 > x2 or (x1 == x2 and y1 > y2)) {
112 rawpt2.
setxy(x1, y1);
113 rawpt1.
setxy(x2, y2);
115 rawpt1.
setxy(x1, y1);
116 rawpt2.
setxy(x2, y2);
118 if(extendFrac > 0.) {
119 extendEnds = (rawpt2 - rawpt1) * extendFrac;
120 _p2 = rawpt2 + extendEnds;
121 _p1 = rawpt1 - extendEnds;
130 void init(
const P2& p1,
const P2& p2,
double extendFrac=-1.) {
136 init(0., 0., 0., 0.);
140 LineSegment(
double x1,
double y1,
double x2,
double y2,
double extendFrac=-1.) {
141 init(x1, y1, x2, y2, extendFrac);
146 init(p1, p2, extendFrac);
174 return std::numeric_limits<double>::infinity();
179 double m()
const {
return slope(); }
184 return std::numeric_limits<double>::infinity();
186 return _p1.
gety() - m() * _p1.
getx();
189 double b()
const {
return intercept(); }
192 double abs()
const {
return _diff.
abs(); }
194 double r()
const {
return abs(); }
202 P2 result(std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
203 double xintersect, yintersect;
206 if (slope() == other.
slope())
210 if (slope() == std::numeric_limits<double>::infinity()) {
211 xintersect = _p1.
getx();
212 yintersect = other.
m() * xintersect + other.
b();
214 and yintersect >= _p1.
gety() and yintersect <= _p2.
gety())
215 result.
setxy(xintersect, yintersect);
216 }
else if (other.
slope() == std::numeric_limits<double>::infinity()) {
218 yintersect = m() * xintersect +
b();
219 if (xintersect >= _p1.
getx() and xintersect <= _p2.
getx()
221 result.
setxy(xintersect, yintersect);
223 xintersect = (other.
b() -
b()) / (m() - other.
m());
224 yintersect = m() * xintersect +
b();
225 if (xintersect >= _p1.
getx() and xintersect <= _p2.
getx()
227 result.
setxy(xintersect, yintersect);
238 std::stringstream ss;
double r() const
Alias for abs.
P2 & operator*=(double a)
P2 & operator+=(const P2 &other)
double b() const
Alias for intercept.
P2 operator-(const P2 &a, const P2 &b)
LineSegment(const LineSegment &other)
Copy constructor.
LineSegment(const P2 &p1, const P2 &p2, double extendFrac=0.)
Point constructor.
double addInQuad(const double &a, const double &b)
Add two numbers in quadrature.
std::ostream & operator<<(std::ostream &os, const Cutflow &cf)
Print a Cutflow to a stream.
P2 operator+(const P2 &a, const P2 &b)
P2 & operator/=(double a)
P2 operator*(const P2 &a, double f)
double m() const
Alias for slope.
P2(double x, double y)
Coordinate constructor.
LineSegment()
Default constructor.
P2 & operator-=(const P2 &other)
A simple container for a point on an xy plane.
double intercept() const
Get the y-intercept of the full line which contains this segment.
void init(double x1, double y1, double x2, double y2, double extendFrac=-1.)
Coordinate initializer / recycler.
void init(const P2 &p1, const P2 &p2, double extendFrac=-1.)
Point initializer / recycler.
LineSegment(double x1, double y1, double x2, double y2, double extendFrac=-1.)
Coordinate constructor.
std::string to_str(const P2 &p2)
Make a string representation of the vector.
double slope() const
Get the slope of the line segment.
P2 & setxy(double x, double y)
double r() const
Alias for abs.
A simple container for a line segment on an xy plane.
P2 intersectsAt(const LineSegment &other) const
Determine if this intersects with other and where.
double abs() const
Get the length of the vector from the origin to this point.
P2 & operator=(const P2 &other)
Copy assignment operator.
double abs() const
Get the length of the vector from the origin to this point.
P2 operator/(const P2 &a, double f)
TODO: see if we can use this one:
P2(const P2 &other)
Copy constructor.