How to draw a perpendicular line from the terminal point of another line in python? [duplicate]

本秂侑毒 提交于 2020-12-15 04:32:07

问题


I'm trying to draw a line segment orthogonal/perpendicular to the current line segment from the terminal point for a given length, here's an illustration to help better explain the problem:

Given the line a coordinates and an arbitrary length, I'd like to find the coordinates for line segment b and (x3,y3).

Appreciate any help.

UPDATE: Found my solution here and adapted it to Python, mods please mark this as duplicate and close it.


回答1:


I think, it would be easy to use sympy module and get it.

import sympy.geometry as gm
line1=gm.Line(gm.Point(1,2),gm.Point(5,4))
line2=line1.perpendicular_line(line1.p2)

line1 - is the initial line ( equation- -2x + 4y - 6) line2- is the perpendicular line drawn at the endpoint (5,4) (equation - -4x - 2y + 28)

Pleas have a look at https://docs.sympy.org/latest/modules/geometry/lines.html for line segments in detail.




回答2:


I found the answer here in C# and here's the python code I came up with in case anyone is interested:

import math
# sample line and length
line_a = ([4, 4], [2, 2])
length = 2
initial_pt_x = line_a[0][0]
initial_pt_y = line_a[0][1]
terminal_pt_x = line_a[1][0]
terminal_pt_y = line_a[1][1]

dx = initial_pt_x - terminal_pt_x
dy = initial_pt_y - terminal_pt_y

perp_angle = math.atan2(dy, dx)

x = terminal_pt_x + math.sin(perp_angle) * length
y = terminal_pt_y - math.cos(perp_angle) * length
# For the opposite direction
x = terminal_pt_x - math.sin(perp_angle) * length
y = terminal_pt_y + math.cos(perp_angle) * length


来源:https://stackoverflow.com/questions/64843551/how-to-draw-a-perpendicular-line-from-the-terminal-point-of-another-line-in-pyth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!