spacebox/src/Segment.hpp

78 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 Segment
{
private:
glm::vec2 start_, end_;
public:
Segment(const glm::vec2&, const glm::vec2&);
Segment();
Segment(const glm::vec2&);
Segment(const Box&, const Box&);
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()); }
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&);
}
/* Add Segment class to the sb namespace. This should be the default location, but Segment is left in the global namespace
* for backward compatibility.
*/
namespace sb
{
using ::Segment;
}