2021-03-12 21:33:46 +00:00
|
|
|
#ifndef SDLPP_HPP_LINE
|
|
|
|
#define SDLPP_HPP_LINE
|
|
|
|
|
|
|
|
#include "sdlpp_common.hpp"
|
|
|
|
#include "sdlpp_vector.hpp"
|
2021-03-13 14:05:16 +00:00
|
|
|
#include <iostream>
|
2021-03-12 21:33:46 +00:00
|
|
|
|
|
|
|
namespace SDLPP {
|
2021-03-13 17:36:29 +00:00
|
|
|
template < typename T > class SDLPPSCOPE Line {
|
2021-03-12 21:33:46 +00:00
|
|
|
public:
|
|
|
|
Line() = delete;
|
|
|
|
~Line() = default;
|
2021-03-13 17:36:29 +00:00
|
|
|
Line( const Vec2D< T > &start, const Vec2D< T > &end )
|
2021-03-13 14:05:16 +00:00
|
|
|
: _start( start ), _end( end ) {
|
|
|
|
updateMost();
|
|
|
|
}
|
2021-03-13 17:36:29 +00:00
|
|
|
Line( T x_1, T y_1, T x_2, T y_2 ) : Line( { x_1, y_1 }, { x_2, y_2 } ) {}
|
|
|
|
Line( const Vec2D< T > &start, const Vec2D< T > &end, bool infinite )
|
|
|
|
: Line( start, end ) {
|
|
|
|
_infinite = infinite;
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
Line( T x_1, T y_1, T x_2, T y_2, bool infinite )
|
2021-03-13 14:05:16 +00:00
|
|
|
: Line( { x_1, y_1 }, { x_2, y_2 }, infinite ) {}
|
2021-03-13 17:36:29 +00:00
|
|
|
Line( const Line &input ) : Line( input.getStart(), input.getEnd() ) {}
|
|
|
|
Line &operator=( const Line &input ) {
|
2021-03-13 14:05:16 +00:00
|
|
|
_start = input.getStart();
|
|
|
|
_end = input.getEnd();
|
|
|
|
updateMost();
|
|
|
|
return *this;
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-13 14:05:16 +00:00
|
|
|
void updateMost() {
|
2021-03-13 17:36:29 +00:00
|
|
|
if ( _start.getY() < _end.getY() ) {
|
2021-03-13 14:05:16 +00:00
|
|
|
top = &_start;
|
|
|
|
bottom = &_end;
|
|
|
|
} else {
|
|
|
|
top = &_end;
|
|
|
|
bottom = &_start;
|
|
|
|
}
|
2021-03-13 17:36:29 +00:00
|
|
|
if ( _start.getX() < _end.getX() ) {
|
2021-03-13 14:05:16 +00:00
|
|
|
left = &_start;
|
|
|
|
right = &_end;
|
|
|
|
} else {
|
|
|
|
left = &_end;
|
|
|
|
right = &_start;
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-13 17:36:29 +00:00
|
|
|
const Vec2D< T > &getStart() const {
|
2021-03-12 21:33:46 +00:00
|
|
|
return _start;
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-13 17:36:29 +00:00
|
|
|
const Vec2D< T > &getEnd() const {
|
2021-03-12 21:33:46 +00:00
|
|
|
return _end;
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-12 21:33:46 +00:00
|
|
|
double length() const {
|
|
|
|
return vecDistance( _start, _end );
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-12 21:33:46 +00:00
|
|
|
double lengthSquared() const {
|
|
|
|
return vecDistanceSquared( _start, _end );
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-12 21:33:46 +00:00
|
|
|
void setInfinite( bool infinite ) {
|
|
|
|
_infinite = infinite;
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-12 21:33:46 +00:00
|
|
|
bool isInfinite() {
|
|
|
|
return _infinite;
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-13 17:36:29 +00:00
|
|
|
void add( const Vec2D< T > &vec ) {
|
2021-03-12 21:33:46 +00:00
|
|
|
_start += vec;
|
|
|
|
_end += vec;
|
|
|
|
}
|
2021-03-15 13:50:40 +00:00
|
|
|
|
2021-03-13 17:36:29 +00:00
|
|
|
const Vec2D< T > &topmost() const {
|
2021-03-13 14:05:16 +00:00
|
|
|
return *top;
|
|
|
|
}
|
2021-03-13 17:36:29 +00:00
|
|
|
const Vec2D< T > &bottomost() const {
|
2021-03-13 14:05:16 +00:00
|
|
|
return *bottom;
|
|
|
|
}
|
2021-03-13 17:36:29 +00:00
|
|
|
const Vec2D< T > &leftmost() const {
|
2021-03-13 14:05:16 +00:00
|
|
|
return *left;
|
|
|
|
}
|
2021-03-13 17:36:29 +00:00
|
|
|
const Vec2D< T > &rightmost() const {
|
2021-03-13 14:05:16 +00:00
|
|
|
return *right;
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
|
|
|
|
private:
|
2021-03-13 17:36:29 +00:00
|
|
|
Vec2D< T > _start;
|
|
|
|
Vec2D< T > _end;
|
|
|
|
Vec2D< T > *top = nullptr;
|
|
|
|
Vec2D< T > *bottom = nullptr;
|
|
|
|
Vec2D< T > *left = nullptr;
|
|
|
|
Vec2D< T > *right = nullptr;
|
2021-03-12 21:33:46 +00:00
|
|
|
bool _infinite = false;
|
|
|
|
};
|
|
|
|
} // namespace SDLPP
|
|
|
|
|
|
|
|
#endif
|