「Pythonプログラミング(第3版)」[米国] JohnZelle第4章回答



Python Programming John Zelle Chapter 4 Answers



正しいかどうかを判断する

1. grAphiCs.pyを使用して、Pythonのシェルウィンドウにグラフィックを描画します。
2.従来、グラフィックウィンドウの左上隅の座標は(0、0)です。
3.グラフィックス画面上の単一のポイントはピクセルと呼ばれます。
4.クラスの新しいインスタンスを作成する関数は、valueメソッドと呼ばれます。
5.インスタンス変数は、オブジェクト内にデータを格納するために使用されます。
6.ステートメントmyShApe.move(10,20)は、myShApeをポイント(10,20)に移動します。
7. 2つの変数が同じオブジェクトを参照している場合、エイリアスが生成されます。
8. Copyメソッドは、グラフィックオブジェクトのコピーを生成するために提供されています。
9、グラフィックウィンドウのタイトルは常に「GrAphiCsWinDow」です。
10. grAphiCsライブラリでは、マウスクリックを取得するために使用されるメソッドはreADMouseです。
回答



 1 F (is drawing a graph in the window created by the GrAphWin() function)  2 T(p.55 '(Traditionally, the graphics programmer positions the point in the upper left corner of the window.)  3 T(p.55 'The graphics window is actually a collection of small dots called 'pixels' (short for 'image elements')')  4 F(p.57 'To create a new instance of a class, we use a special operation called 'constructor', p. 58 'These methods are sometimes called 'value methods' because they allow us to get from the object Instance variable access information.' p.76 'The value method returns information about the instance variable of the object.')  5 T(p.57 'These values ​​are stored in the object as 'instance variables.')  6 F(p.58 '. All graphic objects have a move method. Below is the specification: move(Dx,Dy): Let the object move the Dx unit in the x direction and the Dy unit in the y direction.')  7 T(p.59 '...two variables refer to the same object called 'alias'...', p.76 'The case where two variables refer to the same object is called an alias.')  8 F(p.59 'The grAphiCs library provides a better solution, and all graphical objects support the Clone method of copying objects.')  9 F(p.61 '...the GrAphWin constructor allows an optional parameter to specify the title of the window.') 10 F(p.67 'If you call getMouse on GrAphWin, the program will pause and wait for the user to click somewhere in the graphics window.', p.71 'getMouse() pauses waiting for the user to click in the window and use The Point object returns the location of the mouse click.')

複数の選択肢
1.オブジェクトのインスタンス変数の値を返すメソッドは____と呼ばれます。
A.設定方法
B.機能
C.工法
D.価値法

2.オブジェクトの状態を変更する方法は____と呼ばれます。
A.状態法
B.設定方法
C.工法
D.方法の変更



3、____グラフィッククラスは正方形を描くのに最適です。
四角
B.ポリゴン
C.ライン
D.長方形

4. ____コマンドは、winの座標設定を左下隅の(0,0)と右上隅の(10,10)に変更します。
A. win.setCoords(Point(0,0)、Point(10,10))
B. win.setCoords((0,0)、(10,10))
C. win.setCoords(0、0、10、10)
D. win.setCoords(Point(10,10)、Point(0,0))

5.式____は、(2、3)から(4、5)までの線分を作成します。
A. Line(2、3、4、5)
B.ライン((2,3)、(4,5))
C. Line(2、4、3、5)
D. Line(Point(2,3)、Point(4,5))



6.コマンド____は、グラフィックオブジェクトshApeをグラフィックウィンドウwinに描画できます。
A. win.draw(shape)
B. win.show(shape)
C. shape.draw()
D. shape.draw(win)

7.式____は、点p1とp2の間の水平距離を計算します。
A. abs(p1-p2)
B. p2.getX()-p1.getX()
C. abs(p1.getY()-p2.getY())
D. abs(p1.getX()-p2.getX())

8.オブジェクト____を使用して、グラフィックウィンドウにテキスト入力を取得できます。
文章
B.エントリー
C.入力
D.キーボード

9.視覚要素とユーザーアクションを中心に構成されたユーザーインターフェイスは、____と呼ばれます。
A. GUI
B.アプリケーション
C.ウィンドウャー
D. API

10. Color_rgb(0,255,255)は____です。
A.イエロー
B.シアン
C.マゼンタ
D.オレンジ

回答

 1 D  2 B  3 D  4 C  5 D  6 D  7 D  8 B  9 A 10 B

話し合います

  1. データ(プロパティ、「何を知っているか」)とメソッド(動作、「できる」)をリストすることにより、プログラミングオブジェクトとして説明する興味深い実世界のオブジェクトの例を選択します。

回答

Refrigerator Data: volume Method: open the door (), put (), close the door () Put an elephant in the fridge: Refrigerator. Open the door () Refrigerator. Put in (elephant) Refrigerator. Close the door ()

2.グラフィックモジュールの以下の操作によって生成された各オブジェクトを、できるだけ正確に自分の言葉で説明してください。さまざまなオブジェクトのサイズ、場所、外観を必ず説明してください。必要に応じてスケッチを描くことができます。

 1 a. Point(130,130)  2  3 b. c = Circle(Point(30,40),25)  4 c.setFill('blue')  5 c.setOutline('red')  6  7 c. r = Rectangle(Point(20,20), Point(40,40))  8 r.setFill(color_rgb(0,255,150))  9 r.setWidth(3) 10 11 d. l = Line(Point(100,100), Point(100,200)) 12 l.setOutline('red4') 13 l.setArrow('first') 14 15 e. Oval(Point(50,50), Point(60,100)) 16 17 f. shape = Polygon(Point(5,5), Point(10,10), Point(5,10), Point(10,5)) 18 shape.setFill('orange') 19 20 g. t = Text(Point(100,100), 'Hello World!') 21 t.setFace('courier') 22 t.setSize(16) 23 t.setStyle('italic')

回答

a. One coordinate is (130,130)Point b. One center is (30,40), radius 25 Pixel red outline of blue circle c. One side is 20 pixels, center point is (30,30), with 3 Pixel wide black outline of blue green square d. A bottom is at the point (100,200), length 100 a dark red line of pixels, with an upward pointing arrow at the top e. a width of 10, the height is 50, the center is (55,75Unfilled 'erected' oval f. One by point (7.5,7.5) centered, black outline of the orange hourglass g. one to (100,100) 'Hello World!' text centered in 16-point italics

3.次のインタラクティブグラフィックプログラムを実行するとどうなるかを説明します。

 1 from graphics import *  2  3 def main():  4 win = GraphWin()  5 shape = Circle(Point(50,50), 20)  6 shape.setOutline('red')  7 shape.setFill('red')  8  shape.draw(win)  9 for i in range(10): 10 p = win.getMouse() 11 c = shape.getCenter() 12 dx = p.getX() - c.getX() 13 dy = p.getY() - c.getY() 14  shape.move(dx,dy) 15  win.close() 16 main()

回答

The program draws a red circle with a center of (50, 50) and a radius of 20 pixels in the default graphics window, and then waits for the user to click the mouse 10 times. Each time the user clicks, the circle moves to the point of click (which is centered on it). After 10 clicks, the window disappears.

プログラミングの練習

1.最後の手順を変更して問題について話し合い、次の手順を実行します。
a。円ではなく正方形を描くようにします。
b。 (既存の正方形を移動する代わりに)連続してクリックするたびに、画面上に追加の正方形を描画します。

回答

 1 from graphics import *  2  3 def main():  4 win = GraphWin()  5 shape = Rectangle(Point(30,30), Point(70,70))  6 shape.setOutline('red')  7 shape.setFill('red')  8  shape.draw(win)  9 for _ in range(10): 10 p = win.getMouse() 11 c = shape.getCenter() 12 dx = p.getX() - c.getX() 13 dy = p.getY() - c.getY() 14 shape = shape.clone() 15  shape.move(dx,dy) 16  shape.draw(win) 17  win.close() 18 19 main()

2.ターゲットの中心円は黄色で、赤、青、黒、白の同心円で囲まれています。各リングは同じです
幅は黄色い円の半径と同じです。そのような矢のターゲットを描くプログラムを書いてください。 (ヒント:後で描画されるオブジェクトは
以前に描画されたオブジェクトの上に表示されます。 )。

回答

 1 from graphics import *  2  3 def main():  4 win = GraphWin('Archery Target', 360, 360)  5 win.setCoords(-6, -6, 6, 6)  6 win.setBackground('gray')  7 center = Point(0,0)  8  9 c1 = Circle(center, 5) 10 c1.setFill('white') 11  c1.draw(win) 12 13 c2 = Circle(center, 4) 14 c2.setFill('black') 15  c2.draw(win) 16 17 c3 = Circle(center, 3) 18 c3.setFill('blue') 19  c3.draw(win) 20 21 c4 = Circle(center, 2) 22 c4.setFill('red') 23  c4.draw(win) 24 25 c5 = Circle(center, 1) 26 c5.setFill('yellow') 27  c5.draw(win) 28 29  win.getMouse() 30  win.close() 31 32 main()

  1. 顔を描くプログラムを書く。

回答

 Reference link 
 1 # Author: Cody Leistikow (10/4/02)  2  3 from graphics import *  4  5 def main():  6 win=GraphWin('Face',400,420)  7 win.setBackground('white')  8 neck = Polygon(Point(177,352),Point(176,371),Point(162,398),Point(170,400),Point(185,391),Point(231,373),Point(260,368),Point(287,357),Point(312,333),Point(235,311))  9 neck.setFill(color_rgb(239,222,215))  10 head = Polygon(Point(136,228),Point(107,260),Point(110,279),Point(129,302),Point(140,321),Point(175,350),Point(193,348),Point(234,312),Point(238,296),Point(255,291),Point(272,266),Point(271,240),Point(264,226),Point(253,224),Point(239,239),Point(243,193))  11 head.setFill(color_rgb(239,222,215))  12 headdark1 = Polygon(Point(184,212),Point(190,217),Point(194,261),Point(173,286),Point(198,284),Point(204,310),Point(203,321),Point(200,333),Point(192,346),Point(202,352),Point(206,351),Point(206,372),Point(225,339),Point(218,377),Point(236,371),Point(252,344),Point(258,342),Point(257,369),Point(286,358),Point(294,339),Point(300,343),Point(312,334),Point(295,319),Point(282,313),Point(276,292),Point(258,289),Point(242,296),Point(236,291),Point(240,284),Point(242,278),Point(247,278),Point(249,268),Point(240,265),Point(242,258),Point(234,261),Point(258,185))  13 headdark1.setFill(color_rgb(226,179,154))  14 headdark2 = Polygon(Point(133,231),Point(123,241),Point(112,273),Point(117,275),Point(148,227))  15 headdark2.setFill(color_rgb(226,179,154))  16 headdark3 = Polygon(Point(177,218),Point(161,279),Point(168,221))  17 headdark3.setFill(color_rgb(226,179,154))  18 neckline1 = Line(Point(182,365),Point(185,390))  19 neckline2 = Line(Point(199,385),Point(219,350))  20 neckline3 = Line(Point(219,350),Point(230,324))  21 neckline4 = Line(Point(225,374),Point(235,340))  22 neckline5 = Line(Point(282,313),Point(288,334))  23 throat = Polygon(Point(189,366),Point(192,381),Point(186,373))  24 throat.setFill(color_rgb(117,110,105))  25 headline1 = Line(Point(193,349),Point(212,334))  26 headline2 = Line(Point(212,334),Point(234,312))  27 headline3 = Line(Point(234,312),Point(241,294))  28 headline4 = Line(Point(135,279),Point(140,292))  29 headline5 = Line(Point(149,279),Point(154,289))  30 innerear = Polygon(Point(263,249),Point(262,240),Point(257,234),Point(241,259),Point(242,267),Point(248,269),Point(249,277),Point(242,279),Point(241,283),Point(260,271),Point(263,265),Point(253,260),Point(254,251))  31 innerear.setFill(color_rgb(201,153,145))  32 earline1 = Line(Point(247,249),Point(258,249))  33 earline2 = Line(Point(248,268),Point(255,264))  34 earline3 = Line(Point(241,261),Point(255,261))  35 earline4 = Line(Point(255,261),Point(257,272))  36 earline5 = Line(Point(257,272),Point(265,264))  37 chin = Polygon(Point(161,326),Point(176,327),Point(179,330),Point(165,330))  38 chin.setFill(color_rgb(215,178,182))  39 mouthline1 = Line(Point(154,320),Point(158,322))  40 mouthline2 = Line(Point(158,322),Point(171,318))  41 mouthline3 = Line(Point(171,318),Point(179,318))  42 noseline1 = Line(Point(146,303),Point(146,311))  43 noseline2 = Line(Point(146,311),Point(141,319))  44 noseline3 = Line(Point(141,319),Point(156,312))  45 lefteye = Polygon(Point(120,282),Point(140,293),Point(128,293))  46 lefteye.setFill(color_rgb(255,255,255))  47 righteye = Polygon(Point(163,286),Point(166,289),Point(197,277),Point(199,264))  48 righteye.setFill(color_rgb(255,255,255))  49 leftiris = Polygon(Point(128,287),Point(131,291),Point(137,290))  50 leftiris.setFill(color_rgb(100,150,131))  51 rightiris = Polygon(Point(168,282),Point(172,284),Point(179,275))  52 rightiris.setFill(color_rgb(100,150,131))  53 lefteyebrow = Polygon(Point(111,272),Point(140,290),Point(140,292),Point(111,276))  54 lefteyebrow.setFill(color_rgb(247,218,116))  55 righteyebrow = Polygon(Point(156,288),Point(209,251),Point(212,258),Point(157,288))  56 righteyebrow.setFill(color_rgb(247,218,116))  57 hairbg = Polygon(Point(49,110),Point(74,155),Point(89,103),Point(117,43),Point(127,96),Point(144,65),Point(198,21),Point(180,70),Point(230,32),Point(276,13),Point(241,77),Point(335,34),Point(310,74),Point(323,71),Point(331,65),Point(332,68),Point(363,45),Point(340,136),Point(356,125),Point(350,144),Point(381,124),Point(337,222),Point(309,240),Point(359,249),Point(294,276),Point(334,286),Point(275,297),Point(258,287),Point(272,264),Point(268,233),Point(264,225),Point(252,225),Point(239,238),Point(226,230),Point(228,219),Point(197,260),Point(210,221),Point(210,205),Point(175,217),Point(166,248),Point(142,294),Point(162,221),Point(136,230),Point(106,261),Point(110,277),Point(114,289),Point(116,317),Point(93,288),Point(94,278),Point(75,318),Point(76,274),Point(62,273),Point(69,247),Point(18,253),Point(67,215),Point(53,191))  58 hairbg.setFill(color_rgb(247,218,116))  59 hairdark1 = Polygon(Point(50,114),Point(74,156),Point(71,177))  60 hairdark1.setFill(color_rgb(235,161,56))  61 hairdark2 = Polygon(Point(47,243),Point(93,225),Point(89,239))  62 hairdark2.setFill(color_rgb(235,161,56))  63 hairdark3 = Polygon(Point(128,214),Point(94,256),Point(75,317),Point(99,270))  64 hairdark3.setFill(color_rgb(235,161,56))  65 hairdark4 = Polygon(Point(105,266),Point(116,313),Point(114,283))  66 hairdark4.setFill(color_rgb(235,161,56))  67 hairdark5 = Polygon(Point(119,45),Point(107,106),Point(112,170),Point(124,203),Point(129,200),Point(130,164),Point(121,139),Point(125,91),Point(117,43))  68 hairdark5.setFill(color_rgb(235,161,56))  69 hairdark6 = Polygon(Point(173,126),Point(145,193),Point(157,201),Point(157,216),Point(169,204),Point(186,176),Point(201,161),Point(213,122),Point(223,79),Point(234,55),Point(204,109),Point(160,186),Point(155,187),Point(179,117))  70 hairdark6.setFill(color_rgb(235,161,56))  71 hairdark7 = Polygon(Point(176,217),Point(253,115),Point(225,194),Point(212,192),Point(203,207))  72 hairdark7.setFill(color_rgb(235,161,56))  73 hairdark8 = Polygon(Point(222,86),Point(272,19),Point(240,77))  74 hairdark8.setFill(color_rgb(235,161,56))  75 hairdark9 = Polygon(Point(217,111),Point(284,65),Point(209,138))  76 hairdark9.setFill(color_rgb(235,161,56))  77 hairdark10 = Polygon(Point(217,192),Point(219,230),Point(230,216),Point(224,193))  78 hairdark10.setFill(color_rgb(235,161,56))  79 hairdark11 = Polygon(Point(216,109),Point(252,84),Point(283,66),Point(208,141))  80 hairdark11.setFill(color_rgb(235,161,56))  81 hairdark12 = Polygon(Point(238,161),Point(274,140),Point(228,185))  82 hairdark12.setFill(color_rgb(235,161,56))  83 hairdark13 = Polygon(Point(248,144),Point(298,73),Point(335,33),Point(307,77),Point(303,97))  84 hairdark13.setFill(color_rgb(235,161,56))  85 hairdark14 = Polygon(Point(249,205),Point(300,142),Point(331,67),Point(321,152))  86 hairdark14.setFill(color_rgb(235,161,56))  87 hairdark15 = Polygon(Point(321,152),Point(324,137),Point(357,65),Point(340,135))  88 hairdark15.setFill(color_rgb(235,161,56))  89 hairdark16 = Polygon(Point(142,138),Point(160,77),Point(180,48),Point(166,87),Point(166,114))  90 hairdark16.setFill(color_rgb(235,161,56))  91 hairdark17 = Polygon(Point(226,198),Point(252,183),Point(242,203),Point(246,216),Point(301,184),Point(282,219),Point(331,174),Point(349,144),Point(322,210),Point(358,174),Point(338,221),Point(308,240),Point(294,239),Point(283,256),Point(357,250),Point(276,282),Point(331,286),Point(276,296),Point(258,287),Point(273,265),Point(270,239),Point(265,224),Point(254,224),Point(240,238),Point(227,231),Point(228,220),Point(227,207))  92 hairdark17.setFill(color_rgb(235,161,56))  93 hairline1 = Line(Point(72,176),Point(84,206))  94 hairline2 = Line(Point(68,215),Point(84,206))  95 hairline3 = Line(Point(84,206),Point(109,208))  96 hairline4 = Line(Point(70,246),Point(86,246))  97 hairline5 = Line(Point(98,214),Point(77,276))  98 hairline6 = Line(Point(98,214),Point(146,194))  99 hairline7 = Line(Point(128,196),Point(133,147)) 100 hairline8 = Line(Point(133,147),Point(207,72)) 101 hairline9 = Line(Point(207,72),Point(235,53)) 102 hairline10 = Line(Point(137,230),Point(158,215)) 103 hairline11 = Line(Point(127,95),Point(141,140)) 104 hairline12 = Line(Point(180,70),Point(174,104)) 105 hairline13 = Line(Point(22,252),Point(94,225)) 106 hairline14 = Line(Point(202,160),Point(254,113)) 107 hairline15 = Line(Point(173,125),Point(210,79)) 108 hairline16 = Line(Point(175,217),Point(171,205)) 109 hairline17 = Line(Point(171,205),Point(165,208)) 110 hairline18 = Line(Point(305,96),Point(332,64)) 111 hairline19 = Line(Point(275,139),Point(315,94)) 112 hairline20 = Line(Point(98,112),Point(91,174)) 113 hairline21 = Line(Point(91,174),Point(99,208)) 114 hairline22 = Line(Point(128,214),Point(156,201)) 115 hairline23 = Line(Point(132,113),Point(163,50)) 116 hairline24 = Line(Point(185,92),Point(240,32)) 117 hairline25 = Line(Point(271,245),Point(295,233)) 118 hairline26 = Line(Point(295,233),Point(322,209)) 119 hairline27 = Line(Point(227,190),Point(286,139)) 120 hairline28 = Line(Point(302,183),Point(326,164)) 121 122  neck.draw(win) 123  head.draw(win) 124  headdark1.draw(win) 125  headdark2.draw(win) 126  headdark3.draw(win) 127  headline1.draw(win) 128  headline2.draw(win) 129  headline3.draw(win) 130  headline4.draw(win) 131  headline5.draw(win) 132  neckline1.draw(win) 133  neckline2.draw(win) 134  neckline3.draw(win) 135  neckline4.draw(win) 136  neckline5.draw(win) 137  throat.draw(win) 138  innerear.draw(win) 139  earline1.draw(win) 140  earline2.draw(win) 141  earline3.draw(win) 142  earline4.draw(win) 143  earline5.draw(win) 144  chin.draw(win) 145  mouthline1.draw(win) 146  mouthline2.draw(win) 147  mouthline3.draw(win) 148  noseline1.draw(win) 149  noseline2.draw(win) 150  noseline3.draw(win) 151  lefteye.draw(win) 152  righteye.draw(win) 153  leftiris.draw(win) 154  rightiris.draw(win) 155  lefteyebrow.draw(win) 156  righteyebrow.draw(win) 157  hairbg.draw(win) 158  hairdark1.draw(win) 159  hairdark2.draw(win) 160  hairdark3.draw(win) 161  hairdark4.draw(win) 162  hairdark5.draw(win) 163  hairdark6.draw(win) 164  hairdark7.draw(win) 165  hairdark8.draw(win) 166  hairdark9.draw(win) 167  hairdark10.draw(win) 168  hairdark11.draw(win) 169  hairdark12.draw(win) 170  hairdark13.draw(win) 171  hairdark14.draw(win) 172  hairdark15.draw(win) 173  hairdark16.draw(win) 174  hairdark17.draw(win) 175  hairline1.draw(win) 176  hairline2.draw(win) 177  hairline3.draw(win) 178  hairline4.draw(win) 179  hairline5.draw(win) 180  hairline6.draw(win) 181  hairline7.draw(win) 182  hairline8.draw(win) 183  hairline9.draw(win) 184  hairline10.draw(win) 185  hairline11.draw(win) 186  hairline12.draw(win) 187  hairline13.draw(win) 188  hairline14.draw(win) 189  hairline15.draw(win) 190  hairline16.draw(win) 191  hairline17.draw(win) 192  hairline18.draw(win) 193  hairline19.draw(win) 194  hairline20.draw(win) 195  hairline21.draw(win) 196  hairline22.draw(win) 197  hairline23.draw(win) 198  hairline24.draw(win) 199  hairline25.draw(win) 200  hairline26.draw(win) 201  hairline27.draw(win) 202  hairline28.draw(win) 203  win.getMouse() 204 205 main()

4.クリスマスツリーと雪だるまで冬のシーンを描くプログラムを作成します。

回答

 Reference link 
 1 # By: Megan Neuendorf  2  3 from graphics import *  4  5 def main():  6 win=GraphWin('Winter Scene',500,500)  7 win.setCoords(0,0,200,200)  8 win.setBackground('blue')  9  10 # Body of the snowman  11 c1 = Circle(Point(50,40),40)  12  c1.draw(win)  13 c1.setFill('white')  14 c1.setOutline('white')  15  16 c2 = Circle(Point(50,100),30)  17  c2.draw(win)  18 c2.setFill('white')  19 c2.setOutline('white')  20  21 c3 = Circle(Point(50,145),20)  22  c3.draw(win)  23 c3.setFill('white')  24 c3.setOutline('white')  25  26 # Top Hat of Snowman  27 r1 = Rectangle(Point(30,160),Point(70,165))  28  r1.draw(win)  29 r1.setFill('black')  30  31 r2 = Rectangle(Point(40,165),Point(60,185))  32  r2.draw(win)  33 r2.setFill('black')  34  35 # Eyes of the Snowman  36 e1 = Circle(Point(42.5,150),2.5)  37  e1.draw(win)  38 e1.setFill('black')  39  40 e2 = Circle(Point(57.5,150),2.5)  41  e2.draw(win)  42 e2.setFill('black')  43  44 # Nose of the Snowman  45 n = Polygon(Point(50,142.5),Point(50,137.5),Point(57.5,140))  46  n.draw(win)  47 n.setOutline('orange')  48 n.setFill('orange')  49  50 # Mouth of the Snowman  51 m1 = Circle(Point(40,135),1)  52  m1.draw(win)  53 m1.setFill('black')  54  55 m2 = Circle(Point(45,130),1)  56  m2.draw(win)  57 m2.setFill('black')  58  59 m3 = Circle(Point(50,127.5),1)  60  m3.draw(win)  61 m3.setFill('black')  62  63 m4 = Circle(Point(55,130),1)  64  m4.draw(win)  65 m4.setFill('black')  66  67 m5 = Circle(Point(60,135),1)  68  m5.draw(win)  69 m5.setFill('black')  70  71 # Buttons on the Snowman  72 b1 = Circle(Point(50,115),3)  73  b1.draw(win)  74 b1.setFill('black')  75  76 b2 = Circle(Point(50,105),3)  77  b2.draw(win)  78 b2.setFill('black')  79  80 b3 = Circle(Point(50,95),3)  81  b3.draw(win)  82 b3.setFill('black')  83  84 # Christmas Tree  85 rect1 = Rectangle(Point(140,0),Point(160,25))  86  rect1.draw(win)  87 rect1.setOutline('brown')  88 rect1.setFill('brown')  89  90 t1 = Polygon(Point(100,25),Point(200,25),Point(150,65))  91  t1.draw(win)  92 t1.setOutline('forest green')  93 t1.setFill('forest green')  94  95 t2 = Polygon(Point(110,60),Point(190,60),Point(150,100))  96  t2.draw(win)  97 t2.setOutline('forest green')  98 t2.setFill('forest green')  99 100 t3 = Polygon(Point(120,90),Point(180,90),Point(150,120)) 101  t3.draw(win) 102 t3.setOutline('forest green') 103 t3.setFill('forest green') 104 105 t4 = Polygon(Point(130,115),Point(170,115),Point(150,135)) 106  t4.draw(win) 107 t4.setOutline('forest green') 108 t4.setFill('forest green') 109 110 t5 = Polygon(Point(135,132.5),Point(165,132.5),Point(150,155)) 111  t5.draw(win) 112 t5.setOutline('forest green') 113 t5.setFill('forest green') 114 115 # Star on the Christmas Tree 116 n = Polygon(Point(150,152.5),Point(147.5,160),Point(140,162.5),Point(147.5,165),Point(150,172.5),Point(152.5,165),Point(160,162.5),Point(152.5,160)) 117  n.draw(win) 118 n.setOutline('gold') 119 n.setFill('gold') 120 121 # Circular Ornaments on Christmas Tree 122 o1 = Circle(Point(150,142.5),2.5) 123  o1.draw(win) 124 o1.setOutline('red') 125 o1.setFill('gold') 126 127 o2 = Circle(Point(135,115),2.5) 128  o2.draw(win) 129 o2.setOutline('red') 130 o2.setFill('gold') 131 132 o3 = Circle(Point(165,115),2.5) 133  o3.draw(win) 134 o3.setOutline('red') 135 o3.setFill('gold') 136 137 o4 = Circle(Point(150,95),2.5) 138  o4.draw(win) 139 o4.setOutline('red') 140 o4.setFill('gold') 141 142 o5 = Circle(Point(135,75),2.5) 143  o5.draw(win) 144 o5.setOutline('red') 145 o5.setFill('gold') 146 147 o6 = Circle(Point(165,75),2.5) 148  o6.draw(win) 149 o6.setOutline('red') 150 o6.setFill('gold') 151 152 o7 = Circle(Point(115,60),2.5) 153  o7.draw(win) 154 o7.setOutline('red') 155 o7.setFill('gold') 156 157 o8 = Circle(Point(185,60),2.5) 158  o8.draw(win) 159 o8.setOutline('red') 160 o8.setFill('gold') 161 162 o9 = Circle(Point(150,30),2.5) 163  o9.draw(win) 164 o9.setOutline('red') 165 o9.setFill('gold') 166 167 # Diamond Ornaments on Christmas Tree 168 d1 = Polygon(Point(140,135),Point(142.5,132.5),Point(140,130),Point(137.5,132.5)) 169  d1.draw(win) 170 d1.setOutline('gold') 171 d1.setFill('red') 172 173 d2 = Polygon(Point(160,135),Point(162.5,132.5),Point(160,130),Point(157.5,132.5)) 174  d2.draw(win) 175 d2.setOutline('gold') 176 d2.setFill('red') 177 178 d3 = Polygon(Point(150,122.5),Point(152.5,120),Point(150,117.5),Point(147.5,120)) 179  d3.draw(win) 180 d3.setOutline('gold') 181 d3.setFill('red') 182 183 d4 = Polygon(Point(125,92.5),Point(127.5,90),Point(125,87.5),Point(122.5,90)) 184  d4.draw(win) 185 d4.setOutline('gold') 186 d4.setFill('red') 187 188 d5 = Polygon(Point(175,92.5),Point(177.5,90),Point(175,87.5),Point(172.5,90)) 189  d5.draw(win) 190 d5.setOutline('gold') 191 d5.setFill('red') 192 193 d6 = Polygon(Point(150,67.5),Point(152.5,65),Point(150,62.5),Point(147.5,65)) 194  d6.draw(win) 195 d6.setOutline('gold') 196 d6.setFill('red') 197 198 d7 = Polygon(Point(130,47.5),Point(132.5,45),Point(130,42.5),Point(127.5,45)) 199  d7.draw(win) 200 d7.setOutline('gold') 201 d7.setFill('red') 202 203 d8 = Polygon(Point(170,47.5),Point(172.5,45),Point(170,42.5),Point(167.5,45)) 204  d8.draw(win) 205 d8.setOutline('gold') 206 d8.setFill('red') 207 208 d9 = Polygon(Point(105,27.5),Point(107.5,25),Point(105,22.5),Point(102.5,25)) 209  d9.draw(win) 210 d9.setOutline('gold') 211 d9.setFill('red') 212 213 d10 = Polygon(Point(195,27.5),Point(197.5,25),Point(195,22.5),Point(192.5,25)) 214  d10.draw(win) 215 d10.setOutline('gold') 216 d10.setFill('red') 217 218  win.getMouse() 219  win.close() 220 221 main()

5.画面に5つのサイコロを引くプログラムを作成します。これはまっすぐです(1、2、3、4、5または2、3、4、5、6)。

回答

 1 from graphics import *  2  3 def main():  4 win = GraphWin('Dice', , )  5 win.setCoords(-10, -5, 10, 5)  6  7 d1 = Rectangle(Point(-9, -1), Point(-7, 1))  8  d1.draw(win)  9 c = Circle(Point(-8, 0), 0.3) 10 c.setFill('red') 11 c.setOutline('white') 12  c.draw(win) 13 14 d2 = d1.clone() 15 d2.move(4, 0) 16  d2.draw(win) 17 c = Circle(Point(-4.3, 0.3), 0.25) 18 c.setFill('blue') 19 c.setOutline('white') 20  c.draw(win) 21 c = c.clone() 22 c.move(0.6, -0.6) 23  c.draw(win) 24 25 d3 = d2.clone() 26 d3.move(4, 0) 27  d3.draw(win) 28 29 c = Circle(Point(-0.5, 0.5), 0.25) 30 c.setFill('blue') 31 c.setOutline('white') 32  c.draw(win) 33 c = c.clone() 34 c.move(0.5, -0.5) 35  c.draw(win) 36 c = c.clone() 37 c.move(0.5, -0.5) 38  c.draw(win) 39 40 d4 = d3.clone() 41 d4.move(4, 0) 42  d4.draw(win) 43 c = Circle(Point(3.6, 0.4), 0.25) 44 c.setFill('red') 45 c.setOutline('white') 46  c.draw(win) 47 c = c.clone() 48 c.move(0.8, 0) 49  c.draw(win) 50 c = c.clone() 51 c.move(0, -0.8) 52  c.draw(win) 53 c = c.clone() 54 c.move(-0.8, 0) 55  c.draw(win) 56 57 d5 = d4.clone() 58 d5.move(4, 0) 59  d5.draw(win) 60 c = Circle(Point(7.5, 0.5), 0.25) 61 c.setFill('blue') 62 c.setOutline('white') 63  c.draw(win) 64 c = c.clone() 65 c.move(1, 0) 66  c.draw(win) 67 c = c.clone() 68 c.move(0, -1) 69  c.draw(win) 70 c = c.clone() 71 c.move(-1, 0) 72  c.draw(win) 73 c = c.clone() 74 c.move(0.5, 0.5) 75  c.draw(win) 76 77  win.getMouse() 78  win.close() 79 80 main()

 1 from graphics import *  2  3 def main():  4 win = GraphWin('Dice', , )  5 win.setCoords(-10, -5, 10, 5)  6  7 d2 = Rectangle(Point(-9, -1), Point(-7, 1))  8  d2.draw(win)  9 c = Circle(Point(-8.3, 0.3), 0.25) 10 c.setFill('blue') 11 c.setOutline(color_rgb(240, 240, 240)) 12  c.draw(win) 13 c = c.clone() 14 c.move(0.6, -0.6) 15  c.draw(win) 16 17 d3 = d2.clone() 18 d3.move(4, 0) 19  d3.draw(win) 20 c = Circle(Point(-4.5, 0.5), 0.25) 21 c.setFill('blue') 22 c.setOutline(color_rgb(240, 240, 240)) 23  c.draw(win) 24 c = c.clone() 25 c.move(0.5, -0.5) 26  c.draw(win) 27 c = c.clone() 28 c.move(0.5, -0.5) 29  c.draw(win) 30 31 d4 = d3.clone() 32 d4.move(4, 0) 33  d4.draw(win) 34 c = Circle(Point(-0.4, 0.4), 0.25) 35 c.setFill('red') 36 c.setOutline(color_rgb(240, 240, 240)) 37  c.draw(win) 38 c = c.clone() 39 c.move(0.8, 0) 40  c.draw(win) 41 c = c.clone() 42 c.move(0, -0.8) 43  c.draw(win) 44 c = c.clone() 45 c.move(-0.8, 0) 46  c.draw(win) 47 48 d5 = d4.clone() 49 d5.move(4, 0) 50  d5.draw(win) 51 c = Circle(Point(3.5, 0.5), 0.25) 52 c.setFill('blue') 53 c.setOutline(color_rgb(240, 240, 240)) 54  c.draw(win) 55 c = c.clone() 56 c.move(1, 0) 57  c.draw(win) 58 c = c.clone() 59 c.move(0, -1) 60  c.draw(win) 61 c = c.clone() 62 c.move(-1, 0) 63  c.draw(win) 64 c = c.clone() 65 c.move(0.5, 0.5) 66  c.draw(win) 67 68 d6 = d5.clone() 69 d6.move(4, 0) 70  d6.draw(win) 71 c = Circle(Point(7.6, 0.6), 0.25) 72 c.setFill('blue') 73 c.setOutline(color_rgb(240, 240, 240)) 74  c.draw(win) 75 c = c.clone() 76 c.move(0.8, 0) 77  c.draw(win) 78 c = c.clone() 79 c.move(0, -0.6) 80  c.draw(win) 81 c = c.clone() 82 c.move(-0.8, 0) 83  c.draw(win) 84 c = c.clone() 85 c.move(0, -0.6) 86  c.draw(win) 87 c = c.clone() 88 c.move(0.8, 0) 89  c.draw(win) 90 91  win.getMouse() 92  win.close() 93 94 main()

6.グラフィックスの最終値プログラムを変更して、入力(principalおよびAPR)もEntryオブジェクトを使用してグラフィカルに実行されるようにします。

回答

 Reference link 
 1 from graphics import *  2  3 def main():  4 win = GraphWin('Investment Growth Chart', 640, 480)  5 win.setBackground('white')  6 # Set coordinates for easy display of prompts.  7 win.setCoords(0,0,10,10)  8  9 # Display prompts 10 t1 = Text(Point(5,8), 'Plotting a 10 year investment') 11 t1.setSize(14) 12  t1.draw(win) 13 14 t2 = Text(Point(5,7.5), 'Enter the information below and then click anywhere') 15 t2.setSize(14) 16  t2.draw(win) 17 18 t3 = Text(Point(2,6), 'Initial Principal:') 19 t3.setSize(14) 20  t3.draw(win) 21 22 prinBox = Entry(Point(4.5,6), 6) 23  prinBox.draw(win) 24 prinBox.setText('2000') 25 26 t4 = Text(Point(2,4), 'Annual Interest Rate:') 27 t4.setSize(14) 28  t4.draw(win) 29 30 aprBox = Entry(Point(4.5,4), 6) 31 aprBox.setText('0.05') 32  aprBox.draw(win) 33 34 # wait for mouse click and get values 35  win.getMouse() 36 principal = float(prinBox.getText()) 37 apr = float(aprBox.getText()) 38 39 # Erase the prompts 40  t1.undraw() 41  t2.undraw() 42  t3.undraw() 43  t4.undraw() 44  prinBox.undraw() 45  aprBox.undraw() 46 47 48 # Set Window coords for drawing the graph. 49 win.setCoords(-1.75,-200, 11.5, 10400) 50 51 # Create a graphics window with labels on left edge 52 Text(Point(-1, 0), ' 0.0K').draw(win) 53 Text(Point(-1, 2500), ' 2.5K').draw(win) 54 Text(Point(-1, 5000), ' 5.0K').draw(win) 55 Text(Point(-1, 7500), ' 7.5k').draw(win) 56 Text(Point(-1, 10000), '10.0K').draw(win) 57 58 # Draw bar for initial principal 59 bar = Rectangle(Point(0, 0), Point(1, principal)) 60 bar.setFill('green') 61 bar.setWidth(2) 62  bar.draw(win) 63 64 # Draw a bar for each subsequent year 65 for year in range(1, 11): 66 principal = principal * (1 + apr) 67 bar = Rectangle(Point(year, 0), Point(year+1, principal)) 68 bar.setFill('green') 69 bar.setWidth(2) 70  bar.draw(win) 71 72  win.getMouse() 73  win.close() 74 75 main()

7、円の交点。
円と水平線の交点を計算し、その情報をテキストとグラフィックで表示するプログラムを作成します。
入力:円の半径と線のy切片。
出力:座標が(-10、-10)から(10,10)で、(0、0)を中心とし、指定された半径で描画されたウィンドウ内
サークル。
与えられたy軸でウィンドウを通る水平線を取ります。
赤で2つの交差点を描画します。
交点のx値を出力します。
式:

回答

 1 from graphics import *  2 import math  3  4 def main():  5 radius = float(input('Please enter the radius of the circle: '))  6 yinter = float(input('Please enter the y-intercept of the line: '))  7  8 win = GraphWin('Circle Intersection')  9 win.setCoords(-10, -10, 10, 10) 10 11  Circle(Point(0, 0), radius).draw(win) 12 Line(Point(-10, yinter), Point(10, yinter)).draw(win) 13 14 x = math.sqrt(radius * radius - yinter * yinter) 15 print('X values of intersection', -x, x) 16 17 p1 = Circle(Point(x, yinter), 0.25) 18 p1.setOutline('red') 19 p1.setFill('red') 20  p1.draw(win) 21 22 p2 = p1.clone() 23 p2.move(-2 * x, 0) 24  p2.draw(win) 25 26  win.getMouse() 27  win.close() 28 29 main() 30 31 # Output: 32 # Please enter the radius of the circle: 5 33 # Please enter the y-intercept of the line: 3 34 # X values of intersection -4.0 4.0

8、線分情報。
このプログラムを使用すると、ユーザーは線分を描画してから、線分に関するグラフィック情報とテキスト情報を表示できます。
入力:マウスを2回クリックしたときの線分の終点。
出力:線分の中点をシアンで描画します。
線分を描画します。
印刷行の長さと傾き。
式:

回答

 1 from graphics import *  2 import math  3  4 def main():  5 win = GraphWin('Triangle', 400, 400)  6 win.setCoords(-5, -5, 5, 5)  7  8 draw_coord(win) # This function is defined at the end of the article.  9 10 pt1 = win.getMouse() 11  pt1.draw(win) 12 pt2 = win.getMouse() 13  pt2.draw(win) 14 line = Line(pt1, pt2) 15  line.draw(win) 16 mark = Circle(line.getCenter(),0.075) 17 mark.setFill('cyan') 18  mark.draw(win) 19 20 dx = pt2.getX() - pt1.getX() 21 dy = pt2.getY() - pt1.getY() 22 slope = dy / dx 23 length = math.sqrt(dx * dx + dy * dy) 24 25 message = Text(Point(0, -4), 'Point1:({}, {})
Point2:({}, {})
length: {}
slope:{}'.format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), length, slope)) 26 message.setSize(8) 27 message.setTextColor(color_rgb(144, 144, 144)) 28  message.draw(win) 29 30  win.getMouse() 31  win.close() 32 33 main()

9、長方形の情報。

このプログラムは、ユーザーが描いた長方形に関する情報を表示します。
入力:長方形の対角線としてマウスを2回クリックします。
出力:長方形を描画します。
長方形の周囲と面積を印刷します。
式:面積=(長さ)(幅)
周囲長= 2(長さ+幅)

回答

 1 from graphics import *  2  3 def main():  4 win = GraphWin('Rectangle', 400, 400)  5 win.setCoords(-5, -5, 5, 5)  6  7  draw_coord(win)  8  9 pt1 = win.getMouse() 10 pt2 = win.getMouse() 11 rect = Rectangle(pt1, pt2) 12  rect.draw(win) 13 14 height = abs(pt2.getY() - pt1.getY()) 15 length = abs(pt2.getX() - pt1.getX()) 16 17 area = height * length 18 perimeter = 2 * (height + length) 19 20 message = Text(Point(0, -4), 'Point1:({}, {})
Point2:({}, {})
 Area: {}
Wence:{}'.format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), area, perimeter)) 21 message.setSize(8) 22 message.setTextColor(color_rgb(144, 144, 144)) 23  message.draw(win) 24 25  win.getMouse() 26  win.close() 27 28 main()

10.三角形の情報。
前の質問と同じですが、三角形の頂点は3回クリックされます。
式:周囲長については、線分の問題の長さを参照してください。

回答

 1 from graphics import *  2 import math  3  4 def main():  5 win = GraphWin('Triangle', 400, 400)  6 win.setCoords(-5, -5, 5, 5)  7  8  draw_coord(win)  9 10 pt1 = win.getMouse() 11  pt1.draw(win) 12 pt2 = win.getMouse() 13  pt1.undraw() 14 line = Line(pt1, pt2) 15  line.draw(win) 16 pt3 = win.getMouse() 17  line.undraw() 18 tri = Polygon(pt1, pt2, pt3) 19  tri.draw(win) 20 21 dx1 = pt1.getX() - pt2.getX() 22 dy1 = pt1.getY() - pt2.getY() 23 dx2 = pt2.getX() - pt3.getX() 24 dy2 = pt2.getY() - pt3.getY() 25 dx3 = pt3.getX() - pt1.getX() 26 dy3 = pt3.getY() - pt1.getY() 27 28 a = math.sqrt(dx1 * dx1 + dy1 * dy1) 29 b = math.sqrt(dx2 * dx2 + dy2 * dy2) 30 c = math.sqrt(dx3 * dx3 + dy3 * dy3) 31 32 perimeter = a + b + c 33 s = perimeter / 2 34 area = math.sqrt(s * (s - a) * (s - b) * (s - c)) 35 36 message = Text(Point(0, -4), 'Point1:({}, {})
Point2:({}, {})
Point3:({}, {})
 Area: {}
Circum:{}'.format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), pt3.getX(), pt3.getY(), area, perimeter)) 37 message.setSize(8) 38 message.setTextColor(color_rgb(144, 144, 144)) 39  message.draw(win) 40 41  win.getMouse() 42  win.close() 43 44 main()

十一、 家を5回クリックします。

ユーザーがマウスを5回クリックするだけで簡単な家を描くことができるプログラムを作成します。最初の2回のクリックは、家の長方形のフレームの対角線です。 3回目のクリックは、長方形のドアの上端の中心を示します。ドアの幅は、家のフレームの幅の1/5にする必要があります。ドアの境界は、フレームの上部の角から下部まで伸びている必要があります。 4回目のクリックは、正方形のウィンドウの中心を示します。窓の幅はドアの半分です。最後のクリックは、屋根の頂点を示します。屋根の端は、家のフレームの頂点から上端の角まで伸びます。
回答

 1 from graphics import *  2  3 def main():  4 win = GraphWin(, )  5  6 frameLL = win.getMouse()  7  frameLL.draw(win)  8 frameUR = win.getMouse()  9  frameLL.undraw() 10  Rectangle(frameLL, frameUR).draw(win) 11 12 doorUC = win.getMouse() 13 houseWidth = frameUR.getX() - frameLL.getX() 14 doorWidth = houseWidth * 0.2 15 doorLL = Point(doorUC.getX() - doorWidth / 2, frameLL.getY()) 16 doorUR = Point(doorUC.getX() + doorWidth / 2, doorUC.getY()) 17  Rectangle(doorLL, doorUR).draw(win) 18 19 windowCenter = win.getMouse() 20 windowWidth = doorWidth / 2 21 windowLL = Point(windowCenter.getX() - windowWidth / 2, windowCenter.getY() - windowWidth / 2) 22 windowUR = Point(windowCenter.getX() + windowWidth / 2, windowCenter.getY() + windowWidth / 2) 23  Rectangle(windowLL, windowUR).draw(win) 24 25 peak = win.getMouse() 26 roofL = Line(Point(frameLL.getX(), frameUR.getY()), peak) 27 roofR = Line(peak, frameUR) 28  roofL.draw(win) 29  roofR.draw(win) 30 31  win.getMouse() 32  win.close() 33 34 main()

Draw_coord() implementation:
 1 def draw_coord(win):  2 for i in range(5, -6, -1):  3 l = Line(Point(-5, i), Point(5, i))  4 l.setFill(color_rgb(192, 192, 192))  5  l.draw(win)  6 for i in range(-5, 6, 1):  7 l = Line(Point(i, 5), Point(i, -5))  8 l.setFill(color_rgb(192, 192, 192))  9  l.draw(win) 10 11 x_axis = Line(Point(-5, 0), Point(5, 0)) 12 x_axis.setArrow('last') 13 x_axis.setFill(color_rgb(144, 144, 144)) 14  x_axis.draw(win) 15 x_text = Text(Point(4.75, -0.25), 'x') 16 x_text.setFill(color_rgb(144, 144, 144)) 17  x_text.draw(win) 18 19 y_axis = Line(Point(0, -5), Point(0, 5)) 20 y_axis.setArrow('last') 21 y_axis.setFill(color_rgb(144, 144, 144)) 22  y_axis.draw(win) 23 y_text = Text(Point(0.25, 4.75), 'y') 24 y_text.setFill(color_rgb(144, 144, 144)) 25  y_text.draw(win) 26 27 base_point = Text(Point(-0.25, -0.25), '0') 28 base_point.setFill(color_rgb(144, 144, 144)) 29 base_point.draw(win)

転載:https://www.cnblogs.com/zsh-blogs/p/10005468.html