【问题标题】:Python - How do I find the x and y coordinates of the circle to create an arc on a mapPython - 如何找到圆的 x 和 y 坐标以在地图上创建弧线
【发布时间】:2021-04-13 11:02:45
【问题描述】:

我正在编写一个 python 脚本,它将根据我收到的数据映射一些点并在地图上弯曲。我正在尝试使用我在下面收到的任何数据来获取圆弧/圆的中心坐标。我很难把我的大脑包裹在它周围。

  • Point1 经纬度
  • Point2 经纬度
  • 起始角度
  • 结束角度
  • 圆的半径

我确实意识到我不需要所有这些数据来解决这个问题。

我还有一面旗帜,告诉我是左转还是右转。

根据我上面收到的信息,是否可以计算 x 和 y 线。

我已经有一个在 javascript 中运行的函数来绘制弧线。

function drawArc(center, initialBearing, finalBearing, radius) {{
    var d2r = Math.PI / 180;   // degrees to radians 
    var r2d = 180 / Math.PI;   // radians to degrees 

    var points = 32; 

    // find the raidus in lat/lon 
    var rlat = (radius / EarthRadiusMeters) * r2d;
    var rlng = rlat / Math.cos(center.lat() * d2r); 

    var extp = new Array();

    if (initialBearing > finalBearing) finalBearing += 360;
    var deltaBearing = finalBearing - initialBearing;
    deltaBearing = deltaBearing/points;
    for (var i=0; (i < points+1); i++) 
    {{ 
      extp.push(center.DestinationPoint(initialBearing + i*deltaBearing, radius)); 
      bounds.extend(extp[extp.length-1]);
    }}
    return extp;
}}

我一辈子都找不到获得圆心的 x 和 y 坐标的好方法。

【问题讨论】:

  • 我想指出的是,你把顺时针度数设为正数让我的大脑感到不安。
  • math.stackexchange 中存在此类问题的几个答案。 Example.
  • 我现在会调查这些答案。好像他们可能有我需要的东西。虽然其中一些仍不清楚。
  • @CoryKramer:许多图形库将“原点”定义为屏幕的左上角,并且“y”轴指向下方。在这种情况下,顺时针为正度数是有意义的,因为整个图形从“传统”轴方向镜像到 x 轴。
  • @JakeGriffin 有趣。我来自工程/物理背景,所以我习惯于向上/向右/向前是积极的,而 CCW 是积极的。

标签: javascript python google-maps


【解决方案1】:

给定一个点 (Px, Py),该点的起始角度 (Pa) 和半径 (r),您可以像这样计算中心 (Cx, Cy):

Cx = Px - r * cos(Pa)
Cy = Py - r * sin(Pa)

例如,如果您想要圆弧的中心点位于 (1, 2) 处,该点位于半径为 3 的圆上 Pi/3 弧度(60 度),您可以像这样计算圆心:

Cx = 1 - 3 * cos(Pi/4) = 1 - 3 * 0.5 = 1 - 1.5 = -0.5
Cy = 1 - 3 * sin(Pi/4) = 1 - 3 * sqrt(2)/2 = -1.1213
C = (-0.5, -1.1213)

根据其他信息,您也可以使用类似的公式。例如,您可以在给定“第一”和“第二”点、半径和“转弯方向”(左或右)的情况下计算中心。推导出这些方程是相当简单的三角函数。

【讨论】:

  • 这适用于球体地图吗?我的曲线被随机放置在地图上。
【解决方案2】:

AFAIK,中心点不依赖于圆的参数。它是画布上的 XY 坐标。它可以在图片中间的任何地方或更频繁地看起来更好看。

看这里:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import cairo, math

radius=100

starting_angle = math.radians(210) #  angle in radians
ending_angle = math.radians(330)

WIDTH, HEIGHT = radius*2+20, radius*2+20 # Canvas size

x=WIDTH/2 # X position
y=HEIGHT/2 # Y position


surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
context = cairo.Context (surface)

# Background Neutral Gray ----------
context.set_source_rgb(0.25, 0.25, 0.25)
context.rectangle(0, 0, WIDTH, HEIGHT)
context.fill()


# Red Sector -------------------
context.set_source_rgb(1, 0, 0) # rgb color red
context.arc(x, y, radius, starting_angle, ending_angle)
context.line_to (x, y)
context.fill()


surface.write_to_png ("img.png") # Output to PNG

【讨论】:

  • 我正在尝试获取地图上点之间的弧线。所以我需要根据这些点得到正确的 x 和 y 坐标。您已根据画布的宽度和高度对中心点进行硬编码。
  • 好吧。我说中心点可以在任何地方。我想你的地图在 XY 维度上是平的?因为我没有任何地图,所以我围绕圆圈制作了画布。只需在地图上找到该点的 XY 坐标并画一个圆圈
  • 我明白了。 2D 不太适用于 3D。总有一些不匹配的地方。
猜你喜欢
  • 2020-05-16
  • 2021-10-05
  • 1970-01-01
  • 2016-09-17
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多