From 2a0597a8d3d088df43f9787fd78d4d50155f6d3c Mon Sep 17 00:00:00 2001 From: Jean Fabre-Monplaisir Date: Thu, 8 Jan 2015 17:30:28 +0100 Subject: =?UTF-8?q?Ajout=20de=20fonction=20de=20distance=20d'un=20point=20?= =?UTF-8?q?=C3=A0=20un=20segment.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geom.hpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'geom.hpp') diff --git a/geom.hpp b/geom.hpp index 1b5c058..0b07bdd 100644 --- a/geom.hpp +++ b/geom.hpp @@ -11,6 +11,10 @@ struct vec { return sqrt(x*x + y*y); } + double sqnorm() const { + return x*x + y*y; + } + double angle() const { double xx = x / norm(); double a = acos(x); @@ -81,11 +85,14 @@ struct line { // calculate distance from p to the line return abs(a*p.x+b*p.y+c)/sqrt(a*a+b*b); } + vec dir() const { + // calculate a directional vector oh the line + return vec(-b,a); + } vec proj(vec p) const { // calculate orthogonal projection of point p on the line - // TODO - return vec(0, 0); + return p-(a*p.x+b*p.y+c)/(a*a+b*b)*vec(a,b); } double angle() const { @@ -101,12 +108,17 @@ struct segment { bool on_segment(vec p) const { // TODO + // does point intersect segment? return false; } double dist(vec p) const { - + double scal = vec::dot(b-a, p-a); + double sqn = (b-a).sqnorm + if(scal > sqn)return (p-b).norm ; + if(scal < 0) return (p-a).norm ; + return line(a,b).dist(p); return 1; } }; -- cgit v1.2.3