polygon.cpp

Go to the documentation of this file.
00001 #ifndef POLYGON_IMPLEMENTATION_FILE
00002 #define POLYGON_IMPLEMENTATION_FILE
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.cpp>
00021 
00022 namespace geometric {
00023 
00024 bool polygon::inside(const cartesian_point &to_check)
00025 {
00026   int right_intersect_count = 0;
00027   for (int i = 0; i < length(); i++) {
00028     cartesian_point vert_1 = get(i);
00029     cartesian_point vert_2 = get( (i + 1) % length() );
00030     if ( (to_check.y() < minimum(vert_1.y(), vert_2.y()))
00031         || (to_check.y() > maximum(vert_1.y(), vert_2.y())) ) continue;
00032     double x_intersect;
00033     if (vert_2.x() == vert_1.x()) {
00034       x_intersect = vert_2.x();
00035     } else {
00036       double m = (vert_2.y() - vert_1.y()) / (vert_2.x() - vert_1.x());
00037       x_intersect = 1.0 / m * (to_check.y() - vert_1.y() + m * vert_1.x());
00038     }
00039     if (x_intersect > to_check.x()) right_intersect_count++;
00040   }
00041   return !!(right_intersect_count % 2);
00042 }
00043 
00044 }
00045 
00046 
00047 #endif //POLYGON_IMPLEMENTATION_FILE
00048 

Generated on Fri Nov 28 04:29:15 2008 for HOOPLE Libraries by  doxygen 1.5.1