import cv2
import numpy as np
import math

def calcX(p3x, p3y, A, B, y):
    return int(p3x - ((p3y - y) * A / B))

def calcY(p3y, A, B, L):
    a = L * B
    b = (A * A) + (B * B)
    return int((a / math.sqrt(b)) + p3y)

def calcCollinearX(line, p3y):
    p1x, p1y, p2x, p2y = line
    return int(((p3y - p2y) * p1x + (p1y - p3y) * p2x) / (p1y - p2y))

def calcCollinearY(line, p3x):
    p1x, p1y, p2x, p2y = line
    return int(((p3x - p1x) * p2y + (p2x - p3x) * p1y) / (p2x - p1x))

img = np.zeros((600, 600, 3), dtype = int)
img = np.array(img,dtype='uint8')

point1 = (100,100)
point2 = (500,500)
point3 = (360,320)

cv2.line(img, point1, point2, (0,0,255), 1)
cv2.circle(img, point3, 3, (0,255,0), -1)
cv2.imshow('img1', img)

p1x, p1y = point1
p2x, p2y = point2
p3x, p3y = point3

A = p1x - p2x
B = p1y - p2y
L = 100

y = calcY(p3y, A, B, L)
x = calcX(p3x, p3y, A, B, y)
point4 = (x, y)
cv2.circle(img, point4, 3, (255,255,0), -1)
cv2.line(img, point3, point4, (255, 0, 0), 1)

n3_y = point3[1]
n3_x = calcCollinearX((*point1, *point2), n3_y)
n4_x = point4[0]
n4_y = calcCollinearY((*point1, *point2), n4_x)
cv2.circle(img, (n3_x, n3_y), 3, (0,255,0), -1)
cv2.circle(img, (n4_x, n4_y), 3, (255,255,0), -1)



cv2.imshow('img2', img)

cv2.waitKey(0)
