polygon.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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