'How can I determine whether a line segment is completely covered by a set of smaller segments?

I am trying to write an algorithm using turtle. The turtle is bouncing in a polygon, and painting a portion of the edge it touches. The algorithm should terminate once there are no unpainted areas left on the boundary of the polygon.

In order to do that, I need to check every edge of the polygon and determine whether it is completely painted.

Is there such a function in turtle library? If not, then what is a simple method to determine this? The polygon is not necessarily rectilinear.

Here is a MWE:

import turtle
from sympy import Segment, Point, Ray, N
from mpmath import radians

turtle.setworldcoordinates(-1, -1, 11, 11)
paint_len = 1


        


def paint(e, hit):
    print(hit)
    turtle.tracer(False)
    a.penup()
    a.goto(hit)
    a.setheading(a.towards(N(e.p1.x), N(e.p1.y)))
    dist = min(N(Point(a.pos()).distance(e.p1)), paint_len/2)
    a.forward(dist)
    dist = min(N(Point(a.pos()).distance(e.p2)), paint_len)
    a.setheading(a.towards(N(e.p2.x), N(e.p2.y)))
    a.pendown()
    a.forward(dist)
    a.penup()
    turtle.tracer(True)

t = turtle.Turtle()
a = turtle.Turtle()
a.hideturtle()
a.width(3)

turtle.tracer(False)
p1 = (0,0)
p2 = (10,0)
p3 = (10,10)
p4 = (0,10)
E = [Segment(p1, p2), Segment(p2,p3), Segment(p3,p4), Segment(p4,p1)]

def completely_painted(e):
    a.goto(N(e.p1.x), N(a.p1.y))
    a.setheading(a.towards(N(e.p2.x), N(e.p2.y)))
    a.goto(N(e.p2.x), N(e.p2.y))
    #if a passed through non-painted part:
    #   return False
    return True

def all_edges_painted():
    for e in E:
        if not completely_painted(e):
            return False
    return True


t.penup()
t.goto(p1)
t.pendown()
for p in [p2, p3, p4, p1]:
    t.goto(p)
t.penup()
t.goto(5,5)
t.setheading(60)
t.pendown()
pos = Point(t.pos())
vec = Ray(pos, angle=radians(t.heading()))
ctr = 0
k = 0
p = -1
turtle.tracer(True)
t.speed(10)
a.color('red')
while True:
    for e in E:
        intersections = vec.intersection(e)
        if len(intersections) > 0:
            hit = None
            for i in intersections:
                if isinstance(i, Point):
                    hit = i
            if hit == None:
                continue
            
            ln = e.perpendicular_line(hit)
            pt = ln.projection(pos)
            normal = Ray(hit, pt) 
            t.goto(hit)
            paint(e, N(hit))
            rotate = 2 * t.towards(pt.x, pt.y) - 180 - t.heading()
            t.setheading(rotate)
            if k == 0:
                k = 1
            elif k == 1:
                k = 0
        pos = Point(t.pos())
        vec = Ray(pos, angle=radians(t.heading()))
    if ctr >= 50: # this should be if all_edges_painted()
        break
    ctr += 1

turtle.exitonclick()
turtle.done()

I commented out the parts where the method to check whether all edges are covered goes.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source