'Find the most optimal coordinates for 45 degree line
I am trying to put together a python script that will allow me to find the optimal starting and ending coordinates for a line between two lines that are not always parallel.

x1,x4,y1,y4 and a,b will be given. but i need to determine x2,x3,y2,y3 that will result in a 45 degree line relative to lines m1 and m3.
I have put together the below code to determine the angle for every possible combo of x2,x3,y2,y3. but i think there is a better way to do this.
angle is determining the angle using the principal tan(a) = (m2-m1)/(1+(m2*m1)) and tan(b) = (m3-m2)/(1+(m3*m2))
import math
import numpy as np
def angle(x2,x3,y2,y3):
return abs(int(math.tanh(((y3-y2)/(x3-x2))-((y2-y1)/(x2-x1))/(1+((y2-y1)/(x2-x1))-((y3-y2)/(x3-x2))))/(3.14159265359/180)))
def coor(x1,x2,x3,x4,y1,y2,y3,y4):
if angle(x2,x3,y2,y3)==45 and angle(x3,x4,y3,y4)==45:
print(True)
return True
else:
print(angle(x2,x3,y2,y3),angle(x3,x4,y3,y4))
return False
def line(a, x,y1,x1):
return math.tan(a)*(x-x1)-y1
y1,x1 = 50,20
y4,x4 = 1000,2000
x2 = np.arange(x1-200,x1+200,1)
y2 = line(350,x2,y1,x1)
x3 = np.arange(x4-200,x4+200,1)
y3 = line(20,x3,y4,x4)
for i in x2:
for j in y2:
for k in x3:
for l in y3:
coor(x1,i,k,x4,y1,j,l,y4)
if coor(x1,i,k,x4,y1,j,l,y4) == True:
d,f,g,h = i,k,j,l
print("x2 = {},x3 = {},y2 = {}, y3 = {}".format(d,f,g,h))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
