2017-09-09 2 views
-1

Mein Problem ist zu wissen, ob ein Punkt in einem Polygon (Gruppen von Punkten) auf der gleichen Oberfläche enthalten ist oder nicht.Geometrie im Plan (Mathematik)

Einfaches Beispiel in meiner Lieblingssprache DART

Point myPoint = new Point(10, 12); 

List<Point> myPolygon = [ // The Polygon, for example is simple rect 
    new Point(2, 7), 
    new Point(15,7), 
    new Point(15, 18), 
    new Point(2, 18) 
]; 

bool pointIsContainedInPolygon(List Polygon){ 
    // .. data processing ... 
} 

ich wissen muss, was die Funktion ist: pointIsContainedInPolygon(myPolygon)

+1

Ist es eine Hausaufgabe? – batMan

+0

Nein, es ist keine Hausaufgaben natürlich, ich bin nicht gut in Geometrie – Chr

+0

Es gibt viele Web-Ressourcen für "Punkt in Polygon". Haben Sie danach gesucht? Fehlt etwas von all diesen Webseiten? Brauchst du wirklich eine Routine in Dart, die dir gegeben wird - kannst du es nicht aus einer anderen Sprache portieren? –

Antwort

2

ich die Codedaten in der How can I determine whether a 2D Point is within a Polygon? Post in dart wieder aufgenommen haben, hier ist das Ergebnis (getestet)

bool pnpoly(Point point, List<Point> polygon){ 
    // Step 1: Cut and detail 

    int nvert = polygon.length; 
    List<int> vertx = []; 
    List<int> verty = []; 

    for(Point vert in polygon){ // Listing x and y pos of all vertices 
     vertx.add(vert.x); 
     verty.add(vert.y); 
    } 

    // Step 2: Calcul.. 
    bool c = false; 
    int j = nvert-1; 
    for (int i = 0; i < nvert; j = i++){  
     if(((verty[i]>point.y) != (verty[j]>point.y)) && (point.x < (vertx[j]-vertx[i]) * (point.y-verty[i])/(verty[j]-verty[i]) + vertx[i])){ 
      c = !c; 
     } 
    } 
    return c; 
} 

Hier ist mein Test

List<Point> myPolygon = [ // classic rectangle 
    new Point(2,2), 
    new Point(52,2), 
    new Point(52,41), 
    new Point(2,41) 
]; 

Point myPoint = new Point(53,40); 

print(pnpoly(myPoint, myPolygon)); // false 
+0

Nur ein Tipp über diese Lib, vom Dart-Team und ausgereift. Enthält auch zusätzliche Funktionen für Web-Gl. https://pub.dartlang.org/packages/vector_math –