Skip to content

Instantly share code, notes, and snippets.

@Journeyman1337
Created September 9, 2022 06:52
Show Gist options
  • Save Journeyman1337/e23d5c20b4ad5cdd9c70751d8fc272d8 to your computer and use it in GitHub Desktop.
Save Journeyman1337/e23d5c20b4ad5cdd9c70751d8fc272d8 to your computer and use it in GitHub Desktop.
/*
Copyright (c) 2022 Daniel Valcour
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef RLFW_MATH_GRID_CIRCLE2_HPP
#define RLFW_MATH_GRID_CIRCLE2_HPP
#include <rlfw/math/common/concepts.hpp>
#include <rlfw/math/grid/circle2_def.hpp>
#include <rlfw/math/grid/point2.hpp>
#include <rlfw/math/grid/segment2.hpp>
#include <rlfw/math/grid/rect2.hpp>
#include <rlfw/math/grid/distance.hpp>
#include <rlfw/math/common/round.hpp>
#include <optional>
template<rl::signed_integral I, rl::floating_point F>
constexpr rl::circle2<I, F>::circle2(const I center_x, const I center_y, const F radius) noexcept
: center_x(center_x)
, center_y(center_y)
, radius(radius)
{}
template<rl::signed_integral I, rl::floating_point F>
constexpr rl::point2<I> rl::circle2<I, F>::center() const noexcept
{
return rl::point2<I>(this->center_x, this->center_y);
}
template<rl::signed_integral I, rl::floating_point F>
constexpr I rl::circle2<I, F>::left_x() const noexcept
{
return static_cast<I>(static_cast<F>(this->center_x) - rl::round(this->radius));
}
template<rl::signed_integral I, rl::floating_point F>
constexpr I rl::circle2<I, F>::right_x() const noexcept
{
return static_cast<I>(static_cast<F>(this->center_x) + rl::round(this->radius));
}
template<rl::signed_integral I, rl::floating_point F>
constexpr I rl::circle2<I, F>::top_y() const noexcept
{
return static_cast<I>(static_cast<F>(this->center_y) - rl::round(this->radius));
}
template<rl::signed_integral I, rl::floating_point F>
constexpr I rl::circle2<I, F>::bottom_y() const noexcept
{
return static_cast<I>(static_cast<F>(this->center_y) + rl::round(this->radius));
}
template<rl::signed_integral I, rl::floating_point F>
constexpr rl::point2<I> rl::circle2<I, F>::left() const noexcept
{
return rl::point2<I>(this->left_x(), this->center_y);
}
template<rl::signed_integral I, rl::floating_point F>
constexpr rl::point2<I> rl::circle2<I, F>::right() const noexcept
{
return rl::point2<I>(this->right_x(), this->center_y);
}
template<rl::signed_integral I, rl::floating_point F>
constexpr rl::point2<I> rl::circle2<I, F>::top() const noexcept
{
return rl::point2<I>(this->center_x, this->top_y());
}
template<rl::signed_integral I, rl::floating_point F>
constexpr rl::point2<I> rl::circle2<I, F>::bottom() const noexcept
{
return rl::point2<I>(this->center_x, this->bottom_y());
}
template<rl::signed_integral I, rl::floating_point F>
constexpr I rl::circle2<I, F>::dimensions() const noexcept
{
return static_cast<I>(rl::round(this->diameter()));
}
template<rl::signed_integral I, rl::floating_point F>
constexpr F rl::circle2<I, F>::diameter() const noexcept
{
return this->radius * static_cast<F>(2);
}
template<rl::signed_integral I, rl::floating_point F>
constexpr std::optional<rl::rect2<I>> rl::circle2<I, F>::bounds() const noexcept
{
if (this->is_degenerate()) return std::nullopt;
return rl::rect2<I>(this->left_x(), this->top_y(), this->dimensions(), this->dimensions());
}
template<rl::signed_integral I, rl::floating_point F>
constexpr bool rl::circle2<I, F>::is_degenerate() const noexcept
{
return this->radius < static_cast<F>(0.5);
}
template<rl::signed_integral I, rl::floating_point F>
constexpr bool rl::circle2<I, F>::contains(const rl::point2<I>& other) const noexcept
{
return
!this->is_degenerate() &&
rl::distance<F>(this->center(), other) < this->radius;
}
template<rl::signed_integral I, rl::floating_point F>
constexpr bool rl::circle2<I, F>::contains(const rl::segment2<I>& other) const noexcept
{
return
!this->is_degenerate() &&
rl::distance<F>(this->center(), other.start()) <= this->radius &&
rl::distance<F>(this->center(), other.end()) <= this->radius;
}
template<rl::signed_integral I, rl::floating_point F>
constexpr bool rl::circle2<I, F>::contains(const rl::rect2<I>& other) const noexcept
{
return
!this->is_degenerate() &&
!other.is_degenerate() &&
rl::distance<F>(this->center(), other.top_left()) <= this->radius &&
rl::distance<F>(this->center(), other.top_right()) <= this->radius &&
rl::distance<F>(this->center(), other.bottom_left()) <= this->radius &&
rl::distance<F>(this->center(), other.bottom_right()) <= this->radius;
}
template<rl::signed_integral I, rl::floating_point F>
constexpr bool rl::circle2<I, F>::contains(const rl::circle2<I, F>& other) const noexcept
{
return
!this->is_degenerate() &&
!other.is_degenerate() &&
rl::distance<F>(this->center(), other.left()) <= this->radius &&
rl::distance<F>(this->center(), other.right()) <= this->radius &&
rl::distance<F>(this->center(), other.top()) <= this->radius &&
rl::distance<F>(this->center(), other.bottom()) <= this->radius;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment