I’ve been wondering how to program a Sierpinski Triangle. I don’t need the actual program, I just need a small guideline. Like, for example: Do I use the chaos game method? Do I use the image function, or some other method for drawing graphics? That sort of stuff.
I hate to sound like a sob story, or some angry internet commenter, but I can’t think for myself. I need guides on how to do any programming problems. For example, I did fine in my Java class this year in high school, but when I had to figure out problems that weren’t already instructed, I did terribly on my own. That combined with an inability to work on a project for more than a day or two really hinders me when it comes to programming.
Can somebody please help me? Anything would be appreciated, even a quick “you should learn to do it yourself” style talking-to.
In general I’d say drawing a Sierpinski triangle is a recursive problem. In the past I’ve done this by having a function that draws an outer triangle, and then calls itself 3 times for the 3 inner triangles, but with different argument values. You’ll just have to have a stopping condition so the recursion won’t go on forever.
As far as general programming goes, you need to think the problem through. Think about what you want to accomplish, and then start breaking that down into smaller steps. Once you get it broken down, then you can start coding and testing each step. Eventually, as each step is completed, you will see your program taking shape. The main point is to think things through and how you would accomplish it step by step. No matter how big or small a program is, you build it a line of code at a time.
I’ve been looking at this myself. Here’s a link to a code in a few different languages so you can see the approach.
I’ve already done the Koch curve, which is in my small demo routine thread (and in contributed code in the Wiki).
Please post your code, and if it’s OK, we can add it to contributed code.
I did it!
( with help from http://online.redwoods.cc.ca.us/instruct/darnold/ifs/ )
Currently, the only thing you can change about it is the iterations. (How fast it appears.)
I’d post the code if I knew where a good place would be to put it. Should I post it to this topic, or upload it to a separate site?
What should the next step be: colors, or other shapes?
Here is the code. Feel free to use it or modify.
print("Sierpinksi Triangles Program")
-- iterations = 1000
iparameter("Iterations", 1, 1000, 500)
x = WIDTH/2
y = HEIGHT/2
myImage = image(WIDTH, HEIGHT)
background(0, 0, 0, 255)
sprite(myImage, WIDTH/2, HEIGHT/2)
for i = 1, Iterations do -- lowercase if your not using parameter
randomizer = math.random(3)
if randomizer == 1 then
x = x * .5
y = y * .5
elseif randomizer == 2 then
x = x * .5 + WIDTH/2
y = y * .5
x = x * .5
y = y * .5 + HEIGHT/2
Alright, I’ve decided to go the color route.
So far, I’ve gotten each section of the triangle to be a different color.
Blue on top, with Red and Green on the bottom.
However, I wanted them to blend as they got closer to one of the vertices.
(So if you were in between the Red and the Green triangles, you would get (127, 127, and whatever Blue value.))
Can someone point me in the right direction?