-->

Polygon Circles on Python

Here’s a fun, and mathematical, look at making a circle from straight lines. Using the Math
module, in particular sin, cos and Pi, this code will draw a series of straight lines using the Turtle module. The end result is quite remarkable and has plenty of scope for further exploration.

POLYGONCIRCLES.PY

There’s lots of Mathematics used here along with some intricate coordinate manipulation with the Turtle module.

Enter the code and execute it to see how it turns out.

  1. from turtle import*
  2. from math import sin, cos, pi
  3. r=200
  4. inc=2*pi/100
  5. t=0;n=1.5
  6. for i in range(100):
  7.     x1=r*sin(t); y1=r*cos(t)
  8.     x2=r*sin(t+n);y2=r*cos(t+n)
  9.     penup(); goto(x1,y1)
  10.     pendown();goto(x2,y2)
  11.     t+=inc

Graphical Enhancements

There are several ways in which you can improve this code to make it more interesting. You can insert colours, perhaps a different colour for every line. You can display a message inside the circle and have the Turtle draw around it. Let your imagination run wild on this one.

Turtle’s graphics can take a while to map out and draw, depending on how big and how intricate an image it is you’re designing. Whilst the effect can be quite stunning, it is limited by the amount of time it takes to display an image. Therefore it’s worth seeing if the function turtle.speed() will quicken things up.

Turtle.speed() comes in various values:

  1. slowest
  2. slow
  3. normal
  4. fast
  5. fastest

You can experiment with the various speeds by adding the function in the for loop, just before the penup line.

For example:

  1. for i in range(100):
  2.     x1=r*sin(t); y1=r*cos(t)
  3.     x2=r*sin(t+n);y2=r*cos(t+n)
  4.     speed(‘fastest’)
  5.     penup(); goto(x1,y1)
  6.     pendown();goto(x2,y2)
  7.     t+=inc

Polygon Circles on Python
Polygon Circles on Python


0 Response to "Polygon Circles on Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel