[solved] raycasting (punkt in polygon) ergebnis stimmt einfach ned :(

Seite 1 von 1 - Forum: Coding Stuff auf overclockers.at

URL: https://www.overclockers.at/coding-stuff/solved_raycasting_punkt_in_polygon_ergebnis_stimmt_221626/page_1 - zur Vollversion wechseln!


InfiX schrieb am 15.01.2011 um 03:57

ich hab ein problem, und zwar möcht ich herausfinden ob ein punkt in einem polygon liegt dazu hab ich die methode hier verwendet: http://alienryderflex.com/polygon/

nun stimmt aber einfach oft das ergebnis einfach ned und ich komm einfach nicht drauf was ich falsch mache...

rc1_163696.png

(unten der erste wert ist XY vom punkt und die nächsten 2 sind die 2 eckpunkte vom quadrat)

ergebnis ist true, ok soweit

rc2_163697.png

... jetzt ist das ergebnis plötzlich false, obwohl der punkt drin liegt (kann man auch an den koordinaten ablesen

so schaut meine funktion jetzt aus (eigentlich anders aber ich habs der einfachheit halber mal meine geändert und fast genauso formatiert wie im beispiel um zu schauen ob ich einen fehler find, find ihn aber nicht :()

Code:
boolean inside(float testX, float testY) {
  boolean oddResult = false;
  int j = polyX.length-1;
  for( int i=0; i < polyX.length; i++) {
    if( polyY[i] < testY && polyY[j] >= testY || polyY[j] < testY && polyY[i] >= testY ) {      
      if( polyX[i] + (testY - polyY[i]) / (polyY[j]-polyY[i]) * (polyX[j] - polyX[i]) < testX ) {    
        oddResult = !oddResult;
      }
    }
  } 
  return oddResult;
}


edit: war ja klar, jetzt wo ich stundenlang herumprobiert hab und das ganze hier poste komm ich drauf... hab das j = i; am ende der for-schleife vergessen

so muss es ausschaun:

Code:
boolean inside(float testX, float testY) {
  boolean oddResult = false;
  int j = polyX.length-1;
  for( int i=0; i < polyX.length; i++) {
    if( polyY[i] < testY && polyY[j] >= testY || polyY[j] < testY && polyY[i] >= testY ) {      
      if( polyX[i] + (testY - polyY[i]) / (polyY[j]-polyY[i]) * (polyX[j] - polyX[i]) < testX ) {    
        oddResult = !oddResult;
      }
    }
  j = i;
  } 
  return oddResult;
}


Rektal schrieb am 15.01.2011 um 10:45

lol :-)


Ringding schrieb am 16.01.2011 um 01:02

http://en.wikipedia.org/wiki/Rubber_duck_debugging




overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2025