如何只在多边形内绘制平行线?

问题描述 投票:0回答:0

当我运行我的程序时,首先,它提示用户通过询问多个 x 和 y 坐标来绘制多边形。假设我输入这个:

这将是我的输出:

但是,我想要的是仅在多边形内部绘制蓝线。这意味着它不能超过边缘。知道怎么做吗?任何帮助,将不胜感激。代码如下。

from turtle import *

vertices_x = []
vertices_y = []


def draw_hatchlines(vertices_x, min_y, max_y, max_x):       #blue parallel lines
    pencolor('blue')
    for y in range(int(min_y), int(max_y), 10):
        pu()
        goto(vertices_x[0], y)
        pd()
        goto(max_x, y)

def draw_polygon():
    hideturtle()
    screensize(1000, 1000, "white")

    num_vertices = int(input("Enter the number of vertices: "))
    vertices = []

# Prompt the user to input the vertices of the polygon
    for i in range(num_vertices):
        x, y = input("Enter the x and y coordinates of vertex {} (comma-separated): ".format(i+1)).split(',')
        vertices.append((float(x), float(y)))
        vertices_x.append(float(x))     # list for only x coordinate
        vertices_y.append(float(y))     # list for only x coordinate

    vertices_x.sort()
    vertices_y.sort()

   print(vertices)
   print(vertices_x)
   print(vertices_y)

# Initialize the Turtle and move it to the first vertex
   pu()
   goto(vertices[0][0], vertices[0][1])
   pd()

# Draw lines between the vertices to form the polygon
    for vertex in vertices:
        goto(vertex[0], vertex[1])

# Move the turtle back to the first vertex to complete the polygon
    goto(vertices[0][0], vertices[0][1])

    tracer(0)
    min_y = min(vertices_y)
    max_y = max(vertices_y)
    min_x = min(vertices_x)
    max_x = max(vertices_x)

    draw_hatchlines(vertices_x, min_y, max_y, max_x)

    update()
    done()

draw_polygon()
python polygon turtle-graphics python-turtle
© www.soinside.com 2019 - 2024. All rights reserved.