It's free to join Gamasutra!|Have a question? Want to know who runs this site? Here you go.|Targeting the game development market with your product or service? Get info on advertising here.||For altering your contact information or changing email subscription preferences.
Registered members can log in here.Back to the home page.

Search articles, jobs, buyers guide, and more.

By Jeff Lander
Gamasutra
February 10, 2000

Printer Friendly Version

 

Change Login/Pwd
Post A Job
Post A Project
Post Resume
Post An Event
Post A Contractor
Post A Product
Write An Article
Get In Art Gallery
Submit News

 


 


[Submit Letter]

[View All...]
  



[Submit Event]
[View All...]

 


[Enter Forums...]

Note: Discussion forums for Gamasutra are hosted by the IGDA, which is free to join.
 

 

 


Features

Listing 2. The X Intercept Calculation.

 


// Procedure: PointInPoly (EDGE CROSSING VERSION)
// Purpose: Check if a point is inside a polygon
// Returns: TRUE if Point is inside polygon, else FALSE

BOOL CFateView::PointInPoly(tSector *sector, tPoint2D *hitPos)
{
// Local Variables
    short edge, first, next;
    tPoint2D *pnt1,*pnt2;
    BOOL inside = FALSE; // INITIAL TEST CONDITION
    BOOL flag1,flag2;

    edge = first = sector->edge; // SET UP INITIAL CONDITIONS
    pnt1 = &m_edgelist[edge].pos;
    flag1 = ( hitPos->y >= pnt1->y ) ; // IS THE FIRST                                         VERTEX OVER OR                                         UNDER THE LINE

     /* LOOP THROUGH THE VERTICES IN A SECTOR */
    do {
      next = m_edgelist[edge].nextedge; // CHECK THE NEXT VERTEX
      pnt2 = &m_edgelist[next].pos;
      flag2 = ( hitPos->y >= pnt2->y ); // IS IT OVER OR UNDER
      if (flag1 != flag2) // MAKE SURE THE EDGE ACTUALLY                        // CROSSES THE TEST X AXIS
       {
     // CALCULATE WHETHER THE SEGMENT ACTUALLY CROSSES THE X TEST AXIS
    // A TRICK FROM GRAPHIC GEMS IV TO GET RID OF THE X INTERCEPT DIVIDE
    if (((pnt2->y - hitPos->y) * (pnt1->x - pnt2->x) >=
      (pnt2->x - hitPos->x) * (pnt1->y - pnt2->y)) == flag2 )
      inside = !inside; // IF IT CROSSES TOGGLE THE INSIDE                      // FLAG (ODD IS IN, EVEN OUT)
      }
    pnt1 = pnt2; // RESET FOR NEXT STEP
    edge = next;
    flag1 = flag2;
    } while (edge != first);
    return inside;
}

________________________________________________________

Back to Article


join | contact us | advertise | write | my profile
news | features | companies | jobs | resumes | education | product guide | projects | store



Copyright © 2003 CMP Media LLC

privacy policy
| terms of service