Editing Point-in-polygon

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 4: Line 4:
 
; GW Gourmet Functions Used: [[logZp1oZm1]]
 
; GW Gourmet Functions Used: [[logZp1oZm1]]
  
  #include <cmath>
+
  #include <math.h>
 
  #include <complex>
 
  #include <complex>
 
  #include <iostream>
 
  #include <iostream>
Line 21: Line 21:
 
  bool IsInsidePolygon(const cmplex &z, const cmplex *zv, const int N)
 
  bool IsInsidePolygon(const cmplex &z, const cmplex *zv, const int N)
 
  {
 
  {
  bool close = false;  
+
double y(z.imag());
  if (std::abs(zv[0]-zv[N])>REALSMALL){
+
double x(z.real());
  std::cout<<"PolyIsInside : Bad polygon"<<std::endl;
+
int  i;
 +
  bool close(false);  
 +
  if (abs(zv[0]-zv[N])>REALSMALL){
 +
  cout<<"PolyIsInside : Bad polygon"<<endl;
 
  exit(0);
 
  exit(0);
 
  }  
 
  }  
 
  //check if point is close to boundary of polygon
 
  //check if point is close to boundary of polygon
  for (int i=0; i<N; i++){
+
  for (i=0; i<N; i++){
  if (((std::abs(z-zv[i])+std::abs(z-zv[i+1]))/std::abs(zv[i+1]-zv[i]))<CLOSE_TO_LINE){
+
  if (((abs(z-zv[i])+abs(z-zv[i+1]))/(abs(zv[i+1]-zv[i])))<CLOSE_TO_LINE){
 
  close=true;break;
 
  close=true;break;
 
  }
 
  }
Line 34: Line 37:
 
  //if not close, use faster ray-tracing algorithm
 
  //if not close, use faster ray-tracing algorithm
 
  //adapted from Wm. Randolph Franklin <wrf@ecse.rpi.edu>
 
  //adapted from Wm. Randolph Franklin <wrf@ecse.rpi.edu>
double y = z.imag();
 
double x = z.real();
 
 
  if (!close)
 
  if (!close)
 
  {
 
  {
  bool in = false;
+
  bool in(false);
  for (int i=0, j=N-1; i<N; j=i++) {
+
int j;
 +
  for (i=0, j=N-1; i<N; j=i++) {
 
  if ((((zv[i].imag()<=y) && (y<zv[j].imag())) ||
 
  if ((((zv[i].imag()<=y) && (y<zv[j].imag())) ||
 
  ((zv[j].imag()<=y) && (y<zv[i].imag()))) &&
 
  ((zv[j].imag()<=y) && (y<zv[i].imag()))) &&
Line 54: Line 56:
 
  else
 
  else
 
  {
 
  {
  double sum = 0;
+
cmplex Z;
 +
  double sum(0);
 
  for (int i=0;i<N; i++)
 
  for (int i=0;i<N; i++)
 
  {
 
  {
  cmplex Z=(z-0.5*(zv[i]+zv[i+1]))/(0.5*(zv[i+1]-zv[i]));
+
  Z=(z-0.5*(zv[i]+zv[i+1]))/(0.5*(zv[i+1]-zv[i]));
 
  sum+=logZm1oZp1(Z).imag();
 
  sum+=logZm1oZp1(Z).imag();
 
  }
 
  }
  return std::abs(sum)>=REALSMALL;
+
  return (fabs(sum)<REALSMALL ? false : true);
 
  }
 
  }
 
  }
 
  }

Please note that all contributions to AEMWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see AEMWiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)