Skip to content

Instantly share code, notes, and snippets.

@mbalunovic
Created March 3, 2009 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbalunovic/73399 to your computer and use it in GitHub Desktop.
Save mbalunovic/73399 to your computer and use it in GitHub Desktop.
line segment - point distance
#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