歌者文明清理员 发表于 2023-6-29 15:18:33

【数学笔记】2. 计算圆上一点的坐标

参考链接:传送门

首先,确定一个圆需要确定它其中一个点的坐标(左上,左,左下,上,中心,下,右上,右,右下)以及它的半径 radius。

于是代码可以这么写:

from math import cos, sin, radians

def getpos(radius, topleft, angle):
    pass


又因为我们需要将 top 和 left 分开考虑,所以

from math import cos, sin, radians

def getpos(radius, topleft, angle):
    left, top = topleft


说明:radius 表示圆的半径,topleft 表示圆左上角坐标 (x, y),angle 表示以穿过圆中心的横线为基准,旋转了几度(顺时针为正数,逆时针为负数)。

下一步是获取圆心坐标。设圆心坐标为 (x0, y0),那么可以确定:

x0 = left + radius
y0 = top + radius

接下来就可以计算坐标了。非常重要的一点是角度和弧度的区别(传送门),需要用 radians() 或者 degrees() 进行角度 <-> 弧度换算。

最终代码如下:

from math import cos, sin, radians

def getpos(radius, topleft, angle):
    left, top = topleft
    x0, y0 = left + radius, top + radius
    x1 = x0 + radius * cos(radians(angle))
    y1 = y0 + radius * sin(radians(angle))
    return x1, y1


一种更简单的情况是,给定的坐标不是 topleft,而是 center。

Code:

from math import cos, sin, radians

def getpos(radius, center, angle):
    x0, y0 = center
    x1 = x0 + radius * cos(radians(angle))
    y1 = y0 + radius * sin(radians(angle))
    return x1, y1
页: [1]
查看完整版本: 【数学笔记】2. 计算圆上一点的坐标