polygon.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : polygon                                                           *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "polygon.h"
00019 
00020 #include <basis/array.h>
00021 
00022 using namespace basis;
00023 
00024 namespace geometric {
00025 
00026 bool polygon::inside(const cartesian_point &to_check)
00027 {
00028   int right_intersect_count = 0;
00029   for (int i = 0; i < length(); i++) {
00030     cartesian_point vert_1 = get(i);
00031     cartesian_point vert_2 = get( (i + 1) % length() );
00032     if ( (to_check.y() < minimum(vert_1.y(), vert_2.y()))
00033         || (to_check.y() > maximum(vert_1.y(), vert_2.y())) ) continue;
00034     double x_intersect;
00035     if (vert_2.x() == vert_1.x()) {
00036       x_intersect = vert_2.x();
00037     } else {
00038       double m = (vert_2.y() - vert_1.y()) / (vert_2.x() - vert_1.x());
00039       x_intersect = 1.0 / m * (to_check.y() - vert_1.y() + m * vert_1.x());
00040     }
00041     if (x_intersect > to_check.x()) right_intersect_count++;
00042   }
00043   return !!(right_intersect_count % 2);
00044 }
00045 
00046 }
00047 
00048 
00049 
00050 
Generated on Sat Jan 28 04:22:41 2012 for hoople2 project by  doxygen 1.6.3