![](/img/trans.png)
[英]How do I find the slope (m) for a line given a point (x,y) on the line and the line's angle from the y axis in python?
[英]Python: find a x,y coordinate for a given point B using the distance from the point A (x0, y0) and the angle
是否已经在 Python 中实现了一个函数来使用以度数 (°) 表示的给定角度从点 A (x0, Y0) 找到点 B 的 X、Y?
from math import cos, sin
def point_position(x0, y0, dist, theta):
return dist*sin(theta), dist*cos(theta)
其中x0
= A 点的 x 坐标, y0
= A 点的 y 坐标, dist
= A 和 B 之间的距离, theta
= B 点相对于指南针测量的北 (0°) 的角度 (°)
您只需要一个将度数转换为弧度的函数。 那么你的功能就变成了:
from math import sin, cos, radians, pi
def point_pos(x0, y0, d, theta):
theta_rad = pi/2 - radians(theta)
return x0 + d*cos(theta_rad), y0 + d*sin(theta_rad)
(如您所见,您在原始函数中混淆了正弦和余弦)
(还要注意线性角度变换,因为罗盘上的角度是顺时针方向,数学角度是逆时针方向。各个零点之间也有偏移)
您还可以使用复数来表示点,这比坐标元组要好一些(尽管专用的Point
类会更合适):
import cmath
def point_pos(p, d, theta):
return p + cmath.rect(d, pi/2-radians(theta))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.