Root/poly2d/v2d_line_distance.c

1/*
2 * v2d_line_distance.c - Calculate the distance between a point and a line
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 Werner Almesberger
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 */
12
13
14#include <math.h>
15
16#include "poly2d.h"
17
18
19/*
20 * We use formula (14) from
21 * http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
22 * but keep the sign.
23 */
24
25double v2d_line_distance(const struct v2d *a, const struct v2d *b,
26    const struct v2d *p)
27{
28    double ax, ay;
29
30    ax = b->x-a->x;
31    ay = b->y-a->y;
32
33    return (ax*(a->y-p->y)-ay*(a->x-p->x))/hypot(ax, ay);
34}
35

Archive Download this file

Branches:
master



interactive