spacebox/src/Segment.hpp

79 lines
2.1 KiB
C++

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#pragma once
#include <ostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include "glm/vec2.hpp"
class Box;
class Sprite;
class Segment
{
private:
glm::vec2 start_, end_;
public:
Segment(const glm::vec2&, const glm::vec2&);
Segment();
Segment(const glm::vec2&);
Segment(const Box&, const Box&);
Segment(const Sprite&, const Sprite&);
glm::vec2 start() const;
void start(const glm::vec2&);
glm::vec2 end() const;
void end(const glm::vec2&);
bool intersect(const Segment&, glm::vec2* = nullptr) const;
bool intersect(const Segment&, glm::vec2&) const;
float dx() const;
float dy() const;
float length() const;
Box box() const;
void move(const glm::vec2&);
glm::vec2 center() const;
glm::vec2 step(float) const;
glm::vec2 step_relative(float) const;
std::vector<Segment> subsegments(int) const;
inline bool operator<(const Segment& segment) const { return operator<(segment.length()); }
/*
std::cout << (a < b) << (a > b) << (b < c) << (b <= c) << (c > b) << (c >= b) << (d < 1) << (d < 2) <<
(d >= a) << (d > c) << std::endl;
should print 0101010101
*/
template<typename N>
inline bool operator<(const N& other) const { return length() < other; }
template<typename N>
inline bool operator>(const N& other) const { return other < length(); }
template<typename N>
inline bool operator<=(const N& other) const { return !operator>(other); }
template<typename N>
inline bool operator>=(const N& other) const { return !operator<(other); }
};
namespace std
{
std::ostream& operator<<(std::ostream&, const Segment&);
}