I am creating a script that visualises (or is supposed to) the most amount of intersections given an amount of lines using turtle in python. For some reason, the part of the code that is supposed to highlight all of the crossovers only highlights a few of them. I have experimented with loops, however to no luck. I don't know if it's an issue with the code that grabs the coordinates of intersections or if my loop just wasn't formed properly but it just doesn't (completely) work. My code is below:
import turtle
def find_intersection( p0, p1, p2, p3 ) :
s10_x = p1[0] - p0[0]
s10_y = p1[1] - p0[1]
s32_x = p3[0] - p2[0]
s32_y = p3[1] - p2[1]
denom = s10_x * s32_y - s32_x * s10_y
if denom == 0 : return None # collinear
denom_is_positive = denom > 0
s02_x = p0[0] - p2[0]
s02_y = p0[1] - p2[1]
s_numer = s10_x * s02_y - s10_y * s02_x
if (s_numer < 0) == denom_is_positive : return None # no collision
t_numer = s32_x * s02_y - s32_y * s02_x
if (t_numer < 0) == denom_is_positive : return None # no collision
if (s_numer > denom) == denom_is_positive or (t_numer > denom) == denom_is_positive : return None # no collision
# collision detected
t = t_numer / denom
intersection_point = [p0[0] + (t * s10_x), p0[1] + (t * s10_y)]
return intersection_point
def max_crossovers(num):
segment_points=[]
t = turtle.Turtle()
t.speed(0) # 1:slowest, 3:slow, 5:normal, 10:fast, 0:fastest
t.width(2.5)
count=0 # Keeps track of how many iterations of the loop have taken place.
while count < num:
t.left(360/(num)) # Turns the required angle
t.forward(20*num)
t.backward(40*num) # Gives leeway for lines to crossover
pA=list(t.pos()) # obtain x,y for point A
t.forward(60*num)
pB=list(t.pos()) # obtain x,y for point B
t.backward(30*num) # Prepares turtle in position for the next iteration
segment_points.append([pA, pB])
count+=1
print(segment_points)
# THIS PART ONWARDS NEEDS TO BE IN A LOOP
t.goto(find_intersection(segment_points[0][0], segment_points[0][1], segment_points[0+1][0], segment_points[0+1][1])[0], find_intersection(segment_points[0][0], segment_points[0][1], segment_points[0+1][0], segment_points[0+1][1])[1]) # go to the position of an intersection
t.pencolor("red") # begin process of drawing a circle
t.penup()
t.left(90)
t.backward(10)
t.right(90)
t.pendown()
t.circle(10)
t.penup()
t.left(90)
t.forward(10)
t.right(90)
# UNTIL HERE
max_crossovers(5) # Input number of lines to give the program.
I have tried experimenting with loops and to no luck. Maybe the issue is that the function to detect intersections doesn't work properly otherwise I might just have implemented the loop incorrectly?