[英]How to calculate pixel coordinates having start (x,y) , length in pixel and angle?
line(x1,y1,x2,y2)
where x1,y1 is start the and x2,y2 is end line coordinates.其中 x1,y1 是起点,x2,y2 是终点线坐标。 I have a different task where I have x1,y1 and line length in pixels with angle of direction.我有一个不同的任务,我有 x1、y1 和线长度(以像素为单位)和方向角。 How to calculate x2,y2 having those data only?如何计算只有这些数据的 x2,y2?
Thanks Erick谢谢埃里克
As per stackoverflow.com Admins advice I'm modifying this comment instead of posting answer separately below to provide code to my question plus one doubt that this code works with 0 (zero) angle drawing straight line towards right hand site as it should however 90 degree angle looks like more 110 or something as per picture.根据 stackoverflow.com 管理员的建议,我正在修改此评论,而不是在下面单独发布答案,以提供我的问题的代码,并怀疑此代码适用于 0(零)角度向右侧站点绘制直线,因为它应该是 90度角看起来更像 110 或像图片一样的东西。 What am I doing wrong ?我究竟做错了什么 ? Thanks again in advance.再次提前致谢。
import math from PIL import Image, ImageDraw def calc_x2y2(length, angle, x1,y1): x2 = x1 + math.cos(angle)*length y2 = y1 + math.sin(angle)*length return (x2,y2) w, h = 640, 640 x1,y1 = w/2, h/2 shape = [(x1,y1), calc_x2y2(300,90,x1,y1)] # creating new Image object img = Image.new("RGB", (w, h),(225,225,225)) # create line on the image img1 = ImageDraw.Draw(img) img1.line(shape, fill="#401eba", width=5) img.show()
Straight line that should be 90 degree except it's not应该是 90 度的直线,除非它不是
sin(θ) is perpendicular/hypotenuse and cos(θ) is base/hypotenuse . sin(θ) 是垂直/斜边,cos(θ) 是底/斜边。 So, this becomes a simple math's problem, where y2 = y1 + L.sin(θ) and x2 = x1 + L.cos(θ).因此,这变成了一个简单的数学问题,其中 y2 = y1 + L.sin(θ) 和 x2 = x1 + L.cos(θ)。 Hence, the new co-ordinates are:因此,新坐标为:
(y2 = y1 + L.sin(θ), x2 = x1 + L.cos(θ))
where L = length of line θ = angle between line and X - axis其中 L = 线的长度 θ = 线与 X 轴之间的角度 - 轴
You can make a right angle triangle with given information.你可以用给定的信息制作一个直角三角形。 from there we can see that从那里我们可以看到
sin(A) = p/h cos(A) = b/h
--- (1) sin(A) = p/h cos(A) = b/h
--- (1)
Where h
is length of line and A
is angle h
makes with b(base)
and p
is perpendicular
.其中h
是线的长度, A
是h
与b(base)
夹角, p
是perpendicular
。
We also know that我们也知道
X2 = X1+b and Y2 = Y1+p
--- (2) X2 = X1+b and Y2 = Y1+p
--- (2)
Replacing p and b in (2) with (1) you will get将(2)中的 p 和 b 替换为(1),您将得到
X2 = X1+cos(A)*h
Y2 = Y1+sin(A)*h
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.