随机循环路径生成器

问题描述

我正在尝试设计一种算法,该算法将允许用户将以米为单位的距离定义为输入参数,然后返回随机圆弧路线的坐标(从用户当前位置返回到相同位置) 。坐标将使用Google Directions API进行绘制,并作为路线在Google地图上显示给用户。

虽然我可以在生成坐标后将其绘制出来,但我仍在研究一种首先生成这些坐标的最佳方法。

我的问题是:

  1. 这是一个需要新颖解决方案的问题吗?
  2. 如果需要新颖的解决方案,是否有人对如何有效实现这一想法有想法 准确吗?

谢谢!

解决方法

忽略地球的曲率,并假设用户在赤道上

您需要做的第一件事是为您的圈子选择一个随机中心。输入位置在圆的圆周上,因此中心在随机方向上相距1个半径。因此,通过随机均匀地选择一个方向,我们可以随机均匀地选择一个中心。

radius = circumference/(2*pi)
direction = 2*pi*random()                      # random() is uniform between 0-1
center_x = user_x + radius*cos(direction)
center_y = user_y + radius*sin(direction)

现在,您既有半径又有圆心。您可以通过使angle在0和2pi之间变化来生成圆周上的点,并为每个角度生成:

point_x = center_x + radius*cos(angle)
point_y = center_y + radius*sin(angle)

提示:如果需要生成从用户位置开始的点,而不是将角度从0更改为2pi,请将其从direction - pi更改为direction + pi。如果要改变沿圆运动的方向(顺时针或逆时针),请沿相反方向改变角度。

,

因此,到目前为止,我提出的解决方案如下:

  1. 在极坐标中生成围绕圆的点的列表。 圆将是路线的距离。起点/终点位于 周长。如下图所示,红点表示起点和终点。

    List of points around a circle

  2. 通过从点上每个点的半径减去一个随机数来应用随机噪声 圈。随机数应在半径的0到80%之间,以避免 路线偏向中心。

  3. 根据随机的极坐标生成多边形。

  4. 缩放随机多边形,使其周长与 原圈。参见下图,蓝线显示生成的原始多边形, 绿线显示缩放的版本(周长与原始版本大致相似 圈)。

    Randomised polygons

  5. 尚未实现,但随后会将每个点的坐标转换为 纬度和经度,使用Google Directions API绘制路线。

这是我用来实现此目的的代码示例:

numberOfEdges = 20  # Number of edges on polygon
radius =  1  # Radius (polar co-ordinates)
deltaTheta = (2 * math.pi) / numberOfEdges  # Change in angle per point (polar co-ordinates)

# For each delta theta defining a list of X and Y co-ordinates for the unit circle polygon and
# generating a random polygon by varying the radius  
currentTheta = 0
coordsPolygon = [[1,0]] 
coordsRandomPolygon = [[1,0]]
perimeterPolygon = 0
perimeterRandomPolygon = 0
randomRadiusLst = []
for i in range(numberOfEdges):    
    currentTheta += deltaTheta
    randomRadius = radius - random.uniform(0,(0.8 * radius))
    randomRadiusLst.append(randomRadius)

    if (i ==  (numberOfEdges - 1)):
        coordsPolygon.append([1,0])
        coordsRandomPolygon.append([1,0])
    else:
        coordsPolygon.append([radius * math.cos(currentTheta),radius * math.sin(currentTheta)])
        coordsRandomPolygon.append([randomRadius * math.cos(currentTheta),randomRadius * math.sin(currentTheta)])

    # Distance between points for unit circle polygon and random polygon 
    perimeterPolygon += math.sqrt((coordsPolygon[i][0]-coordsPolygon[i-1][0])**2 + (coordsPolygon[i][1]-coordsPolygon[i-1][1])**2) 
    perimeterRandomPolygon += math.sqrt((coordsRandomPolygon[i][0]-coordsRandomPolygon[i-1][0])**2 + (coordsRandomPolygon[i][1]-coordsRandomPolygon[i-1][1])**2)


# Scaling the random polygon to the perimeter of the original unit circle
scalingFactor = perimeterPolygon / perimeterRandomPolygon
currentTheta = 0
coordsRandomPolygonScaled = [[1,0]]
perimeterScaledRandomPolygon = 0
for i in range(numberOfEdges):
    currentTheta += deltaTheta
    if (i ==  (numberOfEdges - 1)):
        coordsRandomPolygonScaled.append([1,0])
    else:
        coordsRandomPolygonScaled.append([(randomRadiusLst[i] * scalingFactor) * math.cos(currentTheta),(randomRadiusLst[i] * scalingFactor) * math.sin(currentTheta)])

    perimeterScaledRandomPolygon += math.sqrt((coordsRandomPolygonScaled[i][0]-coordsRandomPolygonScaled[i-1][0])**2 + (coordsRandomPolygonScaled[i][1]-coordsRandomPolygonScaled[i-1][1])**2)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...