简体   繁体   English

如何计算具有起点 (x,y) 、像素长度和角度的像素坐标?

[英]How to calculate pixel coordinates having start (x,y) , length in pixel and angle?


I have an image 640 x 640 and I'd like to draw the line on it. 我有一张 640 x 640 的图像,我想在上面画一条线。 Common task where package like PIL allows me to write a line using 像 PIL 这样的包允许我写一行的常见任务

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是线的长度, Ahb(base)夹角, pperpendicular

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.

相关问题 对于线长的(x,y)像素坐标,调整角度变成圆形 - For (x, y) pixel coordinates in length of line, adjust angle to become circle 在特定的 x,y 坐标处绘制一个像素 - Draw a pixel at specific x,y coordinates 如何将 2D 边界框像素坐标 (x, y, w, h) 转换为相对坐标(Yolo 格式)? - How to convert 2D bounding box pixel coordinates (x, y, w, h) into relative coordinates (Yolo format)? 是否可以通过X和Y坐标获取屏幕上特定像素的颜色? - Is it possible to get the color of a particular pixel on the screen with it's X and Y coordinates? 在 Python 中从图像中提取每个像素的 x,y 坐标 - Extract x,y coordinates of each pixel from an image in Python 将像素 x,y 坐标转换为 wgs84 - convert pixel x,y coordinates to wgs84 将像素 X 与像素 Y 进行比较,直到像素 Y 发生变化,然后执行……关于如何减少延迟的任何提示? - Compare pixel X with pixel Y until pixel Y changes, then do… Any tips on how to reduce lag? 通过角度和长度计算线的端点坐标 - Calculate coordinates of end point of a line by having the angle and the length 如何使用 Excel 电子表格中的 x/y/r 像素坐标在 Python 中裁剪多个图像? - How to I crop multiple images in Python using x/y/r pixel coordinates in an Excel spreadsheet? 如何使用python获取某些颜色(RGB)的像素坐标[x,y]列表? - How to get list of pixel coordinates[x, y] of certain color(RGB) with python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM