Hi! (I'm day four in c programming)

I'm working with the c code sample below "Point in Polygon" This runs just fine!

After running this code, I next want to use this same "test point" and polygon again, but this time to test for a line/circle intersection with the circle(3 grid diameter) being around my test point.

The code sample I found below "Circle around the Test Point" looks like what I want to do, but I don't understand the variable assignments structure here.

Examples:

int RaySphere(XY p1,XY p2,XY sc,.... //How do I assign “XY p1” ???

XY dp; // XY “with a space between” dp ??? ...what is this??

dp.x = p2.x - p1.x; //dp with a dot in-between x ???

*mu1 = 0; // a “*” leading mu1 ??? never seen this!

.. is this stuff “c code”? :(

Basically, I want to add this code to the end of the "Point in Polygon" code to use the (3 grid size) circle to inform me if the “test point” is this close or not to polygon border.

Any help would be great!

Thanks!

Code:

/* POINT in POLYGON (in or out) */

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main(int argc, char** argv) {

int bi = 8; //Set Boundry loop indexing variable

float x[bi];

float y[bi];

/* The coordinates of the test point */

float px=16;// Test X location point

float py=20; //Test Y location point

float x1;

float x2;

float k;

/* The points creating the polygon. */

x[0] = 7; y[0] = 22;

x[1] = 10; y[1] = 10;

x[2] = 7; y[2] = 2;

x[3] = 15; y[3] = 7;

x[4] = 21; y[4] = 6;

x[5] = 30; y[5] = 14;

x[6] = 22; y[6] = 18;

x[7] = 20; y[7] = 26;

/* How many times the ray crosses a line-segment */

int crossings = 0;

/* Iterate through each line */

for ( int i = 0; i < bi; i++ ){

/* This is done to ensure that we get the same result when

the line goes from left to right and right to left */

if ( x[i] < x[ (i+1)%bi ] ){

x1 = x[i];

x2 = x[(i+1)%bi];

} else {

x1 = x[(i+1)%bi];

x2 = x[i];

}

/* First check if the ray is possible to cross the line */

if ( px > x1 && px <= x2 && ( py < y[i] || py <= y[(i+1)%bi] ) ) {

static const float eps = 0.000001;

/* Calculate the equation of the line */

float dx = x[(i+1)%bi] - x[i];

float dy = y[(i+1)%bi] - y[i];

float k;

if ( fabs (dx) < eps ){

k = INFINITY; //REM: <include math.h> !!!!

} else {

k = dy/dx;

}

float m = y[i] - k * x[i];

/* Find if the ray crosses the line */

float y2 = k * px + m;

if ( py <= y2 ){

crossings++;

}

}

}

if ( crossings % 2 == 1 )

{

printf("Point is INSIDE the Polygon\n");

}

else

{

printf("Point is OUTSIDE the POLYGON\n");

}

// Need to add the "Circle around the Test Point" code here!!

return (EXIT_SUCCESS);

}

/* END of "POINT in POLYGON" code here */

/* Start of "Circle around the Test Point" code */

/*

Calculate the intersection of a ray and a sphere

The line segment is defined from p1 to p2

The sphere is of radius r and centered at sc

There are potentially two points of intersection given by

p = p1 + mu1 (p2 - p1)

p = p1 + mu2 (p2 - p1)

Return FALSE if the ray doesn't intersect the sphere.

*/

int RaySphere(XY p1,XY p2,XY sc,double r,double *mu1,double *mu2)

{

double a,b,c;

double bb4ac;

XY dp;

dp.x = p2.x - p1.x;

dp.y = p2.y - p1.y;

a = dp.x * dp.x + dp.y * dp.y;

b = 2 * (dp.x * (p1.x - sc.x) + dp.y * (p1.y - sc.y) + dp.z * (p1.z - sc.z));

c = sc.x * sc.x + sc.y * sc.y + sc.z * sc.z;

c += p1.x * p1.x + p1.y * p1.y + p1.z * p1.z;

c -= 2 * (sc.x * p1.x + sc.y * p1.y + sc.z * p1.z);

c -= r * r;

bb4ac = b * b - 4 * a * c;

if (ABS(a) < EPS || bb4ac < 0) {

*mu1 = 0;

*mu2 = 0;

return(FALSE);

}

*mu1 = (-b + sqrt(bb4ac)) / (2 * a);

*mu2 = (-b - sqrt(bb4ac)) / (2 * a);

return(TRUE);

}