Root/poly2d/p2d_contains_point.c

1/*
2 * p2d_contains_point.c - Determine whether polygon contains point/polygon
3 *
4 * Based on the algorithm by W. Randolph Franklin
5 * http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
6 * which is distributed under the following license, similar to the 3-clause
7 * BSD license:
8 *
9 * Copyright (c) 1970-2003, Wm. Randolph Franklin
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * 1. Redistributions of source code must retain the above copyright notice,
19 * this list of conditions and the following disclaimers.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice in the documentation and/or other materials provided with the
22 * distribution.
23 * 3. The name of W. Randolph Franklin may not be used to endorse or promote
24 * products derived from this Software without specific prior written
25 * permission.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 * DEALINGS IN THE SOFTWARE.
34 */
35
36
37#include <stdbool.h>
38
39#include "poly2d.h"
40
41
42bool p2d_contains_point(const struct p2d *p, const struct v2d *v)
43{
44    const struct v2d *j, *i;
45    bool in = 0;
46
47    j = p->v;
48    do {
49        i = j->next;
50        if (((i->y > v->y) != (j->y > v->y)) &&
51            (v->x < (j->x-i->x)*(v->y-i->y)/
52            (j->y-i->y)+i->x))
53            in = !in;
54        j = i;
55    }
56    while (j != p->v);
57    return in;
58}
59

Archive Download this file

Branches:
master



interactive