Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 生成具有边界的随机坐标_C#_.net_Math_Location_Coordinates - Fatal编程技术网

C# 生成具有边界的随机坐标

C# 生成具有边界的随机坐标,c#,.net,math,location,coordinates,C#,.net,Math,Location,Coordinates,我想要一种方法来生成带有边界的随机坐标。大概是这样的: private Coordinate[] Calculate(Coordinate location1, Coordinate location2, Coordinate location3, Coordinate location4) public class Coordinate { public double Latitude { set; get; } public double Longitude { set;

我想要一种方法来生成带有边界的随机坐标。大概是这样的:

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

public class Coordinate
{
    public double Latitude { set; get; }
    public double Longitude { set; get; }
}
我不知道如何使用指定的无线电生成坐标。

只需生成从最小到最大的坐标,并排除错误

    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、坐标位置2、坐标位置3、,
坐标位置(4)
{
坐标[]allCoords={location1,location2,location3,location4};
double minLat=allCoords.Min(x=>x.纬度);
double minLon=allCoords.Min(x=>x.Longitude);
double maxLat=allCoords.Max(x=>x.lation);
double maxLon=allCoords.Max(x=>x.Longitude);
随机r=新随机();
//用你的号码重播500
坐标[]结果=新坐标[500];
for(int i=0;ipointY)^(startY>pointY))/*?在[startY;endY]段内有点*/
&&/*如果是,测试它是否在该段下*/
(pointX-endX<(pointY-endY)*(startX-endX)/(startY-endY));
}
返回内部;
}

只要有一点几何知识,我们就可以使用以下方法:

 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;
    }

这可能有助于您目前所做的工作?我编写了一个方法来生成具有指定半径的坐标。但我想让一些东西和边界一起工作。分享这一点可能是明智的,否则这个问题会变得太广泛,你们无法知道运行时图形的真正形式。因此,您应该使用大量的if。例如,如果位置1不在左侧,而是在下方?它们是顺时针还是逆时针?它可能非常复杂。这只适用于问题的测试用例。如果多边形的形状没有预定义,我建议使用您的方法。然而,对于一个预定义的多边形,使用它并不方便。我想指出的是,在试图在多边形中生成点时,它似乎会无限循环。可能与最小/最大纬度有关?