Skip to content

Commit

Permalink
Fix possibly wrong generated circle
Browse files Browse the repository at this point in the history
The previous implementation could generate a circle with a final point
very close to the first one, or not, depending on double-precision
rouding imprecision. This new version makes sure that the last generated
point is always the same (non-explicitely closed polygon)
  • Loading branch information
wawanbreton committed May 9, 2024
1 parent dbc1c55 commit e58b257
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/utils/polygonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,10 @@ class PolygonUtils
* This creates a regular polygon that is supposed to approximate a circle.
* \param mid The center of the circle.
* \param radius The radius of the circle.
* \param a_step The angle between segments of the circle.
* \param step_angle The angle between segments of the circle.
* \return A new Polygon containing the circle.
*/
static Polygon makeCircle(const Point2LL mid, const coord_t radius, const AngleRadians a_step = std::numbers::pi / 8);
static Polygon makeCircle(const Point2LL mid, const coord_t radius, const AngleRadians step_angle = std::numbers::pi / 8);

/*!
* Create a "wheel" shape.
Expand Down
8 changes: 5 additions & 3 deletions src/utils/polygonUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1431,12 +1431,14 @@ double PolygonUtils::relativeHammingDistance(const Shape& poly_a, const Shape& p
return hamming_distance / total_area;
}

Polygon PolygonUtils::makeCircle(const Point2LL mid, const coord_t radius, const AngleRadians a_step)
Polygon PolygonUtils::makeCircle(const Point2LL mid, const coord_t radius, const AngleRadians step_angle)
{
Polygon circle;
for (double a = 0; a < 2 * std::numbers::pi; a += a_step)
const size_t steps = (2 * std::numbers::pi) / step_angle;
for (size_t step = 0; step < steps; ++step)
{
circle.emplace_back(mid + Point2LL(radius * cos(a), radius * sin(a)));
const double angle = step * step_angle;
circle.emplace_back(mid + Point2LL(radius * cos(angle), radius * sin(angle)));
}
return circle;
}
Expand Down

0 comments on commit e58b257

Please sign in to comment.