I tried a math formula from analytic geometry but I didn't get the desired results.
As you can see in the code, the user draw two lines and a green circle(point of intersection) appear.
I have tried to fix the problem for hours but I failed. Sometimes the point doesn't appear or appear but in the wrong position.
Source of the formula I used in the code
Docs of pygame the library I used
#!/usr/bin/env python from pygame import * from time import sleep init(); win = display.set_mode((500,500)); lines = [] cords = [] preview = False xi = -100 yi = -100 def drawlines(flines): for fcords in flines: draw.line(win,(0,0,0),fcords[0],fcords[1],4) while True: win.fill((255,255,255)) for ev in event.get(): if ev.type == QUIT: quit(); exit(); elif ev.type == MOUSEBUTTONDOWN: if ev.button == 1: preview = True a = mouse.get_pos() cords.append(mouse.get_pos()) elif ev.type == MOUSEBUTTONUP: if ev.button == 1: cords.append(mouse.get_pos()) lines.append((cords)) cords = [] preview = False ######################################## THIS BROKEN PART ################################# if len(lines) == 2: #line 1 points_line1 = lines[0] point1_line1 = points_line1[0] point2_line1 = points_line1[1] line1_vector = (point2_line1[0]-point1_line1[0],point2_line1[1]-point1_line1[1]) a_line1 = line1_vector[1] b_line1 = -line1_vector[0] c_line1 = -((a_line1*point1_line1[0]) + (b_line1*point1_line1[1])) #line2 points_line2 = lines[1] point1_line2 = points_line2[0] point2_line2 = points_line2[1] line2_vector = (point2_line2[0]-point1_line2[0],point2_line2[1]-point1_line2[1]) a_line2 = line2_vector[1] b_line2 = -line2_vector[0] c_line2 = -((a_line2*point1_line2[0]) + (b_line2*point1_line2[1])) if (a_line2 != 0 and b_line2 != 0): if (a_line1 / a_line2) != ( b_line1/b_line2): #intersection between line1 and line2 yi = ((((a_line1*c_line2) / a_line2) + c_line1) / -b_line1) xi = (((-b_line1*yi)-c_line1) / a_line1) ########################################################################################### elif preview: draw.line(win,(0,0,0),a,mouse.get_pos(),4) drawlines(lines) draw.circle(win,(0,200,0),(int(xi),int(yi)),10) display.flip() 