void closest_point(point2d p, line l, point2d *cp) { if (fabs(l.b) <= EPSILON) { // vertical line cp->x = -l.c; cp->y = p.y; return; } if (fabs(l.a) <= EPSILON) { // horizontal line cp->x = p.x; cp->y = -l.c; return; } // line with slope line perp; // perpendicular to l through (x,y) point_and_slope_to_line(p, 1/l.a, &perp); intersection_point(l, perp, cp); }
void closest_point(const point p_in, line l, point p_c) { line perp; /* perpendicular to l through (x,y) */ if(fabs(l.b) <= EPSILON) { /* vertical line */ p_c[X] = -(l.c); p_c[Y] = p_in[Y]; return; } if(fabs(l.a) <= EPSILON) { /* horizontal line */ p_c[X] = p_in[X]; p_c[Y] = -(l.c); return; } point_and_slope_to_line(p_in, 1/l.a, &perp); /* non-degenerate line */ /*printf("perpendicular bisector "); print_line(perp);*/ intersection_point(l, perp, p_c); /*printf("closest point "); print_point(p_c);*/ }