Twenty-odd years after enduring my Discrete Math course in college, I finally needed to use the modulus concept.
I was trying to get my Logo turtles to make stars by just selecting a number of prongs on a slider button. After a lot of experimenting with the number of degrees the turtles turned I found the formula for how much to turn at the end of each prong in the odd-numbered stars:
180 - 360 / (2 * number of prongs)
It didn't work for the even numbered stars, though. It would leave a half-finished star, as shown below:
But that meant the 12-prong formula was perfect for a 24-prong star. If the number of prongs is even, the formula is:
180 - 360 / number of prongs
So I just had to put in an if-statement: if the number of prongs selected is even, use the second formula, otherwise, use the first. Like this:
ifelse numberofprongs = even[fd 400 rt 180 - 360 / numberofprongs][fd 400 rt 180 - 360 / (2 * numberofprongs)]
But NetLogo doesn't have an "even" condition or value, so how could I get the turtle to do the right thing to even or odd numbered stars?
The answer (for once!) came from the modulus concept in discrete math. It's like dividing by a number and getting a remainder. Every even number is divisible by 2 with no remainder, so that was my way of getting the computer to recognize an even number. This code says, "If the number of prongs is divisible by 2 with a remainder of 0, do the first command. Otherwise, do the second command."
ifelse numberofprongs mod 2 = 0[fd 400 rt 180 - 360 / numberofprongs][fd 400 rt 180 - 360 / (2 * numberofprongs)]
It worked!
No comments:
Post a Comment