Triangle Function

Back to index

This sample program just produces a bunch of triangles, but each time the program is run the triangles wind up in different spots. Get this sample up and running and be sure to test it out by running it several times.
#!/usr/bin/python from tkinter import * import random tk = Tk() canvas = Canvas(tk,width=300, height=300, background="black") canvas.pack() def makeTri(x,y,s): canvas.create_line(x,y,x+s/2,y+s,fill="yellow", width=3) canvas.create_line(x,y,x-s/2,y+s,fill="magenta", width=3) canvas.create_line(x+s/2,y+s,x-s/2,y+s,fill="cyan", width=3) for x in range(0,10): xpt = random.randint(20,280) ypt = random.randint(20,280) size = random.randint(10,30) makeTri(xpt,ypt,size) canvas.create_text(140,15,text="Random Triangles", fill="red")
The use of the randint function and the following user defined function are the most interesting features of this sample program.
def makeTri(x,y,s): canvas.create_line(x,y,x+s/2,y+s,fill="yellow", width=3); canvas.create_line(x,y,x-s/2,y+s,fill="magenta", width=3); canvas.create_line(x+s/2,y+s,x-s/2,y+s,fill="cyan", width=3);
The height and the base of these triangles is the same. Therefore, what type of triangle are they?
ASSIGNMENT:

Your assignment will be to create a 12x12 array of randomly colored triangles of uniform size. So, the size and arrangement of the triangles will always be the same, but the color distribution will change each time the program runs. Use at least six different colors.