Created
March 3, 2009 16:49
-
-
Save mbalunovic/73399 to your computer and use it in GitHub Desktop.
line segment - point distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <algorithm> | |
#include <iostream> | |
#include <cstring> | |
#include <cmath> | |
using namespace std; | |
struct point { | |
int x,y; | |
}; | |
struct line { | |
point t1,t2; | |
}; | |
int sqr ( int x ) { return x * x; } | |
int cross(point p1, point p2, point p3){ | |
return (( p2.x - p1.x )*( p3.y-p1.y ) - (p3.x-p1.x) * (p2.y-p1.y)); | |
} | |
int dot(point p1, point p2, point p3){ | |
return (( p2.x - p1.x )*( p3.x-p2.x ) + ( p2.y - p1.y )*( p3.y-p2.y)); | |
} | |
double distances ( point p1, point p2 ){ | |
return sqrt ( sqr(p1.x-p2.x)+sqr(p1.y-p2.y) ); | |
} | |
double lp_distance( point A, point B, point C ){ | |
double dista = cross(A,B,C) / distances(A,B); | |
int dots = dot(A,B,C); | |
if(dots>0) | |
return distances(B,C); | |
dots = dot(B,A,C); | |
if(dots>0) | |
return distances(A,C);return abs(dista); | |
} | |
int main(){ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment