Python 2.6 Graphics Cookbook
上QQ阅读APP看书,第一时间看更新

Cloning and resizing stars

A technique of simultaneous re-positioning and resizing a set of stars is shown.

How to do it...

The instructions used in recipe 1 should be used.

Just use the name clone_stars.py when you write, save, and execute this program.

# clone_stars.py
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
from Tkinter import *
root = Tk()
root.title('Re-sized and re-positioned polygon stars')

cw = 200                                      # canvas width
ch = 100                                      # canvas height
canvas_1 = Canvas(root, width=cw, height=ch, background="white")
canvas_1.grid(row=0, column=1)                              

# blue star, anchored to an anchor point.
x_anchor = 15
y_anchor = 150
size_scaling = 1

canvas_1.create_polygon(x_anchor,        y_anchor,\
                        x_anchor + 20 * size_scaling,   y_anchor - \ 40* size_scaling,\
                        x_anchor + 30 * size_scaling,   y_anchor + \ 10* size_scaling,\
                        x_anchor,        y_anchor - 30* size_scaling,\
                        x_anchor + 40 * size_scaling,   y_anchor - \ 20* size_scaling,\
                           fill="green")                 
size_scaling = 2
x_anchor = 80
y_anchor = 120
canvas_1.create_polygon(x_anchor,        y_anchor,\
                        x_anchor + 20 * size_scaling,   y_anchor - \ 40* size_scaling,\
                        x_anchor + 30 * size_scaling,   y_anchor + \ 10* size_scaling,\
                        x_anchor,        y_anchor - 30* size_scaling,\
                        x_anchor + 40 * size_scaling,   y_anchor - \ 20* size_scaling,\
                           fill="darkgreen")                 
size_scaling = 3
x_anchor = 160
y_anchor = 110
canvas_1.create_polygon(x_anchor,        y_anchor,\
                        x_anchor + 20 * size_scaling,   y_anchor - \ 40* size_scaling,\
                        x_anchor + 30 * size_scaling,   y_anchor + \ 10* size_scaling,\
                        x_anchor,        y_anchor - 30* size_scaling,\
                        x_anchor + 40 * size_scaling,   y_anchor - \ 20* size_scaling,\
                           fill="lightgreen")   
size_scaling = 3
x_anchor = 160
y_anchor = 110
canvas_1.create_polygon(x_anchor,        y_anchor,\
                        x_anchor + 20 * size_scaling,   y_anchor - \ 40* size_scaling,\
                        x_anchor + 30 * size_scaling,   y_anchor + \ 10* size_scaling,\
                   x_anchor,        y_anchor - 30* size_scaling,\
                        x_anchor + 40 * size_scaling,   y_anchor - \ 20* size_scaling,\
                           fill="forestgreen")
root.mainloop()
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

How it works...

The results are given in the next screenshot, showing a string of stars with changing size.

How it works...

In addition to the variable and conveniently re-assigned anchor point of the polygon star we have now introduced an amplification factor that can change the size of any particular star without distorting it.

There's more...

The last three examples have illustrated some important and fundamental ideas used to draw pre-defined shapes in any size and in any position. It was important to separate these effects in different examples at this stage so that the separate actions are easy to understand. Later on, where the effects are used in combination, it becomes difficult to wrap your head around what is happening, particularly if extra transformations like rotation are involved. If we animate code that generates images it can be much easier to understand geometric relationships. By animate, I mean the display of successive images separated by short-time intervals similar to the way images in movies are manipulated. Such time-regulated animation, surprisingly, offers methods of examining the behavior of image-generating code in a way that is much more intuitive and clear to the human brain. This idea is developed in the later chapters.