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.
Enter the code and execute it to see how it turns out.
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:
You can experiment with the various speeds by adding the function in the for loop, just before the penup line.
For example:
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.
from turtle import* from math import sin, cos, pi r=200 inc=2*pi/100 t=0;n=1.5 for i in range(100): x1=r*sin(t); y1=r*cos(t) x2=r*sin(t+n);y2=r*cos(t+n) penup(); goto(x1,y1) pendown();goto(x2,y2) 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:
slowest slow normal fast fastest
You can experiment with the various speeds by adding the function in the for loop, just before the penup line.
For example:
for i in range(100): x1=r*sin(t); y1=r*cos(t) x2=r*sin(t+n);y2=r*cos(t+n) speed(‘fastest’) penup(); goto(x1,y1) pendown();goto(x2,y2) t+=inc
Polygon Circles on Python |
0 Response to "Polygon Circles on Python"
Post a Comment