![](/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.