| 1 |
// -*- related-file-name: "../include/lcdf/point.hh" -*- |
| 2 |
|
| 3 |
/* point.{cc,hh} -- 2D points |
| 4 |
* |
| 5 |
* Copyright (c) 1998-2023 Eddie Kohler |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the Free |
| 9 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 10 |
* any later version. This program is distributed in the hope that it will be |
| 11 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 13 |
* Public License for more details. |
| 14 |
*/ |
| 15 |
|
| 16 |
#ifdef HAVE_CONFIG_H |
| 17 |
# include <config.h> |
| 18 |
#endif |
| 19 |
#include <lcdf/point.hh> |
| 20 |
|
| 21 |
Point |
| 22 |
Point::rotated(double rotation) const noexcept |
| 23 |
{ |
| 24 |
double r = length(); |
| 25 |
double theta = angle() + rotation; |
| 26 |
return Point(r * cos(theta), r * sin(theta)); |
| 27 |
} |
| 28 |
|
| 29 |
Point |
| 30 |
Point::midpoint(const Point &a, const Point &b) noexcept |
| 31 |
{ |
| 32 |
return Point((a.x + b.x)/2, (a.y + b.y)/2); |
| 33 |
} |
| 34 |
|
| 35 |
bool |
| 36 |
Point::on_line(const Point &a, const Point &b, double tolerance) const noexcept |
| 37 |
{ |
| 38 |
Point c = b - a; |
| 39 |
double d = c.x * (y - a.y) - c.y * (x - a.x); |
| 40 |
return (d * d <= tolerance * tolerance * c.squared_length()); |
| 41 |
} |
| 42 |
|
| 43 |
bool |
| 44 |
Point::on_segment(const Point &a, const Point &b, double t) const noexcept |
| 45 |
{ |
| 46 |
double tt; |
| 47 |
Point c = b - a; |
| 48 |
if (fabs(c.x) > fabs(c.y)) |
| 49 |
tt = (x - a.x) / c.x; |
| 50 |
else if (c.y) |
| 51 |
tt = (y - a.y) / c.y; |
| 52 |
else |
| 53 |
tt = 0; |
| 54 |
if (tt < 0 || tt > 1) |
| 55 |
return 0; |
| 56 |
return on_line(a, b, t); |
| 57 |
} |