SDDSlib
Loading...
Searching...
No Matches
pointInsideContour.c File Reference

Provides functions for determining the relative position of points with respect to a polygonal contour, including whether a point lies inside it. More...

#include "mdb.h"

Go to the source code of this file.

Functions

double pointIsLeftOfLine (int64_t i1, int64_t i2, double *x, double *y, double x0, double y0)
 
int pointIsInsideContour (double x0, double y0, double *x, double *y, int64_t n, double *center, double theta)
 Determine if a given point (x0, y0) is inside a specified polygonal contour.
 

Detailed Description

Provides functions for determining the relative position of points with respect to a polygonal contour, including whether a point lies inside it.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, R. Soliday

Definition in file pointInsideContour.c.

Function Documentation

◆ pointIsInsideContour()

int pointIsInsideContour ( double x0,
double y0,
double * x,
double * y,
int64_t n,
double * center,
double theta )

Determine if a given point (x0, y0) is inside a specified polygonal contour.

This function uses the winding number method to check if the point (x0, y0) is inside the polygon defined by arrays x and y. If a rotation is specified (by a non-zero theta and a center), the point is rotated about the given center before the inside test is performed.

Parameters
[in]x0The x-coordinate of the point to test.
[in]y0The y-coordinate of the point to test.
[in]xArray of x-coordinates of the polygon vertices.
[in]yArray of y-coordinates of the polygon vertices.
[in]nThe number of vertices in the polygon.
[in]centerPointer to an array representing the (x,y) center of rotation.
[in]thetaThe clockwise rotation angle (in radians) to apply to the polygon.
Returns
Non-zero if the point is inside the polygon, zero otherwise.

Definition at line 51 of file pointInsideContour.c.

57 {
58 int64_t winding_number = 0;
59 int64_t i1, i2;
60
61 if (theta != 0 && center) {
62 double x1, y1, ct, st;
63 /* rotate (x0, y0) by theta about center */
64 x0 -= center[0];
65 y0 -= center[1];
66 ct = cos(theta);
67 st = sin(theta);
68 x1 = x0 * ct - y0 * st + center[0];
69 y1 = x0 * st + y0 * ct + center[1];
70 x0 = x1;
71 y0 = y1;
72 }
73
74 for (i1 = 0; i1 < n; i1++) {
75 if (i1 == (n - 1))
76 /* wrap */
77 i2 = 0;
78 else
79 i2 = i1 + 1;
80
81 if (y[i1] <= y0) {
82 /* start y <= y0 */
83 if (y[i2] > y0) {
84 /* upward crossing */
85#ifdef DEBUG
86 printf("upward crossing\n");
87 fflush(stdout);
88#endif
89 if (pointIsLeftOfLine(i1, i2, x, y, x0, y0) > 0) {
90 /* Point left of edge */
91 ++winding_number;
92 }
93 }
94 } else {
95 /* start y > y0 */
96 if (y[i2] <= y0) {
97 /* downward crossing */
98#ifdef DEBUG
99 printf("downward crossing\n");
100 fflush(stdout);
101#endif
102 if (pointIsLeftOfLine(i1, i2, x, y, x0, y0) < 0) {
103 /* Point right of edge */
104 --winding_number;
105 }
106 }
107 }
108#ifdef DEBUG
109 printf("i1 = %" PRId64 ", winding_number = %" PRId64 "\n", i1, winding_number);
110 fflush(stdout);
111#endif
112 }
113
114#ifdef DEBUG
115 printf("winding_number = %" PRId64 "\n", winding_number);
116 fflush(stdout);
117#endif
118
119 return (winding_number != 0);
120}

◆ pointIsLeftOfLine()

double pointIsLeftOfLine ( int64_t i1,
int64_t i2,
double * x,
double * y,
double x0,
double y0 )

Definition at line 19 of file pointInsideContour.c.

22 {
23 double result;
24 result = (x[i2] - x[i1]) * (y0 - y[i1]) - (x0 - x[i1]) * (y[i2] - y[i1]);
25#ifdef DEBUG
26 printf("pointIsLeftOfLine(%ld, %ld, %le, %le) = %le\n",
27 i1, i2, x0, y0, result);
28 fflush(stdout);
29#endif
30
31 return result;
32}