2 * Grace - Graphics for Exploratory Data Analysis
4 * Home page: http://plasma-gate.weizmann.ac.il/Grace/
6 * Copyright (c) 1991-1995 Paul J Turner, Portland, OR
7 * Copyright (c) 1996-2003 Grace Development Team
9 * Maintained by Evgeny Stambulchik
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * routines to allocate, manipulate, and return
32 * information about regions.
41 #include "core_utils.h"
45 * routines to determine if a point lies in a polygon
47 static int intersect_to_left(const WPoint
*wp
,
48 const WPoint
*wp1
, const WPoint
*wp2
)
52 /* ignore horizontal lines */
53 if (wp1
->y
== wp2
->y
) {
56 /* not contained vertically */
57 if (((wp
->y
< wp1
->y
) && (wp
->y
< wp2
->y
)) ||
58 ((wp
->y
> wp1
->y
) && (wp
->y
> wp2
->y
))) {
61 /* none of the above, compute the intersection */
62 if ((xtmp
= wp2
->x
- wp1
->x
) != 0.0) {
63 m
= (wp2
->y
- wp1
->y
) / xtmp
;
64 b
= wp1
->y
- m
* wp1
->x
;
65 xtmp
= (wp
->y
- b
) / m
;
70 /* check for intersections at a vertex */
71 /* if this is the max ordinate then accept */
72 if (wp
->y
== wp1
->y
) {
73 if (wp1
->y
> wp2
->y
) {
79 /* check for intersections at a vertex */
80 if (wp
->y
== wp2
->y
) {
81 if (wp2
->y
> wp1
->y
) {
87 /* no vertices intersected */
94 * determine if (x,y) is in the polygon xlist[], ylist[]
96 static int inbound(const WPoint
*wp
, const WPoint
*wps
, int n
)
100 for (i
= 0; i
< n
; i
++) {
101 l
+= intersect_to_left(wp
, &wps
[i
], &wps
[(i
+ 1) % n
]);
106 static int isleft(const WPoint
*wp
, const WPoint
*wp1
, const WPoint
*wp2
)
108 if ((wp2
->x
- wp1
->x
)*(wp
->y
- wp2
->y
) -
109 (wp2
->y
- wp1
->y
)*(wp
->x
- wp2
->x
) >= 0) {
116 static int inband(const WPoint
*wp
, const WPoint
*wp1
, const WPoint
*wp2
)
120 s
= (wp
->x
- wp1
->x
)*(wp2
->x
- wp1
->x
) +
121 (wp
->y
- wp1
->y
)*(wp2
->y
- wp1
->y
);
122 d2
= (wp2
->x
- wp1
->x
)*(wp2
->x
- wp1
->x
) +
123 (wp2
->y
- wp1
->y
)*(wp2
->y
- wp1
->y
);
124 if (s
< 0 || s
> d2
) {
131 int inregion(const Quark
*q
, const WPoint
*wp
)
133 region
*r
= region_get_data(q
);
142 return inbound(wp
, r
->wps
, r
->n
);
145 return isleft(wp
, &r
->wps
[0], &r
->wps
[1]);
152 return inband(wp
, &r
->wps
[0], &r
->wps
[1]);