turtle 实现clip_rule=“evenodd”代码
时间: 2024-05-15 10:15:39 浏览: 81
python-turtle实现模拟按钮可仿照示例代码进行更改-原创
以下是使用turtle实现clip_rule = "evenodd"的示例代码:
```python
import turtle
# 创建画布
canvas = turtle.Screen()
canvas.bgcolor("white")
# 创建两个多边形
polygon1 = [(50, 50), (100, 100), (150, 50), (100, 0)]
polygon2 = [(0, 0), (100, 100), (200, 0)]
# 设置画笔属性
turtle.penup()
turtle.pencolor("black")
turtle.pensize(2)
turtle.speed(0)
# 绘制多边形1
turtle.goto(polygon1[0])
turtle.pendown()
for point in polygon1:
turtle.goto(point)
turtle.goto(polygon1[0])
# 绘制多边形2
turtle.penup()
turtle.goto(polygon2[0])
turtle.pendown()
for point in polygon2:
turtle.goto(point)
turtle.goto(polygon2[0])
# 设置剪裁规则为evenodd
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.penup()
turtle.goto(polygon1[0])
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
for point in polygon1:
turtle.goto(point)
turtle.goto(polygon1[0])
turtle.end_fill()
turtle.penup()
turtle.goto(polygon2[0])
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
for point in polygon2:
turtle.goto(point)
turtle.goto(polygon2[0])
turtle.end_fill()
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.penup()
turtle.goto(polygon1[0])
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
for point in polygon1:
turtle.goto(point)
turtle.goto(polygon1[0])
turtle.end_fill()
turtle.penup()
turtle.goto(polygon2[0])
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
for point in polygon2:
turtle.goto(point)
turtle.goto(polygon2[0])
turtle.end_fill()
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.penup()
turtle.goto(polygon2[0])
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
for point in polygon2:
turtle.goto(point)
turtle.goto(polygon2[0])
turtle.end_fill()
turtle.penup()
turtle.goto(polygon1[0])
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
for point in polygon1:
turtle.goto(point)
turtle.goto(polygon1[0])
turtle.end_fill()
turtle.end_fill()
# 关闭画布
turtle.done()
```
运行上述代码,将会在画布上绘制两个多边形,并使用evenodd剪裁规则将两个多边形的交集颜色填充为黑色。
阅读全文