from turtleimport * def gotos ( x, y) : penup( ) goto( x, y) pendown( ) # 眼睛 def eyes ( ) : tracer( False ) a= 2.5 for iin range ( 120 ) : if 0 <= i< 30 or 60 <= i< 90 : a-= 0.05 lt( 3 ) fd( a) else : a+= 0.05 lt( 3 ) fd( a) tracer( True ) # 胡须 def beard ( ) : gotos( - 37 , 135 ) seth( 165 ) fd( 60 ) gotos( - 37 , 125 ) seth( 180 ) fd( 60 ) gotos( - 37 , 115 ) seth( 193 ) fd( 60 ) gotos( 37 , 135 ) seth( 15 ) fd( 60 ) gotos( 37 , 125 ) seth( 0 ) fd( 60 ) gotos( 37 , 115 ) seth( - 13 ) fd( 60 ) # 嘴巴 def mouth ( ) : gotos( 5 , 148 ) seth( 270 ) fd( 100 ) seth( 0 ) circle( 120 , 50 ) seth( 230 ) circle( - 120 , 100 ) # 围巾 def scarf ( ) : fillcolor( '#e70010' ) begin_fill( ) seth( 0 ) fd( 200 ) circle( - 5 , 90 ) fd( 10 ) circle( - 5 , 90 ) fd( 207 ) circle( - 5 , 90 ) fd( 10 ) circle( - 5 , 90 ) end_fill( ) # 鼻子 def nose ( ) : gotos( 4 , 148 ) fillcolor( '#e70010' ) begin_fill( ) circle( 20 ) end_fill( ) # 黑眼睛 def black_eyes ( ) : seth( 0 ) gotos( - 20 , 195 ) fillcolor( '#000000' ) begin_fill( ) circle( 13 ) end_fill( ) pensize( 6 ) gotos( 20 , 205 ) seth( 75 ) circle( - 10 , 150 ) pensize( 3 ) gotos( - 17 , 200 ) seth( 0 ) fillcolor( '#ffffff' ) begin_fill( ) circle( 5 ) end_fill( ) gotos( 0 , 0 ) # 脸 def face ( ) : fd( 183 ) fillcolor( '#ffffff' ) begin_fill( ) lt( 45 ) circle( 120 , 100 ) seth( 90 ) eyes( ) seth( 180 ) penup( ) fd( 60 ) pendown( ) seth( 90 ) eyes( ) penup( ) seth( 180 ) fd( 64 ) pendown( ) seth( 215 ) circle( 120 , 100 ) end_fill( ) # 头型 def head ( ) : penup( ) circle( 150 , 40 ) pendown( ) fillcolor( '#00a0de' ) begin_fill( ) circle( 150 , 280 ) end_fill( ) # 画哆啦A梦 def Doraemon ( ) : # 头部 head( ) # 围脖 scarf( ) # 脸 face( ) # 眼睛 black_eyes( ) # 红鼻子 nose( ) # 嘴巴 mouth( ) # 胡须 beard( ) # 身体 gotos( 0 , 0 ) seth( 0 ) penup( ) circle( 150 , 50 ) pendown( ) seth( 30 ) fd( 40 ) seth( 70 ) circle( - 30 , 270 ) fillcolor( '#00a0de' ) begin_fill( ) seth( 230 ) fd( 80 ) seth( 90 ) circle( 1000 , 1 ) seth( - 89 ) circle( - 1000 , 10 ) # print(pos()) seth( 180 ) fd( 70 ) seth( 90 ) circle( 30 , 180 ) seth( 180 ) fd( 70 ) # print(pos()) seth( 100 ) circle( - 1000 , 9 ) seth( - 86 ) circle( 1000 , 2 ) seth( 230 ) fd( 40 ) # print(pos()) circle( - 30 , 230 ) seth( 45 ) fd( 81 ) seth( 0 ) fd( 203 ) circle( 5 , 90 ) fd( 10 ) circle( 5 , 90 ) fd( 7 ) seth( 40 ) circle( 150 , 10 ) seth( 30 ) fd( 40 ) end_fill( ) # 左手 seth( 70 ) fillcolor( '#ffffff' ) begin_fill( ) circle( - 30 ) end_fill( ) # 脚 gotos( 103.74 , - 182.59 ) seth( 0 ) fillcolor( '#ffffff' ) begin_fill( ) fd( 15 ) circle( - 15 , 180 ) fd( 90 ) circle( - 15 , 180 ) fd( 10 ) end_fill( ) gotos( - 96.26 , - 182.59 ) seth( 180 ) fillcolor( '#ffffff' ) begin_fill( ) fd( 15 ) circle( 15 , 180 ) fd( 90 ) circle( 15 , 180 ) fd( 10 ) end_fill( ) # 右手 gotos( - 133.97 , - 91.81 ) seth( 50 ) fillcolor( '#ffffff' ) begin_fill( ) circle( 30 ) end_fill( ) # 口袋 gotos( - 103.42 , 15.09 ) seth( 0 ) fd( 38 ) seth( 230 ) begin_fill( ) circle( 90 , 260 ) end_fill( ) gotos( 5 , - 40 ) seth( 0 ) fd( 70 ) seth( - 90 ) circle( - 70 , 180 ) seth( 0 ) fd( 70 ) # 铃铛 gotos( - 103.42 , 15.09 ) fd( 90 ) seth( 70 ) fillcolor( '#ffd200' ) # print(pos()) begin_fill( ) circle( - 20 ) end_fill( ) seth( 170 ) fillcolor( '#ffd200' ) begin_fill( ) circle( - 2 , 180 ) seth( 10 ) circle( - 100 , 22 ) circle( - 2 , 180 ) seth( 180 - 10 ) circle( 100 , 22 ) end_fill( ) goto( - 13.42 , 15.09 ) seth( 250 ) circle( 20 , 110 ) seth( 90 ) fd( 15 ) dot( 10 ) gotos( 0 , - 150 ) if __name__== '__main__' : setup( 1.0 , 1.0 ) title( "Doraemon" ) bgcolor( 'skyblue' ) speed( 0 ) pensize( 3 ) hideturtle( ) Doraemon( ) gotos( 300 , - 100 ) write( '永\n远\n爱\n你' , font= ( "Bradley Hand ITC" , 50 , "bold" ) ) mainloop( )
/// <summary> /// 创建哆啦A梦(Doraemon)的几何图形 - 基于Python turtle代码重制版 /// </summary> private void CreateGeometricShapes ( BlockTableRecord btr) { // 哆啦A梦尺寸参数(基于Python代码提取) const double HEAD_RADIUS= 150.0 ; // 头部半径 const double BODY_WIDTH= 203.0 ; // 身体宽度 const double EYE_RADIUS= 13.0 ; // 黑眼珠半径 const double WHITE_EYE_RADIUS= 5.0 ; // 白眼珠半径 const double NOSE_RADIUS= 20.0 ; // 鼻子半径 const double BELL_RADIUS= 20.0 ; // 铃铛半径 const double HAND_RADIUS= 30.0 ; // 手部半径 const double FOOT_WIDTH= 90.0 ; // 脚部宽度 const double FOOT_HEIGHT= 15.0 ; // 脚部高度 // 颜色定义(基于Python代码) // 蓝色身体: #00a0de -> 近似颜色索引 // 白色: #ffffff -> 颜色索引 7 // 红色: #e70010 -> 颜色索引 1 // 黑色: #000000 -> 颜色索引 0 // 黄色铃铛: #ffd200 -> 颜色索引 2 // ========== 1. 头部(蓝色圆形)========== Circle head= new Circle ( new Point3d ( 0 , 50 , 0 ) , Vector3d. ZAxis, HEAD_RADIUS) ; head. ColorIndex= 5 ; // 蓝色 (接近#00a0de) btr. AppendEntity ( head) ; // ========== 2. 脸部(白色区域)========== // 脸型(模拟Python代码中的复杂形状) Polyline face= new Polyline ( ) ; // 根据Python代码中的face()函数,脸是近似椭圆形的白色区域 // 简化实现:创建一个椭圆形状 double faceCenterX= 0 ; double faceCenterY= 50 ; double faceWidth= HEAD_RADIUS* 1.1 ; double faceHeight= HEAD_RADIUS* 0.7 ; // 创建椭圆(用多段线近似) for ( int i= 0 ; i<= 36 ; i++ ) { double angle= i* 10 * Math. PI/ 180 ; double x= faceCenterX+ faceWidth* Math. Cos ( angle) / 2 ; double y= faceCenterY+ faceHeight* Math. Sin ( angle) / 2 ; face. AddVertexAt ( i, new Point2d ( x, y) , 0 , 0 , 0 ) ; } face. Closed= true ; face. ColorIndex= 7 ; // 白色 btr. AppendEntity ( face) ; // ========== 3. 眼睛(黑色眼珠+白色高光)========== // 左眼 Circle leftEye= new Circle ( new Point3d ( - 20 , 95 , 0 ) , Vector3d. ZAxis, EYE_RADIUS) ; leftEye. ColorIndex= 0 ; // 黑色 btr. AppendEntity ( leftEye) ; // 左眼高光 Circle leftEyeHighlight= new Circle ( new Point3d ( - 17 , 100 , 0 ) , Vector3d. ZAxis, WHITE_EYE_RADIUS) ; leftEyeHighlight. ColorIndex= 7 ; // 白色 btr. AppendEntity ( leftEyeHighlight) ; // 右眼 Circle rightEye= new Circle ( new Point3d ( 20 , 105 , 0 ) , Vector3d. ZAxis, EYE_RADIUS) ; rightEye. ColorIndex= 0 ; // 黑色 btr. AppendEntity ( rightEye) ; // 右眼睫毛(模拟Python代码中的弧线) Arc rightEyeLash= new Arc ( new Point3d ( 20 , 205 , 0 ) , 10 , 75 * Math. PI/ 180 , 225 * Math. PI/ 180 ) ; rightEyeLash. ColorIndex= 0 ; // 黑色 btr. AppendEntity ( rightEyeLash) ; // ========== 4. 鼻子(红色圆形)========== Circle nose= new Circle ( new Point3d ( 4 , 48 , 0 ) , Vector3d. ZAxis, NOSE_RADIUS) ; nose. ColorIndex= 1 ; // 红色 btr. AppendEntity ( nose) ; // ========== 5. 胡须(6根)========== // 左上方胡须 Line leftUpperWhisker= new Line ( new Point3d ( - 37 , 35 , 0 ) , new Point3d ( - 97 , 70 , 0 ) ) ; // 长度60,角度165° leftUpperWhisker. ColorIndex= 0 ; btr. AppendEntity ( leftUpperWhisker) ; // 左中间胡须 Line leftMiddleWhisker= new Line ( new Point3d ( - 37 , 25 , 0 ) , new Point3d ( - 97 , 25 , 0 ) ) ; // 长度60,角度180° leftMiddleWhisker. ColorIndex= 0 ; btr. AppendEntity ( leftMiddleWhisker) ; // 左下方胡须 Line leftLowerWhisker= new Line ( new Point3d ( - 37 , 15 , 0 ) , new Point3d ( - 97 , - 20 , 0 ) ) ; // 长度60,角度193° leftLowerWhisker. ColorIndex= 0 ; btr. AppendEntity ( leftLowerWhisker) ; // 右上方胡须 Line rightUpperWhisker= new Line ( new Point3d ( 37 , 35 , 0 ) , new Point3d ( 97 , 70 , 0 ) ) ; // 长度60,角度15° rightUpperWhisker. ColorIndex= 0 ; btr. AppendEntity ( rightUpperWhisker) ; // 右中间胡须 Line rightMiddleWhisker= new Line ( new Point3d ( 37 , 25 , 0 ) , new Point3d ( 97 , 25 , 0 ) ) ; // 长度60,角度0° rightMiddleWhisker. ColorIndex= 0 ; btr. AppendEntity ( rightMiddleWhisker) ; // 右下方胡须 Line rightLowerWhisker= new Line ( new Point3d ( 37 , 15 , 0 ) , new Point3d ( 97 , - 20 , 0 ) ) ; // 长度60,角度-13° rightLowerWhisker. ColorIndex= 0 ; btr. AppendEntity ( rightLowerWhisker) ; // ========== 6. 嘴巴(红色微笑)========== // 嘴部弧线(根据Python代码中的mouth()函数) Arc mouthArc1= new Arc ( new Point3d ( 5 , - 52 , 0 ) , // 半径120,角度50° 120 , 270 * Math. PI/ 180 , 320 * Math. PI/ 180 ) ; mouthArc1. ColorIndex= 1 ; // 红色 btr. AppendEntity ( mouthArc1) ; Arc mouthArc2= new Arc ( new Point3d ( - 115 , - 52 , 0 ) , // 半径-120,角度100° 120 , 230 * Math. PI/ 180 , 330 * Math. PI/ 180 ) ; mouthArc2. ColorIndex= 1 ; // 红色 btr. AppendEntity ( mouthArc2) ; // ========== 7. 围巾(红色)========== Polyline scarf= new Polyline ( ) ; scarf. AddVertexAt ( 0 , new Point2d ( - 100 , 0 ) , 0 , 0 , 0 ) ; scarf. AddVertexAt ( 1 , new Point2d ( 100 , 0 ) , 0 , 0 , 0 ) ; scarf. AddVertexAt ( 2 , new Point2d ( 100 , - 10 ) , 0 , 0 , 0 ) ; scarf. AddVertexAt ( 3 , new Point2d ( - 100 , - 10 ) , 0 , 0 , 0 ) ; scarf. Closed= true ; scarf. ColorIndex= 1 ; // 红色 btr. AppendEntity ( scarf) ; // ========== 8. 身体(蓝色)========== Polyline body= new Polyline ( ) ; double bodyTopY= - 30 ; double bodyBottomY= - 180 ; body. AddVertexAt ( 0 , new Point2d ( - BODY_WIDTH/ 2 , bodyTopY) , 0 , 0 , 0 ) ; body. AddVertexAt ( 1 , new Point2d ( BODY_WIDTH/ 2 , bodyTopY) , 0 , 0 , 0 ) ; body. AddVertexAt ( 2 , new Point2d ( BODY_WIDTH/ 2 , bodyBottomY) , 0 , 0 , 0 ) ; body. AddVertexAt ( 3 , new Point2d ( - BODY_WIDTH/ 2 , bodyBottomY) , 0 , 0 , 0 ) ; body. Closed= true ; body. ColorIndex= 5 ; // 蓝色 btr. AppendEntity ( body) ; // ========== 9. 口袋(白色圆形)========== Circle pocket= new Circle ( new Point3d ( 0 , - 80 , 0 ) , Vector3d. ZAxis, 70 ) ; pocket. ColorIndex= 7 ; // 白色 btr. AppendEntity ( pocket) ; // 口袋内圆 Circle pocketInner= new Circle ( new Point3d ( 0 , - 80 , 0 ) , Vector3d. ZAxis, 50 ) ; pocketInner. ColorIndex= 4 ; // 青色(接近口袋颜色) btr. AppendEntity ( pocketInner) ; // ========== 10. 铃铛(黄色)========== Circle bell= new Circle ( new Point3d ( 0 , - 40 , 0 ) , Vector3d. ZAxis, BELL_RADIUS) ; bell. ColorIndex= 2 ; // 黄色 btr. AppendEntity ( bell) ; // 铃铛项圈(红色) Line bellCollar1= new Line ( new Point3d ( - 13 , - 40 , 0 ) , new Point3d ( - 2 , - 60 , 0 ) ) ; bellCollar1. ColorIndex= 1 ; // 红色 btr. AppendEntity ( bellCollar1) ; Line bellCollar2= new Line ( new Point3d ( 13 , - 40 , 0 ) , new Point3d ( 2 , - 60 , 0 ) ) ; bellCollar2. ColorIndex= 1 ; // 红色 btr. AppendEntity ( bellCollar2) ; // 铃铛中心点 Circle bellCenter= new Circle ( new Point3d ( 0 , - 40 , 0 ) , Vector3d. ZAxis, 2 ) ; bellCenter. ColorIndex= 0 ; // 黑色 btr. AppendEntity ( bellCenter) ; // 铃铛垂线 Line bellLine= new Line ( new Point3d ( 0 , - 60 , 0 ) , new Point3d ( 0 , - 65 , 0 ) ) ; bellLine. ColorIndex= 0 ; // 黑色 btr. AppendEntity ( bellLine) ; // ========== 11. 手部(白色圆形)========== // 左手 Circle leftHand= new Circle ( new Point3d ( - 133 , - 91 , 0 ) , Vector3d. ZAxis, HAND_RADIUS) ; leftHand. ColorIndex= 7 ; // 白色 btr. AppendEntity ( leftHand) ; // 右手 Circle rightHand= new Circle ( new Point3d ( 133 , - 91 , 0 ) , Vector3d. ZAxis, HAND_RADIUS) ; rightHand. ColorIndex= 7 ; // 白色 btr. AppendEntity ( rightHand) ; // ========== 12. 脚部(白色椭圆形)========== // 左脚 Polyline leftFoot= new Polyline ( ) ; leftFoot. AddVertexAt ( 0 , new Point2d ( - 103 , - 182 ) , 0 , 0 , 0 ) ; leftFoot. AddVertexAt ( 1 , new Point2d ( - 13 , - 182 ) , 0 , 0 , 0 ) ; leftFoot. AddVertexAt ( 2 , new Point2d ( - 13 , - 197 ) , 0 , 0 , 0 ) ; leftFoot. AddVertexAt ( 3 , new Point2d ( - 103 , - 197 ) , 0 , 0 , 0 ) ; leftFoot. Closed= true ; leftFoot. ColorIndex= 7 ; // 白色 // 添加圆角效果(近似) leftFoot. SetBulgeAt ( 1 , 0.5 ) ; leftFoot. SetBulgeAt ( 3 , 0.5 ) ; btr. AppendEntity ( leftFoot) ; // 右脚 Polyline rightFoot= new Polyline ( ) ; rightFoot. AddVertexAt ( 0 , new Point2d ( 13 , - 182 ) , 0 , 0 , 0 ) ; rightFoot. AddVertexAt ( 1 , new Point2d ( 103 , - 182 ) , 0 , 0 , 0 ) ; rightFoot. AddVertexAt ( 2 , new Point2d ( 103 , - 197 ) , 0 , 0 , 0 ) ; rightFoot. AddVertexAt ( 3 , new Point2d ( 13 , - 197 ) , 0 , 0 , 0 ) ; rightFoot. Closed= true ; rightFoot. ColorIndex= 7 ; // 白色 // 添加圆角效果(近似) rightFoot. SetBulgeAt ( 0 , 0.5 ) ; rightFoot. SetBulgeAt ( 2 , 0.5 ) ; btr. AppendEntity ( rightFoot) ; // ========== 13. 尾巴(蓝色小球)========== Circle tail= new Circle ( new Point3d ( - 130 , - 120 , 0 ) , Vector3d. ZAxis, 15 ) ; tail. ColorIndex= 5 ; // 蓝色 btr. AppendEntity ( tail) ; // ========== 14. 签名文字 ========== DBText signature= new DBText ( ) ; signature. Position= new Point3d ( 0 , - 220 , 0 ) ; signature. Height= 12 ; signature. TextString= "Doraemon" ; signature. ColorIndex= 5 ; // 蓝色 signature. HorizontalMode= TextHorizontalMode. TextCenter; signature. AlignmentPoint= signature. Position; btr. AppendEntity ( signature) ; // ========== 15. 爱的文字(模仿Python代码)========== DBText loveText= new DBText ( ) ; loveText. Position= new Point3d ( 250 , - 150 , 0 ) ; loveText. Height= 20 ; loveText. TextString= "永远爱你" ; loveText. ColorIndex= 1 ; // 红色 loveText. HorizontalMode= TextHorizontalMode. TextLeft; loveText. VerticalMode= TextVerticalMode. TextTop; loveText. AlignmentPoint= loveText. Position; btr. AppendEntity ( loveText) ; }