This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/geo/Line2.h
FrankE 34e52cd7f0 worked on floorplan (v2)
worked on grid-generation (v2)
new helper methods for geometry
new test cases
2016-07-13 19:11:18 +02:00

116 lines
2.6 KiB
C++
Executable File

#ifndef LINE2D_H
#define LINE2D_H
//#include <KLib/geo/Line.h>
#include "Point2.h"
class Line2 {
public:
Point2 p1;
Point2 p2;
public:
/** empty ctor */
Line2() : p1(), p2() {;}
/** value ctor */
Line2(const Point2 p1, const Point2 p2) : p1(p1), p2(p2) {;}
/** value ctor */
Line2(const float x1, const float y1, const float x2, const float y2) : p1(x1,y1), p2(x2,y2) {;}
// bool getSegmentIntersection(const Line& other) const {
// static K::Point p;
// return K::Line::getSegmentIntersection(other, p);
// }
/** get intersection between these two lines (unlimited length!) */
bool getLineIntersection(const Line2& other, Point2& result) const {
double bx = p2.x - p1.x;
double by = p2.y - p1.y;
double dx = other.p2.x - other.p1.x;
double dy = other.p2.y - other.p1.y;
double b_dot_d_perp = bx*dy - by*dx;
if(b_dot_d_perp == 0) {return false;}
double cx = other.p1.x - p1.x;
double cy = other.p1.y - p1.y;
double t = (cx*dy - cy*dx) / b_dot_d_perp;
result.x = p1.x + t * bx;
result.y = p1.y + t * by;
return true;
}
bool getSegmentIntersection(const Line2& other) const {
const double delta = 0.0000001;
const double bx = p2.x - p1.x;
const double by = p2.y - p1.y;
const double dx = other.p2.x - other.p1.x;
const double dy = other.p2.y - other.p1.y;
const double b_dot_d_perp = bx*dy - by*dx;
if (std::abs(b_dot_d_perp) == 0) {return false;}
const double cx = other.p1.x - p1.x;
const double cy = other.p1.y - p1.y;
const double t = (cx * dy - cy * dx) / b_dot_d_perp;
if(t < 0+delta || t > 1-delta) {return false;}
const double u = (cx * by - cy * bx) / b_dot_d_perp;
if(u < 0+delta || u > 1-delta) {return false;}
return true;
}
bool getSegmentIntersection(const Line2& other, Point2& result) const {
const float delta = 0.000001;
const float bx = p2.x - p1.x;
const float by = p2.y - p1.y;
const float dx = other.p2.x - other.p1.x;
const float dy = other.p2.y - other.p1.y;
const float b_dot_d_perp = bx*dy - by*dx;
if(b_dot_d_perp == 0) {return false;}
const float cx = other.p1.x - p1.x;
const float cy = other.p1.y - p1.y;
const float t = (cx * dy - cy * dx) / b_dot_d_perp;
if(t < 0+delta || t > 1-delta) {return false;}
const float u = (cx * by - cy * bx) / b_dot_d_perp;
if(u < 0+delta || u > 1-delta) {return false;}
result.x = p1.x + t * bx;
result.y = p1.y + t * by;
return true;
}
};
#endif // LINE2D_H