Here is a solution if you have finite lines. It doesn't have the algorithm from scratch as it uses shapely, but it works. Plus, since its python, should be faster to use library anyway.
import math
import shapely.geometry.linestring
from matplotlib import pyplot as plt
from math import pi, cos, sin
from random import random
#make 2 random points inside unit circle
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return str((self.x, self.y))
class Circle:
def __init__(self, origin, radius):
self.origin = origin
self.radius = radius
origin = Point(0, 0)
radius = 1
circle = Circle(origin, radius)
vector_points=[]
for i in range(2):
p = random() * 2 * math.pi
r = circle.radius * math.sqrt(random())
vector_points.append((math.cos(p) * r,math.sin(p) * r))
#make random points on edge of unit circle
def point(h, k, r):
theta = random() * 2 * pi
return h + cos(theta) * r, k + sin(theta) * r
#math functions
def line_intersection(line1, line2):
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff, ydiff)
if div == 0:
raise Exception('lines do not intersect')
d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return x, y
if __name__ == '__main__':
#vizualize unit circle
xy = [point(0,0,1) for _ in range(3000)]
#two points on edge of unit circle, display with line
line=[point(0,0,1) for _ in range(2)]
#make vector inside unit circle, display with line
vector=vector_points
#display everything
plt.axes().set_aspect('equal')
plt.scatter(*zip(*xy), color='b')
plt.plot(*zip(*line), color='r')
plt.plot(*zip(*vector_points),color='g')
plt.show()
#check intersection of finite lines
check=shapely.geometry.linestring.LineString
line1=check(vector)
line2=check(line)
if line1.intersects(line2):
print(line_intersection(vector,line))
else:
print('lines do not intersect')