Thread: .... lost in C syntax structure : (

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    62

    .... lost in C syntax structure : (

    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);
    }

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    62
    NOTE: sorry ...In the circle code all references to the "Z" component axis are to be removed (only need 2D).

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Rick19468 View Post
    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 ???
    "XY" is a user-defined type which you can use like any built-in type (int, double, char, ...).
    You create user-defined types with the typedef keyword. Thus, the first three arguments of "RaySphere" are of type XP.
    The second line defines the variable "dp" to be of type XY too.
    The third line suggests that XY is a structure containing at least a member called "x". You have to find the typedef of XY to know how exactly this structure looks like but I guess it is something like:
    Code:
    typedef struct
    {
        int x, y, z;
    } XY;
    Quote Originally Posted by Rick19468 View Post
    *mu1 = 0; // a “*” leading mu1 ??? never seen this!
    "mu1" is a pointer.
    In short, a pointer stores the memory address of an object. To get the value of that object or change it's value (as it is done here), you have to dereference the pointer which is done by the "*" operator. Thus with pointers you work on objects indirectly.

    I've also noticed that the circle code you've posted contains some macros (ABS, EPS, TRUE, FALSE) which you need to adapt to your code too.

    You will probably don't understand everything I've written above (considering that you have just started to learn C). Just take your time, read through the links I've given you and come back with any additional questions.

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    62

    Struts!! : )

    Thanks Andi! ..... I was able to work it out last night. Awesome stuff this programming.







    Quote Originally Posted by AndiPersti View Post
    "XY" is a user-defined type which you can use like any built-in type (int, double, char, ...).
    You create user-defined types with the typedef keyword. Thus, the first three arguments of "RaySphere" are of type XP.
    The second line defines the variable "dp" to be of type XY too.
    The third line suggests that XY is a structure containing at least a member called "x". You have to find the typedef of XY to know how exactly this structure looks like but I guess it is something like:
    Code:
    typedef struct
    {
        int x, y, z;
    } XY;

    "mu1" is a pointer.
    In short, a pointer stores the memory address of an object. To get the value of that object or change it's value (as it is done here), you have to dereference the pointer which is done by the "*" operator. Thus with pointers you work on objects indirectly.

    I've also noticed that the circle code you've posted contains some macros (ABS, EPS, TRUE, FALSE) which you need to adapt to your code too.

    You will probably don't understand everything I've written above (considering that you have just started to learn C). Just take your time, read through the links I've given you and come back with any additional questions.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Syntax Versus Pointer Syntax
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2011, 10:56 AM
  2. structure syntax error
    By uavflyer in forum C Programming
    Replies: 10
    Last Post: 07-19-2010, 12:42 AM
  3. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  4. Syntax error before a structure
    By ChoCo in forum C Programming
    Replies: 2
    Last Post: 10-03-2009, 08:50 AM
  5. Some structure and Syntax Help Please!
    By jessweetd in forum C Programming
    Replies: 4
    Last Post: 09-30-2004, 11:13 PM