Pythonを使用して国旗を描き、中国に来て、武漢に来てください。



Use Python Draw National Flag



Pythonのturtleモジュールで国旗を描くことを学びましたが、紹介や描画方法の多くが間違っています。中国の旗には厳格な規則と基準があります。この記事は国旗の知識に基づいており、厳密で正確な図を示しています。方法。

••(1)基本的な紹介

旗の表面は赤と長方形で、長さと高さは3〜2です。旗の表面の左上には、5つの黄色い五芒星が飾られています。 1つの星は大きく、外接円は旗の高さの10分の3であり、左側の4つの星は小さく、外接円は旗の高さの10分の1であり、リングは大星の右。旗竿カバーは白です。

•(2)5つの星の位置と描画は次のとおりです。

••((1)5つの星の位置を決定するには、最初に旗の面を4つの等しい長方形に分割し、左上の長方形を10の等しい部分に分割し、左右を15の等しい部分に分割します。••(()大きな五芒星の中心点は、長方形の5点、左側の5点、右側の10点です。描画方法は次のとおりです。この点を円の中心とし、3つの等しい半径の円を作成します。

••この円上に、5つの等距離の点を設定し、そのうちの1つは円の真上にある必要があります。次に、5つのポイントのそれぞれで区切られた2つのポイントを接続して、それぞれを直線にします。



••5本の直線で形成される外側の等高線は、必要な大きな五芒星です。五芒星の一つが上を向いています。•(3)4つの小さな五芒星の中心点。最初の点は上から下の8点、左の10番目の点は右の5点、2番目の点は上から下の6、左の12です。ポイント3右、•3番目のポイントは上7と下3、左12と右3ポイント、4番目のポイントは上9と下10、左10と5ポイントです。描画方法は、上記の4点を円の中心とし、半径を1等分します。••それぞれ4つの円を作ります。各円に5つの等距離の点を設定し、そのうちの1つは、大きな5つの尖った星の中心と上記の4つの円の中心との間の接続線上に配置する必要があります。••次に、同じ方法を使用して大きな五芒星を形成し、小さな五芒星を形成します。 4つの小さな五芒星のそれぞれには、大きな五芒星の中心に面した尖点があります。

••(3)国旗の一般基準は、以下の5種類に設定されており、すべての部門が適宜選択することができます。

••((1)長いです288センチ、高さ192CM。••(()長いです240センチ、高さ160CM。••((3)長いです192センチ、高さ128CM。••((4)長いです144センチ、高さ96CM。••((5)長いです96センチ、高さ64CM。

(4)具体的な場所の関係は次のとおりです。

(5)Pythonコードの実装

#!/usr/bin/env python # -*- coding: utf-8 –*- ''' Use the turtle class to draw a five-star red flag. ''' __author__ = 'xu xinjian' import turtle import math '''Define a class of coordinate points''' class Point: def __init__(self,x=0,y=0): self.x=x self.y=y def getx(self): return self.x def gety(self): return self.y '''Define a class to get the distance between two points''' class Getlen: def __init__(self,p1,p2): self.x=p1.getx()-p2.getx() self.y=p1.gety()-p2.gety() #Use math.sqrt() to find the square root self.len= math.sqrt((self.x**2)+(self.y**2)) #Define the function to get the length of the line def getlen(self): return self.len '''Initialize window and canvas size''' turtle.setup(*2,*2,startx=350,starty=250) turtle.bgcolor('red') turtle.shape('turtle') turtle.color('yellow','yellow') ''' Draw a big five-pointed star ''' turtle.penup() turtle.goto(-160,108) turtle.right(72) turtle.pendown() length1=2*48*math.cos(18)#Calculate the side length of the five-pointed star # print(length1) turtle.begin_fill()#Start filling #Repeat drawing five sides for i in range(5): turtle.forward(length1) turtle.right(144) turtle.end_fill() '''Back to the center of the big five-pointed star''' turtle.penup() turtle.goto(-160,80) turtle.setheading(0)#To the right ''' Draw the first small five-pointed star ''' p0=Point(-160,80) p1=Point(-80,124) d1=Getlen(p0,p1).getlen() # angle1=math.degrees(math.atan(3/5)) print('angle1:',angle1) turtle.left(angle1) turtle.penup() turtle.forward(d1-16) turtle.pendown() turtle.left(18) length2=2*16*math.cos(18) turtle.begin_fill() for i in range(5): turtle.forward(length2) turtle.right(144) turtle.end_fill() '''Back to the center of the big five-pointed star''' turtle.penup() turtle.goto(-160,80) turtle.setheading(0) ''' Draw the second small five-pointed star ''' p0=Point(-160,80) p1=Point(-48,96) d1=Getlen(p0,p1).getlen() angle1=math.degrees(math.atan(1/7)) print('angle1:',angle1) turtle.left(angle1) turtle.penup() turtle.forward(d1-16) turtle.pendown() turtle.left(18) length2=2*16*math.cos(18) turtle.begin_fill() for i in range(5): turtle.forward(length2) turtle.right(144) turtle.end_fill() '''Back to the center of the big five-pointed star''' turtle.penup() turtle.goto(-160,80) turtle.setheading(0) ''' Draw the third small five-pointed star ''' p0=Point(-160,80) p1=Point(-48,48) d1=Getlen(p0,p1).getlen() angle1=math.degrees(math.atan(2/7)) print('angle1:',angle1) turtle.right(angle1) turtle.penup() turtle.forward(d1-16) turtle.pendown() turtle.left(18) length2=2*16*math.cos(18) turtle.begin_fill() for i in range(5): turtle.forward(length2) turtle.right(144) turtle.end_fill() '''Back to the center of the big five-pointed star''' turtle.penup() turtle.goto(-160,80) turtle.setheading(0) ''' Draw the fourth small five-pointed star ''' p0=Point(-160,80) p1=Point(-80,16) d1=Getlen(p0,p1).getlen() angle1=math.degrees(math.atan(4/5)) print('angle1:',angle1) turtle.right(angle1) turtle.penup() turtle.forward(d1-16) turtle.pendown() turtle.left(18) length2=2*16*math.cos(18) turtle.begin_fill() for i in range(5): turtle.forward(length2) turtle.right(144) turtle.end_fill() turtle.hideturtle() turtle.mainloop()

(6)効果の達成

画像

注:5つ星の赤い旗の紹介資料は、Baidu百科事典からのものです。