project_drawing/spiderweb.py

56 lines
1.7 KiB
Python

from turtle import *
from superturtle import *
from superturtle.movement import *
segment_positions = []
def draw_web_line(side_length):
global segment_positions
fly(-350, 350)
for side in range(5):
draw_segmented_line(side_length)
right(22.5)
fly(-350, 350)
#right(22.5)
def draw_segmented_line(side_length):
"Draws a line with 4 equal segments and marks the points"
segment_length = side_length / 4
line_segments = []
for segment in range(4):
forward(segment_length)
mark_point() # Mark the points at each segment
line_segments.append(pos())
segment_positions.append(line_segments)
def mark_point():
"Marks a point at the current location"
dot(5, "black") # Draw a dot to mark the segment points
def connect_segments():
"Connects the corresponding segment points of adjacent lines"
global segment_positions
for i in range(4): # Iterate over the 4 segments
for j in range(5): # Iterate over the 5 lines
next_j = (j + 1) % 5 # To wrap around to the first line after the fifth
# Skip connections if either segment is near the x-axis or y-axis
if segment_positions[j][i][0] == -350 or segment_positions[next_j][i][1] == 350:
continue
penup()
goto(segment_positions[j][i]) # Go to the current segment
pendown()
goto(segment_positions[next_j][i]) # Draw a line to the next segment
# Main function to draw the web
def draw_web(side_length):
draw_web_line(side_length)
connect_segments()
# Draw the web lines
# draw_web(300)
# hideturtle()
# done()