Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double EPS = 1.e-11; //10^(-11) - very important!!! doubles are not precise!!!
- public double ptLineDist(Point P){//distance from a point to a line
- double cross = abs(cross(P, P1, P2));
- double dist = sqrt((P1.x - P2.x)*(P1.x - P2.x) + (P1.y - P2.y)*(P1.y - P2.y));
- return cross/dist;
- }
- public double ptRayDist(Point P){ //distance from a point to a ray
- double ret = ptLineDist(P);
- double p1p2x = P2.x - P1.x;
- double p1p2y = P2.y - P1.y;
- double p1px = P.x - P1.x;
- double p1py = P.y - P1.y;
- double val = p1p2x*p1px + p1p2y*p1py;
- if (val >= -EPS)
- return ret;
- else return sqrt((P.x - P1.x)*(P.x - P1.x) + (P.y - P1.y)*(P.y - P1.y));
- }
Add Comment
Please, Sign In to add comment