113 lines
2.8 KiB
C++
113 lines
2.8 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef POINT2_H
|
||
#define POINT2_H
|
||
|
||
#include <cmath>
|
||
#include <algorithm>
|
||
#include <string>
|
||
|
||
/**
|
||
* 2D Point
|
||
*/
|
||
struct Point2 {
|
||
|
||
float x;
|
||
float y;
|
||
|
||
/** ctor */
|
||
Point2() : x(0), y(0) {;}
|
||
|
||
/** ctor */
|
||
Point2(const float x, const float y) : x(x), y(y) {;}
|
||
|
||
Point2 operator - () const {return Point2(-x, -y);}
|
||
|
||
|
||
Point2 operator + (const Point2& o) const {return Point2(x+o.x, y+o.y);}
|
||
|
||
Point2 operator - (const Point2& o) const {return Point2(x-o.x, y-o.y);}
|
||
|
||
Point2 operator * (const float v) const {return Point2(v*x, v*y);}
|
||
|
||
Point2 operator / (const float v) const {return Point2(x/v, y/v);}
|
||
|
||
|
||
Point2& operator *= (const float v) {x*=v; y*=v; return *this;}
|
||
|
||
Point2& operator /= (const float v) {x/=v; y/=v; return *this;}
|
||
|
||
Point2& operator += (const Point2& o) {x+=o.x; y+=o.y; return *this;}
|
||
|
||
Point2& operator -= (const Point2& o) {x-=o.x; y-=o.y; return *this;}
|
||
|
||
|
||
bool operator == (const Point2& o) const {return x==o.x && y==o.y;}
|
||
|
||
bool operator != (const Point2& o) const {return x!=o.x || y!=o.y;}
|
||
|
||
bool eq (const Point2& o, const float delta) const { return eq(x,o.x,delta) && eq(y,o.y,delta); }
|
||
|
||
Point2 perpendicular() const {return Point2(-y, x);}
|
||
|
||
float length() const {return std::sqrt(x*x + y*y);}
|
||
|
||
Point2 normalized() const {return (*this) / length();}
|
||
|
||
Point2 rotated(const float rad) const {
|
||
return Point2(x*std::cos(rad)-y*std::sin(rad), x*std::sin(rad)+y*std::cos(rad));
|
||
}
|
||
|
||
/** get the distance between this point and the other one */
|
||
float getDistance(const Point2& o) const {
|
||
const float dx = x - o.x;
|
||
const float dy = y - o.y;
|
||
return std::sqrt(dx*dx + dy*dy);
|
||
}
|
||
|
||
std::string asString() const {
|
||
return "(" + std::to_string(x) + "," + std::to_string(y) + ")";
|
||
}
|
||
|
||
private:
|
||
|
||
static inline bool eq(const float a, const float b, const float delta) {return std::abs(a-b) <= delta;}
|
||
static inline bool ne(const float a, const float b, const float delta) {return std::abs(a-b) > delta;}
|
||
|
||
};
|
||
|
||
inline float dot(const Point2 p1, const Point2 p2) {
|
||
return (p1.x*p2.x) + (p1.y*p2.y);
|
||
}
|
||
|
||
inline float determinant(const Point2 p1, const Point2 p2) {
|
||
return (p1.x*p2.y) - (p1.y*p2.x);
|
||
}
|
||
|
||
inline void swap(Point2& p1, Point2& p2) {
|
||
std::swap(p1.x, p2.x);
|
||
std::swap(p1.y, p2.y);
|
||
}
|
||
|
||
namespace std {
|
||
|
||
template <> struct hash<Point2> {
|
||
std::size_t operator()(const Point2& p) const {
|
||
uint32_t x = *((uint32_t*)&(p.x));
|
||
uint32_t y = *((uint32_t*)&(p.y));
|
||
return std::hash<uint32_t>()(x^y);
|
||
}
|
||
};
|
||
|
||
}
|
||
|
||
#endif // POINT2_H
|