aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--geom.hpp82
-rw-r--r--main.cpp1
-rw-r--r--problem.hpp9
4 files changed, 83 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index d1b0544..96a1d45 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
BIN=pathfind
$(BIN): *.cpp *.hpp
- g++ -o $@ *.cpp -lm
+ g++ -o $@ *.cpp -lm -O2
diff --git a/geom.hpp b/geom.hpp
index 09b2f36..1b5c058 100644
--- a/geom.hpp
+++ b/geom.hpp
@@ -17,9 +17,49 @@ struct vec {
return (y > 0 ? a : -a);
}
+ vec normalize() const {
+ double n = norm();
+ return vec(x / n, y / n);
+ }
+
+ bool is_nil() const {
+ return x == 0 && y == 0;
+ }
+
vec operator+ (const vec& o) const {
return vec(x + o.x, y + o.y);
}
+ vec operator- (const vec& o) const {
+ return vec(x - o.x, y - o.y);
+ }
+ vec operator* (double m) const {
+ return vec(m * x, m * y);
+ }
+ vec operator/ (double m) const {
+ return vec(m / x, m / y);
+ }
+
+ static vec from_polar(double r, double theta) {
+ return vec(r * cos(theta), r * sin(theta));
+ }
+
+ static vec dot(vec a, vec b) { // dot product (produit scalaire)
+ return a.x * b.x + a.y * b.y;
+ }
+ static vec cross(vec a, vec b) { // cross product (d├ęterminant 2x2)
+ return a.x * b.y - a.y * b.x;
+ }
+ static vec angle(vec a, vec b) { // oriented angle between two vectors
+ if (a.is_nil() || b.is_nil()) return 0;
+ float cos = dot(a.normalize(), b.normalize());
+ if (cos <= -1) return M_PI;
+ float uangle = acos(cos);
+ if (cross(a, b) > 0) {
+ return uangle;
+ } else {
+ return -uangle;
+ }
+ }
};
struct line {
@@ -27,17 +67,27 @@ struct line {
double a, b, c;
line(double aa, double bb, double cc) : a(aa), b(bb), c(cc) {}
- line(vec p1, vec p2) {
- a = p1.x-p2.x ;
- b = p1.y-p2.y ;
- c = p1.x*(p2.x-p1.x)+p1.y*(p2.y-p1.y);
-
- }
-
+ line(vec p1, vec p2) {
+ a = p1.x-p2.x ;
+ b = p1.y-p2.y ;
+ c = p1.x*(p2.x-p1.x)+p1.y*(p2.y-p1.y);
+ }
+
+ bool on_line(vec p) const {
+ return a * p.x + b * p.y + c == 0;
+ }
+
double dist(vec p) const {
+ // calculate distance from p to the line
return abs(a*p.x+b*p.y+c)/sqrt(a*a+b*b);
}
+ vec proj(vec p) const {
+ // calculate orthogonal projection of point p on the line
+ // TODO
+ return vec(0, 0);
+ }
+
double angle() const {
return vec(-b, a).angle();
}
@@ -47,7 +97,13 @@ struct line {
struct segment {
vec a, b;
- segment(vec pa, vec pb) : a(pa), b(pb), {}
+ segment(vec pa, vec pb) : a(pa), b(pb) {}
+
+ bool on_segment(vec p) const {
+ // TODO
+ // does point intersect segment?
+ return false;
+ }
double dist(vec p) const {
@@ -61,6 +117,15 @@ struct circle {
circle(double x, double y, double rr) : c(x, y), r(rr) {}
circle(vec cc, double rr) : c(cc), r(rr) {}
+
+ bool on_circle(vec.p) const {
+ return ((p - c).norm() == r);
+ }
+
+ double dist(vec p) const {
+ // TODO
+ return 1;
+ }
};
struct circpoint {
@@ -68,6 +133,7 @@ struct circpoint {
double theta;
circpoint(circle cc, double th) : c(cc), theta(th) {}
+ circpoint(vec cc, double rr, double th : c(cc, rr), theta(th) {}
vec pos() const {
return c.c + vec(c.r * cos(theta), c.r * sin(theta));
diff --git a/main.cpp b/main.cpp
index b5a4386..31616fa 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,6 +1,7 @@
#include <stdio.h>
#include "geom.hpp"
+#include "problem.hpp"
int main() {
printf("%.10f\n", M_PI);
diff --git a/problem.hpp b/problem.hpp
index 535affa..10adaf8 100644
--- a/problem.hpp
+++ b/problem.hpp
@@ -16,9 +16,14 @@ struct hilare_a { // System A
// position actuelle
double x, y, theta, phi;
+ vec pos() const { return vec(x, y); }
+ vec dir() const { return vec::from_polar(1, theta); }
+
vec pos_trolley() const {
- //TODO
- return vec(0, 0);
+ return pos() + vec::from_polar(l, theta + phi + M_PI);
+ }
+ vec dir_trolley() const {
+ return vec::from_polar(1, theta + phi);
}
};