RingRacers/src/math/line_segment.hpp
James R 37f2384229 Add srb2::math, fixed-point, vector, line and slope formula classes
- srb2::math::Fixed
  - Operator overloads for FixedMul and FixedDiv
  - Implicit conversion between fixed_t, Fixed and
    floating-point types
- srb2::math::Vec2
  - Template to any type
  - Operator overloads for arithmetic operations
  - Convertible between different types
- srb2::math::LineSegment
  - Template to any type
  - Holds two Vec2 instances
  - Sorting methods and vertical/horizontal test
- srb2::math::LineEquation
  - Slope formula from LineSegment
  - y method to find y from x
  - Intersect algorithm
  - Fixed-point specialization to avoid overflows
- srb2::math::LineEquationX
  - Inherits LineEquation
  - x method to find x from y
2023-11-10 00:03:06 -08:00

43 lines
1.1 KiB
C++

// DR. ROBOTNIK'S RING RACERS
//-----------------------------------------------------------------------------
// Copyright (C) 2023 by James Robert Roman
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
#ifndef math_line_segment_hpp
#define math_line_segment_hpp
#include <algorithm>
#include <utility>
#include "vec.hpp"
namespace srb2::math
{
template <typename T>
struct LineSegment
{
using vec2 = Vec2<T>;
using view = std::pair<const vec2&, const vec2&>;
vec2 a, b;
LineSegment(vec2 a_, vec2 b_) : a(a_), b(b_) {}
template <typename U>
LineSegment(const LineSegment<U>& b) : LineSegment(b.a, b.b) {}
bool horizontal() const { return a.y == b.y; }
bool vertical() const { return a.x == b.x; }
view by_x() const { return std::minmax(a, b, [](auto& a, auto& b) { return a.x < b.x; }); }
view by_y() const { return std::minmax(a, b, [](auto& a, auto& b) { return a.y < b.y; }); }
};
}; // namespace srb2
#endif/*math_line_segment_hpp*/