def clip_draw_diamond(vertex, circle, xL, xR, yT, yB) -> None: """实验特供型金刚石裁剪及绘制函数 :param vertex: 金刚石顶点表 :param circle: 金刚石同心圆表 :param xL: 左框线 :param xR: 右框线 :param yT: 上框线 :param yB: 下框线 :return: 无返回值 -> 实验特供裁剪及绘制金刚石 """ turtle.speed(0) # 设置笔头移动速度->0->最快 turtle.ht() # 隐藏海龟,提高绘制速度 turtle.setheading(90) # 海龟朝北 nums = len(vertex) # 绘制同心圆裁剪情况 for i in range(nums): circle[i][2] = clip_circle_rectangle_special(xL, xR, yT, yB) x = circle[i][0][0] y = circle[i][0][1] r = circle[i][1] extent = circle[i][2] draw_circle(x, y, r, extent, 'green') # 绘制金刚石线段裁剪情况 for i in range(nums): for j in range(i + 1, nums): line_segment = sutherland_cohen([vertex[i], vertex[j]], xL, yT, xR, yB) if line_segment: line_to(line_segment[0], line_segment[1], 'green')
def diamond_draw_no_interrupt(vertex, radius, nums) -> None: """金刚石绘制函数(不间断->即一笔画) PS : 这里的一笔绘指的仅仅是金刚石的线段部分,不包括同心圆(同心圆都不想交怎么一笔绘嘛) 此外,线段一笔绘要求圆周等分数为奇数 PS : 不写了23333 :param vertex: 顶点表 :param radius: 外圆半径 : 用于绘制同心圆,因此这里又把这个参数拿过来了 :param nums: 外圆等分数 : 其实可以使用vertex的大小,但是不想多计算一步,直接拿来用 :return: 无返回值 -> 绘制金刚石图案 """ turtle.speed(0) # 绘制同心圆 for i in range(0, nums): draw_circle(0, 0, (i + 1) * (radius / nums)) turtle.speed(1) # 一笔绘线段 # 定义并初始化相邻点数据结构 next_point = list() print("\n啊这", list(range(nums - 1))) for i in range(nums - 1): print(i, end=" ") next_point.append([]) print(i) next_point[i] = list(range(i, nums)) # 绘制 for j in range(nums): for i in range(nums - j - 1): line_to(vertex[i], vertex[next_point[i][0]]) next_point[i].pop(0)
def diamond_draw_abandoned(radius, nums): """金刚石绘制函数 PS : 最后一个金刚石图形参数是用来表示整个金刚石图案的信息的,实验四里的裁剪会用上 :param radius: 外圆半径 :param nums: 外圆等分数 :return: diamond -> 金刚石图形参数 """ turtle.bgcolor("black") # 还原示例图片中的黑色底色 turtle.speed(0) # 设置笔头移动速度->0->最快 turtle.ht() # 隐藏海龟,提高绘制速度 x_list = [] y_list = [] circle = list() line = dict() table_line_point = dict() for i in range(0, nums): # 将圆周上的等分点都加到列表里 x_list.append(radius * math.cos(2 * math.pi / nums * i)) y_list.append(radius * math.sin(2 * math.pi / nums * i)) # 绘制同心圆(注意绘制起点是圆周底点) 并加入到圆组参数中 circle.append([[0, 0], (i + 1) * (radius / nums), 360]) draw_circle(0, 0, (i + 1) * (radius / nums)) for i in range(0, nums): line[i] = list() table_line_point[i] = [x_list[i], y_list[i]] for j in range(i + 1, nums): line[i].append([x_list[j], y_list[j]]) line_to(x_list[i], y_list[i], x_list[j], y_list[j]) return [circle, line, table_line_point]
def diamond_draw(vertex, radius, nums) -> None: """金刚石绘制函数 :param vertex: 顶点表 :param radius: 外圆半径 : 用于绘制同心圆,因此这里又把这个参数拿过来了 :param nums: 外圆等分数 : 其实可以使用vertex的大小,但是不想多计算一步,直接拿来用 :return: 无返回值 -> 绘制金刚石图案 """ for i in range(0, nums): draw_circle(0, 0, (i + 1) * (radius / nums)) for i in range(0, nums): for j in range(i + 1, nums): line_to(vertex[i], vertex[j])
def polaris_draw(x_lst, y_lst) -> None: """ version-1.0-绘制一颗北极星[彩边][含内线] 给定了各顶点坐标(毕竟绘制北极星并不是本次实验的重点) """ # 颜色RGB:橙蓝粉绿 color_lst = [(249, 134, 105), (71, 177, 249), (249, 49, 249), (94, 249, 94)] # turtle色彩模式切换为RGB模式 turtle.colormode(255) # 绘制北极星内线 for i in [0, 2, 4, 6]: line_to([x_lst[i], y_lst[i]], [x_lst[i + 8], y_lst[i + 8]], color_lst[i // 2]) for i in [1, 3, 5, 7]: point_color_dict = {1: 0, 3: 2, 5: 2, 7: 0} line_to([x_lst[i], y_lst[i]], [x_lst[i + 8], y_lst[i + 8]], color_lst[point_color_dict[i]]) # 绘制北极星轮廓 # 轮廓点色对应表 point_color_dict = { 0: 0, 1: 1, 2: 1, 3: 2, 4: 2, 5: 3, 6: 3, 7: 0, 8: 0, 9: 1, 10: 1, 11: 2, 12: 2, 13: 3, 14: 3, 15: 0 } turtle.penup() # 画笔抬起 -- 移动时不画线 turtle.goto(x_lst[0], y_lst[0]) # 将笔尖移动到(x,y) turtle.pendown() for i in range(x_lst.__len__() - 1): turtle.color(color_lst[point_color_dict[i]]) turtle.goto(x_lst[i + 1], y_lst[i + 1]) turtle.color(color_lst[0]) turtle.goto(x_lst[0], y_lst[0])
def draw_graphic_2D_circle_line(circle: list, line: dict, table_line_point: dict): """绘制由圆形和线段组成的二维图形 PS : 这又是一个实验例程特化的函数; 规定所有圆的圆心在(0,0); 所有圆形的起绘点都在圆的右点从而需要海龟朝北 :param circle: 圆组 -> [圆心, 半径, 弧度], ... :param line: 线段组 -> { 1 : [点, 点, ...] , ... } :param table_line_point: 线段组 -> { 1 : 点 , ... } """ turtle.speed(0) # 设置笔头移动速度->0->最快 turtle.ht() # 隐藏海龟,提高绘制速度 turtle.setheading(90) # 海龟朝北 for i in range(len(circle)): draw_circle(circle[i][0][0], circle[i][0][1], circle[i][1], circle[i][2]) for i in line: for j in range(len(line[i])): line_to(table_line_point[i][0], table_line_point[i][1], line[i][j][0], line[i][j][1])
def magic_triangle_draw(): """魔术三角形绘制 """ a = [50, -50 / math.sqrt(3)] b = [-150, -50 / math.sqrt(3)] c = [50, 200 * math.sqrt(3) - 50 / math.sqrt(3)] d = [-50, 200 * math.sqrt(3) - 50 / math.sqrt(3)] e = [-300, -50 / math.sqrt(3) - 50 * math.sqrt(3)] f = [100, -50 / math.sqrt(3) - 50 * math.sqrt(3)] vertex = [a, b, c, d, e, f] turtle.begin_fill() for i in range(vertex.__len__() - 1): line_to(vertex[i], vertex[i + 1], "pink") line_to(vertex[-1], vertex[0], "pink") turtle.end_fill() for i in range(vertex.__len__()): vertex[i] = rotate_point(vertex[i], 120) turtle.begin_fill() for i in range(vertex.__len__() - 1): line_to(vertex[i], vertex[i + 1], "red") line_to(vertex[-1], vertex[0], "red") turtle.end_fill() for i in range(vertex.__len__()): vertex[i] = rotate_point(vertex[i], 120) turtle.begin_fill() for i in range(vertex.__len__() - 1): line_to(vertex[i], vertex[i + 1], "yellow") line_to(vertex[-1], vertex[0], "yellow") turtle.end_fill()