2016-12-27 13 views
-1

私は、境界でランダムな座標を生成するメソッドが必要です。は境界でランダムな座標を生成する

private Coordinate[] Calculate(Coordinate location1, Coordinate location2, Coordinate location3, Coordinate location4) 

public class Coordinate 
{ 
    public double Latitude { set; get; } 
    public double Longitude { set; get; } 
} 

どのように指定された無線で座標を生成できるかわかりません。 enter image description here

+0

これは役立つかもしれませんhttp://gis.stackexchange.com/questions/15545/calculating-coordinates-of-square- x-mile-center-point – CoderHawk

+1

これまでに何をしていますか? – CoderHawk

+0

指定した半径の座標を生成するメソッドを作成しました。しかし、私は何かが境界線で動作したい。 – David

答えて

3

最小から最大まで座標を生成し、間違って除外します。私たちは、次のアプローチを使用できることはほとんど幾何学の知識を持つ

private Coordinate[] Calculate(Coordinate location1, Coordinate location2, Coordinate location3, 
     Coordinate location4) 
    { 
     Coordinate[] allCoords = {location1, location2, location3, location4}; 
     double minLat = allCoords.Min(x => x.Latitude); 
     double minLon = allCoords.Min(x => x.Longitude); 
     double maxLat = allCoords.Max(x => x.Latitude); 
     double maxLon = allCoords.Max(x => x.Longitude); 

     Random r = new Random(); 

     //replase 500 with your number 
     Coordinate[] result = new Coordinate[500]; 
     for (int i = 0; i < result.Length; i++) 
     { 
      Coordinate point = new Coordinate(); 
      do 
      { 
       point.Latitude = r.NextDouble()*(maxLat - minLat) + minLat; 
       point.Longitude = r.NextDouble()*(maxLon - minLon) + minLon; 
      } while (!IsPointInPolygon(point, allCoords)); 
      result[i] = point; 
     } 
     return result; 
    } 

    //took it from http://codereview.stackexchange.com/a/108903 
    //you can use your own one 
    private bool IsPointInPolygon(Coordinate point, Coordinate[] polygon) 
    { 
     int polygonLength = polygon.Length, i = 0; 
     bool inside = false; 
     // x, y for tested point. 
     double pointX = point.Longitude, pointY = point.Latitude; 
     // start/end point for the current polygon segment. 
     double startX, startY, endX, endY; 
     Coordinate endPoint = polygon[polygonLength - 1]; 
     endX = endPoint.Longitude; 
     endY = endPoint.Latitude; 
     while (i < polygonLength) 
     { 
      startX = endX; 
      startY = endY; 
      endPoint = polygon[i++]; 
      endX = endPoint.Longitude; 
      endY = endPoint.Latitude; 
      // 
      inside ^= ((endY > pointY)^(startY > pointY)) /* ? pointY inside [startY;endY] segment ? */ 
         && /* if so, test if it is under the segment */ 
         (pointX - endX < (pointY - endY)*(startX - endX)/(startY - endY)); 
     } 
     return inside; 
    } 
1

private Coordinate Calculate(Coordinate location1, Coordinate location2, Coordinate location3, 
     Coordinate location4) 
    { 
     Random random=new Random(DateTime.Now.Millisecond); 
     Coordinate randomCoordinate = new Coordinate() 
     { 
      Latitude = random.Next((int) Math.Floor(location4.Latitude), (int) Math.Floor(location2.Latitude)) 
     }; 
     if (randomCoordinate.Latitude > location1.Latitude) 
     { 
      double m1 = (location2.Longitude - location1.Longitude)/(location2.Latitude - location1.Latitude); 
      double m2 = (location2.Longitude - location3.Longitude)/(location2.Latitude - location3.Latitude); 
      double maxLongitude = (randomCoordinate.Latitude - location2.Latitude) *m1; 
      double minLongitude = (randomCoordinate.Latitude - location2.Latitude) *m2; 
      randomCoordinate.Longitude = random.Next((int) Math.Ceiling(minLongitude), (int) Math.Floor(maxLongitude)); 
     } 
     else 
     { 
      double m1 = (location4.Longitude - location1.Longitude)/(location4.Latitude - location1.Latitude); 
      double m2 = (location4.Longitude - location3.Longitude)/(location4.Latitude - location3.Latitude); 
      double maxLongitude = (randomCoordinate.Latitude - location4.Latitude) * m1; 
      double minLongitude = (randomCoordinate.Latitude - location4.Latitude) * m2; 
      randomCoordinate.Longitude = random.Next((int)Math.Ceiling(minLongitude), (int)Math.Floor(maxLongitude)); 
     } 
     return randomCoordinate; 
    } 
+1

実行時に実際の図形を知ることはできません。したがって、あなたは多くのifを使うべきです。たとえば、location1が左にはないが下にある場合は?時計回りかどうか?それは非常に複雑なことがあります。 – RusArt

+0

これは質問のテストケースでのみ機能します。ポリゴンの形状があらかじめ定義されていない場合、私はあなたのアプローチを使用することをお勧めします。ただし、あらかじめ定義されたポリゴンを使用すると便利ではありません –

関連する問題