绘制一条线,给出角度和线上的一个点 [英] Drawing a line given the angle and a point on the line
问题描述
在上面的图片中,我将三角形中的蓝线角度绘制到法线上。另外,三角形的尖端是已知的,这是法线上唯一已知的点。
我的python函数代码如下。这段代码绘制了一条线,通过尖端,但不一定是NORMAL到蓝线。
def draw_intercepts(img,triangle) :
tip = triangle [tip]
x1 = tip [0]
y1 = tip [1]
arrow_angle = triangle [arrow_angle]
y_intercept = int(y1 +((1 / np.tan(arrow_angle))* x1))
x_intercept = int(x1 +(np.tan(arrow_angle)* y1))
cv2.line(img,( x_intercept,0),(0,y_intercept),[0,0,255],3,cv2.LINE_AA)
<这个代码是通过回答这篇文章写的:
https://math.stackexchange.com/questions/2381119/how-to-find-slope-x-and-y-截取给定的角度到法线向量和a-poi
注意:
我更新了推荐的代码:
def draw_intercepts(img,triangle):
tip = triangle [tip]
x1 =尖端[0 ]
y1 = tip [1]
arrow_angle = triangle [arrow_angle]
arrow_angle_rad = np.radians(arrow_angle)
y_intercept = int(y1 +((1 / np。 tan(arrow_angle_rad))* x1))
x_intercept = int(x1 +(np.tan(arrow_angle_rad)* y1))
cv2.line(img,(x_intercept,0),(0,y_intercept) ,[0,0,255],3,cv2.LINE_AA)
解决了这个问题。
现在当arrow_angle处于第一或第三象限时,线条被完美绘制,例如0 < arrow_angle< 90和180℃ arrow_angle<但是在第二和第四象限(90 <箭头<180°和270 <箭头<360°)的线角度没有画出正确的角度或位置。即使我不知道它是在某处绘制的,因为它在图像中是不可见的。
c = -Sin(arrow_angle)
s = Cos (arrow_angle)
如果 arrow_angle
红线:
c = Cos(arrow_angle)
s = Sin(arrow_angle)
$ c
然后通过点划线
(x1-c * 4096,y1-s * 4096)和(x1 + c * 4096,y1 + s * 4096)
(我使用与屏幕大小相当的任意大常量)
In my image I have a triangle (representing an arrow). This arrow defines the direction and area under consideration for further search in same image. For example if I have a triangle rotated at 30 degree w.r.t x-axis and it's tip is located at (250,150) in the image. I would want to find and draw a line normal the tip of triangle, as shown in the image below.
In the image above, I have the angle of blue line in the triangle, to which the normal is to be drawn. Also the tip of the triangle is known which is the only point known on the normal line.
My code for the python function is given below. This code draws a line, passing through the tip but not necessarily NORMAL to the blue line.
def draw_intercepts(img,triangle):
tip=triangle["tip"]
x1=tip[0]
y1=tip[1]
arrow_angle=triangle["arrow_angle"]
y_intercept=int(y1+((1/np.tan(arrow_angle))*x1))
x_intercept=int(x1+(np.tan(arrow_angle)*y1))
cv2.line(img,(x_intercept,0),(0,y_intercept),[0,0,255],3,cv2.LINE_AA)
This code is written by following an answer to this post:
https://math.stackexchange.com/questions/2381119/how-to-find-slope-x-and-y-intercepts-given-angle-to-the-normal-vector-and-a-poi
Note:
I updated the code as recommended:
def draw_intercepts(img,triangle):
tip=triangle["tip"]
x1=tip[0]
y1=tip[1]
arrow_angle=triangle["arrow_angle"]
arrow_angle_rad=np.radians(arrow_angle)
y_intercept=int(y1+((1/np.tan(arrow_angle_rad))*x1))
x_intercept=int(x1+(np.tan(arrow_angle_rad)*y1))
cv2.line(img, (x_intercept, 0), (0, y_intercept), [0, 0, 255], 3, cv2.LINE_AA)
This resolved the issue.
Now the line is drawn perfectly when the arrow_angle is in 1st or 3rd quadrant e.g. 0 < arrow_angle < 90 and 180 < arrow_angle < 270 but in 2nd and 4th quadrant ( 90 < arrow_angle < 180 and 270 < arrow_angle < 360) line angle is not drawn as correct angle or position. Even I don't know it line is drawn somewhere because it is not visible in image.
解决方案 Note that used line equation "in intercept segments" is not universal - does not work for horizontal and vertical lines. With intercept approach cases 0, Pi/2, Pi, 3*Pi/2 or -Pi/2 must be treated separately.
If you arrow_angle
is for blue line:
c = -Sin(arrow_angle)
s = Cos(arrow_angle)
If you arrow_angle
is for red line:
c = Cos(arrow_angle)
s = Sin(arrow_angle)
Then draw line through points
(x1 - c * 4096, y1 - s * 4096) and (x1 + c * 4096, y1 + s * 4096)
(I used arbitrary large constant comparable with screen size)
这篇关于绘制一条线,给出角度和线上的一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!