How To Draw Equilateral Triangle Using Pygame
I'm a large fan of fractals, and dedicated a whole chapter of Hacking Math Class with Python to them. The Sierpinski Triangle (or "Sieve") is a famous shape that pops upwardly in the well-nigh unexpected ways.
In my volume I showed the easy mode to depict one by making a turtle practise it, but there's a little more than complicated way that involves another famous triangle. It was known to the Chinese and Indians while nosotros Europeans were notwithstanding hiding from the Vikings, just nosotros similar to telephone call it Pascal's Triangle subsequently a more recent French mathematician.
The numbers can be generated by adding the numbers in the row in a higher place, just using a figurer it's quicker to use the combination formula.
Here's the (Python ii) code I copied and pasted from dheerosaur on Stack Overflow:
import operator equally op
...
def ncr(north, r):
r = min(r, n-r)
if r == 0: return 1
numer = reduce(op.mul, xrange(northward, n-r, -1))
denom = reduce(op.mul, xrange(1, r+i))
return numer//denom
(To convert it to Python 3, only change "xrange" to "range" and add "from itertools import reduce" to the imports.)
The 1st row and column are really the 0th, so the "twenty" in row 7 is actually row 6, column 3:
>>> ncr(6,3)
20
How could you peradventure go The Sierpinski Sieve from Pascal'south Triangle? Allow'southward apply Pygame.
I use a template I establish at Professor Craven's excellent Pygame tutorial. This sets upwardly the screen and the colors and the brandish loop. All I have to do is draw shapes; it even updates the brandish and quits for me. I have to create a function to draw a triangle where I want it and what size I desire it. The number (calculated with ncr in a higher place) volition determine whether it's filled with color or not:
def triangle(x,y,size,filled):
'''draw an equilateral triangle'''
#if the number isn't a multiple of 2, color it in.
if filled % 2 != 0:
pygame.draw.polygon(screen,VIOLET,[[x,y],
[x-size/2.,y+ane.732*size/2.],
[ten+size/2.,y+1.732*size/2.]],
0)
else: thick = 0 #leave multiples of 2 empty
The "gasket" function volition draw the triangles at the correct spots:
def gasket(rows):
size = 400./rows #resize depending on the number of rows
for i in range(rows):
for j in range(i+i):
y = ane.732*i*size/2. #tiptop of equilateral triangle
x = -i*size/2. + j * size + WIDTH/2. #width of triangle
triangle(x,y,size,ncr(i,j)) #draw the triangle
Now running "gasket(50)" volition get you 50 rows of Pascal's triangle, with the multiples of 2 left empty:
Look familiar? You can change one digit in the "triangle" part to leave out the multiples of 3 instead:
Or the multiples of 8. The design of empty triangles changes every time. Try other numbers!
This blog entry has been brought to you by O'Reilly books.:-)
Source: http://hackingmathclass.blogspot.com/2016/03/drawing-triangles-hard-way.html
Posted by: blakeronfiess.blogspot.com

0 Response to "How To Draw Equilateral Triangle Using Pygame"
Post a Comment