contrib/gel/gtrl/gtrl_polygon.cxx
Go to the documentation of this file.
00001 // This is gel/gtrl/gtrl_polygon.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author crossge@crd.ge.com
00008 
00009 #include "gtrl_polygon.h"
00010 
00011 gtrl_polygon::gtrl_polygon( const vcl_vector<gtrl_vertex_sptr> ps)
00012   : ps_(ps)
00013 {
00014 }
00015 
00016 
00017 // simple and efficient point in polygon test.
00018 //   from comp.graphics.algorithms faq
00019 bool gtrl_polygon::inside( const gtrl_vertex_sptr point) const
00020 {
00021     bool c = false;
00022 
00023     for (unsigned int i = 0, j = ps_.size()-1; i < ps_.size(); j = i++)
00024       {
00025         if ((((ps_[i]->y()<=point->y()) && (point->y()<ps_[j]->y())) ||
00026              ((ps_[j]->y()<=point->y()) && (point->y()<ps_[i]->y()))) &&
00027           (point->x() < (ps_[j]->x() - ps_[i]->x()) * (point->y() - ps_[i]->y())
00028           / (ps_[j]->y() - ps_[i]->y()) + ps_[i]->x()))
00029           c = !c;
00030       }
00031 
00032   return c;
00033 }