How can I make a semi-circle using Zelle graphics in Python?

3.2k Views Asked by At

How can I make a semi-circle in Python's Zelle graphics package? This code makes me a circle.

balldistance=40;
ball1=Circle(Point(spacing*b+spacing-150,FieldHeight-GroundDepth),ball1);
ball1.setFill("red");
ball1.draw(Field);
1

There are 1 best solutions below

0
On

The Zelle graphics module doesn't provide code for drawing a semi-circle (arc) directly. However, since the module is written in Python, built on tkinter, and tkinter provides an arc drawing routine, we can add our own Arc subclass that inherits from the Zelle Oval class and implements arcs:

from graphics import *

class Arc(Oval):

    def __init__(self, p1, p2, extent):
        self.extent = extent
        super().__init__(p1, p2)

    def __repr__(self):
        return "Arc({}, {}, {})".format(str(self.p1), str(self.p2), self.extent)

    def clone(self):
        other = Arc(self.p1, self.p2, self.extent)
        other.config = self.config.copy()
        return other

    def _draw(self, canvas, options):
        p1 = self.p1
        p2 = self.p2
        x1, y1 = canvas.toScreen(p1.x, p1.y)
        x2, y2 = canvas.toScreen(p2.x, p2.y)
        options['style'] = tk.CHORD
        options['extent'] = self.extent
        return canvas.create_arc(x1, y1, x2, y2, options)


win = GraphWin("My arc example", 200, 200)

arc = Arc(Point(50, 50), Point(100, 100), 180)
arc.setFill("red")
arc.draw(win)

win.getMouse()
win.close()

OUTPUT

enter image description here