• 0

[python] drawing a semi circle


Question

hi,

I was trying to draw a semi circle in python using the circle function in the turtle module. The documentation is here: http://docs.python.org/lib/module-turtle.html. I don't understand what the extent parameter does. Can you please explain it to me? And if it isn't too much trouble can you post code that will draw a semi circle? Thanks.

Link to comment
https://www.neowin.net/forum/topic/473049-python-drawing-a-semi-circle/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Have never had to do a semi-circle and can't test it right now, but try this and see what happens:

from turtle import *
import time

color("green")
up()
goto(0,-50)
down()
circle(50, 45)
up()
goto(-150,-120)
color("red")
write("Done!")

time.sleep(5)

FTFM: If extent is not a full circle, one endpoint of the arc is the current pen position. The arc is drawn in a counter clockwise direction if radius is positive, otherwise in a clockwise direction. In the process, the direction of the turtle is changed by the amount of the extent.

This from the turtle.py source might help:

0154	 def circle(self, radius, extent=None):
0155		 if extent is None:
0156			 extent = self._fullcircle
0157		 x0, y0 = self._position
0158		 xc = x0 - radius * sin(self._angle * self._invradian)
0159		 yc = y0 - radius * cos(self._angle * self._invradian)
0160		 if radius >= 0.0:
0161			 start = self._angle - 90.0
0162		 else:
0163			 start = self._angle + 90.0
0164			 extent = -extent
0165		 if self._filling:
0166			 if abs(extent) >= self._fullcircle:
0167				 item = self._canvas.create_oval(xc-radius, yc-radius,
0168												 xc+radius, yc+radius,
0169												 width=self._width,
0170												 outline="")
0171				 self._tofill.append(item)
0172			 item = self._canvas.create_arc(xc-radius, yc-radius,
0173											xc+radius, yc+radius,
0174											style="chord",
0175											start=start,
0176											extent=extent,
0177											width=self._width,
0178											outline="")
0179			 self._tofill.append(item)
0180		 if self._drawing:
0181			 if abs(extent) >= self._fullcircle:
0182				 item = self._canvas.create_oval(xc-radius, yc-radius,
0183												 xc+radius, yc+radius,
0184												 width=self._width,
0185												 outline=self._color)
0186				 self._items.append(item)
0187			 item = self._canvas.create_arc(xc-radius, yc-radius,
0188											xc+radius, yc+radius,
0189											style="arc",
0190											start=start,
0191											extent=extent,
0192											width=self._width,
0193											outline=self._color)
0194			 self._items.append(item)
0195		 angle = start + extent
0196		 x1 = xc + abs(radius) * cos(angle * self._invradian)
0197		 y1 = yc - abs(radius) * sin(angle * self._invradian)
0198		 self._angle = (self._angle + extent) % self._fullcircle
0199		 self._position = x1, y1
0200		 if self._filling:
0201			 self._path.append(self._position)
0202		 self._draw_turtle()

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.