基于Python绘制三种不同的中国结
kaKA-小圆 人气:0前言
今天就来分享几个用python绘制的图案吧
马上就要迎来新年了 就绘制了几个中国结,嘿嘿
话不多说,直接展示一下代码和效果图吧
示例一
效果图
代码展示
import turtle turtle.screensize(600,800) turtle.pensize(10) turtle.pencolor("red") turtle.seth(-45) turtle.fd(102) turtle.circle(-6,180) turtle.fd(102) turtle.circle(6,180) turtle.fd(102) turtle.circle(-6,180) turtle.fd(102) turtle.circle(6,180) turtle.fd(102) turtle.circle(-6,180) turtle.fd(102) turtle.circle(6,180) turtle.fd(92) turtle.circle(-6,270) turtle.fd(92) turtle.circle(6,180) turtle.fd(102) turtle.circle(-6,180) turtle.fd(102) turtle.circle(6,180) turtle.fd(102) turtle.circle(-6,180) turtle.fd(102) turtle.circle(6,180) turtle.fd(102) turtle.circle(-6,180) turtle.fd(88) turtle.fd(20) turtle.seth(135) turtle.fd(20) turtle.seth(225) turtle.fd(20) turtle.seth(315) turtle.fd(20) turtle.seth(45) turtle.fd(20) turtle.seth(135) turtle.begin_fill() turtle.fillcolor("red") turtle.fd(50) turtle.seth(45) turtle.fd(30) turtle.seth(-45) turtle.fd(30) turtle.seth(225) turtle.fd(30) turtle.end_fill() turtle.seth(90) turtle.fd(40) turtle.pensize(20) turtle.fd(10) turtle.pensize(5) turtle.seth(105) turtle.fd(30) turtle.circle(-8,240) turtle.circle(20,20) turtle.fd(5) turtle.circle(20,60) turtle.fd(25) turtle.penup() turtle.setx(0) turtle.sety(0) turtle.goto(2,-127) turtle.pendown() turtle.pensize(5) turtle.begin_fill() turtle.fillcolor("red") turtle.seth(0) turtle.fd(15) turtle.seth(-90) turtle.fd(10) turtle.seth(180) turtle.fd(15) turtle.seth(90) turtle.fd(10) turtle.seth(0) turtle.fd(15) turtle.end_fill() turtle.pensize(2) for x in range(6): turtle.seth(-90) turtle.fd(50) turtle.penup() turtle.seth(90) turtle.fd(50) turtle.seth(180) turtle.fd(3) turtle.pendown() turtle.done()
示例二
效果图
如图所示 在代码里找到文字所在的地方,自己进行一下编辑,是可以加自己想加的话进去的
代码展示
import turtle as t def goto(x,y): t.penup() t.goto(x,y) t.pendown() def init(): t.setup(800,800) t.pensize(10) t.pencolor("red") t.speed(14) def jiexin(): m,n=0,200 for i in range(11): goto(m,n) t.seth(-45) t.fd(200) m-=20/pow(2,0.5) n-=20/pow(2,0.5) m,n=0,200 for j in range(11): goto(m,n) t.seth(-135) t.fd(200) m+=20/pow(2,0.5) n-=20/pow(2,0.5) def jiexiaoban(): m=-20/pow(2,0.5) n=200-20/pow(2,0.5) for k in range(4): goto(m,n) t.seth(135) t.fd(20) t.circle(10,180) t.fd(20) m-=40/pow(2,0.5) n-=40/pow(2,0.5) m=20/pow(2,0.5) n=200-20/pow(2,0.5) for k in range(4): goto(m,n) t.seth(45) t.fd(20) t.circle(-10,180) t.fd(20) m+=40/pow(2,0.5) n-=40/pow(2,0.5) m=20/pow(2,0.5) n=200-200*pow(2,0.5)+20/pow(2,0.5) for k in range(4): goto(m,n) t.seth(-45) t.fd(20) t.circle(10,180) t.fd(20) m+=40/pow(2,0.5) n+=40/pow(2,0.5) m=-20/pow(2,0.5) n=200-200*pow(2,0.5)+20/pow(2,0.5) for k in range(4): goto(m,n) t.seth(-135) t.fd(20) t.circle(-10,180) t.fd(20) m-=40/pow(2,0.5) n+=40/pow(2,0.5) def waiyuan(): goto(90*pow(2,0.5),200-110*pow(2,0.5)) t.seth(-45) t.circle(20,270) goto(-90*pow(2,0.5),200-110*pow(2,0.5)) t.seth(-135) t.circle(-20,270) goto(80*pow(2,0.5),200-120*pow(2,0.5)) t.seth(-45) t.circle(40,270) goto(-80*pow(2,0.5),200-120*pow(2,0.5)) t.seth(-135) t.circle(-40,270) def shengzi(): goto(0,200) t.pensize(20) t.seth(90) t.fd(60) goto(0,320) t.pensize(12) t.seth(180) t.circle(30,360) def hanzi(): goto(-150,325) t.write("Python",font=("Arial",40,"normal")) def main(): init() jiexin() jiexiaoban() waiyuan() shengzi() hanzi() t.hideturtle() t.done() main()
示例三
效果图
这个其实跟第二个差不多 就是没有上面那行字体
代码展示
import turtle as t from math import sqrt class chineseKnot: def __init__(self) -> None: # 画笔初始化 self.t = t self.t.pensize(10) self.t.setup(700, 700) self.t.pencolor("red") self.t.speed(14) # 结心坐标 self.x = 0 self.y = 200 def drawKnot(self) -> None: self.drawBody() self.drawEdge() self.drawAdorn() self.t.hideturtle() self.t.done() def drawBody(self) -> None: for i in range(11): self.__goto(self.x - i * 10 * sqrt(2), self.y - i * 10 * sqrt(2)) self.t.seth(-45) self.t.fd(200) self.__goto(self.x + i * 10 * sqrt(2), self.y - i * 10 * sqrt(2)) self.t.seth(-135) self.t.fd(200) def drawEdge(self) -> None: for i in range(4): # 左上角 self.__goto(-10 * sqrt(2) - i * 20 * sqrt(2), 200 - 10 * sqrt(2) - i * 20 * sqrt(2)) self.t.seth(135) self.t.fd(20) self.t.circle(10, 180) self.t.fd(20) # 右上角 self.__goto(10 * sqrt(2) + i * 20 * sqrt(2), 200 - 10 * sqrt(2) - i * 20 * sqrt(2)) self.t.seth(45) self.t.fd(20) self.t.circle(-10, 180) self.t.fd(20) # 左下角 self.__goto(-10 * sqrt(2) - i * 20 * sqrt(2), 200 - 190 * sqrt(2) + i * 20 * sqrt(2)) self.t.seth(-135) self.t.fd(20) self.t.circle(-10, 180) self.t.fd(20) # 右下角 self.__goto(10 * sqrt(2) + i * 20 * sqrt(2), 200 - 190 * sqrt(2) + i * 20 * sqrt(2)) self.t.seth(-45) self.t.fd(20) self.t.circle(10, 180) self.t.fd(20) # 左侧 self.t.seth(-45) self.__goto(90 * sqrt(2), 200 - 110 * sqrt(2)) self.t.circle(20, 270) self.__goto(-90 * sqrt(2), 200 - 110 * sqrt(2)) self.t.circle(-20, 270) # 右侧 self.__goto(80 * sqrt(2), 200 - 120 * sqrt(2)) self.t.circle(40, 270) self.__goto(-80 * sqrt(2), 200 - 120 * sqrt(2)) self.t.circle(-40, 270) def drawAdorn(self): # 上侧 self.__goto(self.x, self.y) self.t.pensize(14) self.t.seth(90) self.t.fd(60) self.__goto(0, 320) self.t.seth(180) self.t.circle(30, 360) def __goto(self, x: int, y: int) -> None: self.t.penup() self.t.goto(x, y) self.t.pendown() if __name__ == '__main__': knot = chineseKnot() knot.drawKnot()
加载全部内容