added new helper methods
worked on gridWalker v3
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "Point2.h"
|
||||
#include "Ray2.h"
|
||||
#include "Line2.h"
|
||||
|
||||
#include "../Assertions.h"
|
||||
|
||||
@@ -65,6 +66,24 @@ public:
|
||||
|
||||
}
|
||||
|
||||
/** does this circle intersect with the given ray? */
|
||||
bool intersects(const Line2 line) const {
|
||||
|
||||
// https://math.stackexchange.com/questions/311921/get-location-of-vector-circle-intersection
|
||||
Point2 dir = line.p2 - line.p1;
|
||||
Point2 start = line.p1;
|
||||
const float a = dir.x*dir.x + dir.y*dir.y;
|
||||
const float b = 2 * dir.x * (start.x-center.x) + 2 * dir.y * (start.y - center.y);
|
||||
const float c = (start.x-center.x) * (start.x-center.x) + (start.y - center.y)*(start.y - center.y) - radius*radius;
|
||||
const float discr = b*b - 4*a*c;
|
||||
|
||||
if (discr < 0) {return false;}
|
||||
|
||||
const float t = (2*c) / (-b + std::sqrt(discr));
|
||||
return (t <= 1) && (t >= 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** configure this sphere to contain the given point-set */
|
||||
void adjustToPointSet(const std::vector<Point2>& lst) {
|
||||
|
||||
@@ -77,10 +77,10 @@ public:
|
||||
}
|
||||
|
||||
|
||||
Heading& operator = (const float _rad) {
|
||||
rad = _rad;
|
||||
return *this;
|
||||
}
|
||||
Heading& operator = (const float _rad) {
|
||||
rad = _rad;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** compare two headings */
|
||||
bool operator == (const Heading other) const {return rad == other.rad;}
|
||||
@@ -95,6 +95,11 @@ public:
|
||||
|
||||
float getRAD() const {return rad;}
|
||||
|
||||
/** convert heading into a direction-vector */
|
||||
Point2 asVector() const {
|
||||
return Point2(std::cos(rad), std::sin(rad));
|
||||
}
|
||||
|
||||
// /** get a random heading */
|
||||
// static Heading rnd() {
|
||||
// static std::minstd_rand gen(1234);
|
||||
|
||||
Reference in New Issue
Block a user