Program to Draw Concentric Circles in Computer Graphics

Drawing Circles with Python Turtle Graphics

Python Turtle Graphics Circles - Archery Target

In this lesson nosotros are going to learn how to draw circles with Python Turtle Graphics. Nosotros will then alter the default circle method so that nosotros can centre our circles at specific (x, y) coordinates, so have some fun some with creating an archery target and adding some interactivity.

As you lot may already know, Python Turtle Graphics is fantastic way to larn about programming and also about Maths. In general at that place seems to exist piffling integration between these two subjects at school level, which is something I hope to run across change. Many of my posts on this blog are written to help farther this crusade. See Computer Maths Category for related posts.

Before we start, please note that there are many ways to attain the same goal with Python Turtle Graphics. While this can be a expert thing, it tin likewise lead to confusion. For this reason I have done things is a certain way which I consider gives the all-time foundation for making full use of the potential of this module. For example:

  • I create a screen object then I can control its colour and title etc.
  • I use functions which have an existing turtle every bit an argument, to help discourage the use of global variables and provide added flexibility, and then the same part can work for multiple turtles.

Don't worry too much about these details if they don't make total sense to you. The lawmaking is fairly self explanatory and there are comments to assistance.

Drawing Circles with Python

The default way to create circles in with Python Turtle Graphics is to simple use the circumvolve method, equally in the following example.

            import turtle  # Set upwardly screen screen = turtle.Screen() screen.championship("Circle") screen.setup(450, 450) screen.bgcolor("cyan")  # Create a turtle toby = turtle.Turtle() toby.speed(0) toby.width(5) toby.hideturtle() toby.color("red")  # Draw a circle starting at (ten, y) radius = 100 toby.circle(radius)  # Make information technology all work properly turtle.done()                      

Python turtle circle

This is fine for many purposes, merely it tin can be frustrating every bit information technology doesn't give you control of where the centre of the circle is. Detect how in the case above the circle is not centred at the location of the turtle (called toby), which came into being at the default location of (0, 0). Using the default method, the circumvolve is drawn from the staring betoken, so the starting point is on the circumference.

Drawing Circles Centred at (x, y)

The next programme demonstrates how to draw circles centred at specific (x, y) coordinates. Information technology does this by utilize of a function draw_circle() which takes several arguments as described in the code.

Using this function, it is relatively like shooting fish in a barrel to depict an archery target. See the program below.

            import turtle   def draw_circle(tur, ten, y, radius, color="black"):     """     Draws a circle with center at (x, y), radius radius and color colour.     Create your turtle elsewhere and laissez passer it in as tur.     """     tur.colour(color)     tur.pu()     tur.goto(x, y - radius)  # -radius because the default circle method starts drawing at the border.     tur.pd()     tur.begin_fill()     tur.circle(radius)     tur.end_fill()   # Ready up screen screen = turtle.Screen() screen.championship("Archery") screen.setup(450, 450) screen.bgcolor("cyan")  # Draw the target toby = turtle.Turtle() toby.speed(0) toby.width(v) toby.hideturtle()  draw_circle(toby, 0, 0, 160, "black")  # Describe a black circumvolve at coords (0, 0) with radius 160 pixels draw_circle(toby, 0, 0, 120, "bluish") draw_circle(toby, 0, 0, 80, "red") draw_circle(toby, 0, 0, 40, "yellow")  # Make information technology all work properly turtle.done()                      

This programme makes use of lots of features which you can use in your own programs. Yous can utilize equally much or as piffling as you like, simply experiment with the ideas. If you don't accept many ideas, try just making pocket-size changes, like irresolute the colours or sizes of the circles. Some of the colours bachelor in Python Turtle Graphics can exist found hither.

The Side by side Level

This section contains some more than avant-garde Python programming techniques, so if you are a relative beginner, you may desire to leave it for after on.

For example it includes

  • Event detection with Python Turtle Graphics
  • Event callback functions
  • Passing additional arguments to a callback using lambda
            import turtle  CROSS_SIZE = xx   def draw_circle(tur, 10, y, radius, color="blackness"):     """     Draws a circle with middle at (x, y), radius radius and color color.     Create your turtle elsewhere and laissez passer it in every bit tur.     """     tur.color(color)     tur.pu()     tur.begin_fill()     tur.goto(x, y - radius)  # -radius considering the default circumvolve method starts drawing at the border.     tur.pd()     tur.circumvolve(radius)     tur.end_fill()   def draw_plus(tur, x, y, length=CROSS_SIZE):     """     Draws a cross centered at (10, y) with existing turtle tur and length given by CROSS_SIZE.     """     tur.penup()     tur.goto(x, y - (length / 2))     tur.pendown()     tur.goto(x, y + (length / 2))     tur.penup()     tur.goto(x - (length / 2), y)     tur.pendown()     tur.goto(x + (length / 2), y)     print("Mouse click at", x, ",", y)  # for useful feedback about where you clicked.   screen = turtle.Screen() screen.championship("Archery") screen.setup(450, 450) screen.bgcolor("cyan") screen.heed()  # Depict cross when screen is clicked cross_turtle = turtle.Turtle(visible=Simulated) cross_turtle.color("green") cross_turtle.width(4) cross_turtle.speed(0) # The lambda here is a useful trick to enable additional arguments to be passed to the onclick callback. screen.onclick(lambda 10, y, tur=cross_turtle: draw_plus(tur, 10, y)) screen.onkey(lambda: cross_turtle.clear(), "space")  # Clear crosses on keypress.  # Describe the target toby = turtle.Turtle() toby.speed(0) toby.width(v) toby.hideturtle()  draw_circle(toby, 0, 0, 160, "blackness")  # Draw a black circle at coords (0, 0) with radius 160 pixels draw_circle(toby, 0, 0, 120, "blue") draw_circle(toby, 0, 0, fourscore, "crimson") draw_circle(toby, 0, 0, 40, "yellow")  # Brand it all piece of work properly. turtle.washed()                      

Python turtle archery

At that place are lots of ingredients hither that you tin utilise in your own projects. Equally before, become ahead and edit $.25 of the programme or apply bits in you ain project. The program is not currently a game as such, but I expect it could be made into one. Tin can yous retrieve of how? I'1000 thinking some kind of random positioning of the crosses or reflex-testing game. We haven't looked at timers and animation in this article, just for sure they are possible with Python Turtle Graphics. It may exist that an idea y'all have might become possible with a bit more noesis, then maybe brand a notation and come back to it later. If you accept an idea that you want assistance with, allow me know in the comments and I'll see if I tin help.


This lesson has shown yous how to depict circles using Python Turtle Graphics and then how to improve the basic functionality and add some interactive features. I promise you plant information technology fun and interesting.

Happy computing!

petersonsomuckledge.blogspot.com

Source: https://compucademy.net/drawing-circles-with-python-turtle-graphics/

0 Response to "Program to Draw Concentric Circles in Computer Graphics"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel