基于Python繪制三種不同的中國結(jié)
前言
今天就來分享幾個用python繪制的圖案吧
馬上就要迎來新年了 就繪制了幾個中國結(jié),嘿嘿
話不多說,直接展示一下代碼和效果圖吧
示例一
效果圖
代碼展示
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) # 結(jié)心坐標 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) # 左側(cè) 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) # 右側(cè) 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): # 上側(cè) 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()
到此這篇關(guān)于基于Python繪制三種不同的中國結(jié)的文章就介紹到這了,更多相關(guān)Python繪制中國結(jié)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
完美解決Python matplotlib繪圖時漢字顯示不正常的問題
今天小編就為大家分享一篇完美解決Python matplotlib繪圖時漢字顯示不正常的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Pycharm如何設(shè)置默認請求頭和切換python環(huán)境
這篇文章主要介紹了Pycharm如何設(shè)置默認請求頭和切換python環(huán)境問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06使用Flask-Login模塊實現(xiàn)用戶身份驗證和安全性
當你想要在你的Flask應用中實現(xiàn)用戶身份驗證和安全性時,F(xiàn)lask-Login這個擴展將會是你的最佳伙伴,它提供了一組簡單而強大的工具來處理,下面我們就來看看具體的操作方法吧2023-08-08Python中用字符串調(diào)用函數(shù)或方法示例代碼
字符串作為python中常用的數(shù)據(jù)類型,掌握字符串的常用方法十分必要。下面這篇文章主要給大家介紹了關(guān)于Python中通過字符串調(diào)用函數(shù)或方法的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08