/* * v2d_line_distance.c - Calculate the distance between a point and a line * * Written 2012 by Werner Almesberger * Copyright 2012 Werner Almesberger * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. */ #include #include "poly2d.h" /* * We use formula (14) from * http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html * but keep the sign. */ double v2d_line_distance(const struct v2d *a, const struct v2d *b, const struct v2d *p) { double ax, ay; ax = b->x-a->x; ay = b->y-a->y; return (ax*(a->y-p->y)-ay*(a->x-p->x))/hypot(ax, ay); }