Click to See Complete Forum and Search --> : Hhhheeeellllpppppp!!!!!!!


m316foley
12-11-2002, 07:49 PM
Ok, in my computer math class my teacher asked me personally to see if I could create a program that would run a for loop to create a polygon. The user is to input the amount of sides and you should draw it. We start off looking to the stright up, then we start drawing the shape, the only problem are shapes that have interior angles that aren't factors of 360. I don't want the actual code, but is there maybe a formula that would do this?


for(int i=0;i<numOfSides;i++)
{
bot.move(100);
bot.turnRight(360/num);
}


My main problem is that I have to get the drawing tool to end up exactly where it started in the same direction (straight up). This is what I think I have:

Initial Turn:

int initialTurn=Math.abs(90-(360/numOfSides));
bot.turnRight(initialTurn);


Final Turn:

bot.turnRight(180);
bot.turnRight(180-initialTurn);


This all works as long as the interior angles are factors of 360.

Any solutions?
Thanks in advance!

Grizzly
12-11-2002, 08:21 PM
Well, my first instinct is the modulus operator. For integers that aren't factors of 360, save the remainder and apply it to future angles.

m316foley
12-11-2002, 08:23 PM
But how would I apply it to other angles and still wind up in the same place? I guess I could just completely hack off the remainder and round it closest to one of the factors. But would that still leave me in the same place?

Grizzly
12-11-2002, 09:16 PM
I'm just kind of thinking off the cuff here, so bear with me...

But why not just keep some variable as a "remainder" counter. Like:

remBucket=0;

...//code code code

remBucket+=aNewRemainderFound;


If you understand what I mean, you should be incrementing your remainder counter every time there's a remainder detected. Then, when you're drawing your final side to the polygon, divide remBucket by 10, and add 0.5 to it. Then type-cast that badboy into an integer, and ad the result to the angle on the final polygon side. I would think that would work just fine.

Again, I'm just thinking off the top of my head, so if that was crappy advice I apologize :)

Conrad Song
12-11-2002, 10:07 PM
To get an equilateral polygon from only integer commands is not possible. Check to see if your turtle (bot) class accepts floating-point. If so, this is trivial.

If not, then you can 'draw' on inspiration from a snail to get a concave polygon. If you need the polygon to be convex, then it is still possible, but a bit trickier.

If you study fractals, you may find more information. This problem is more creativity than math.

m316foley
12-11-2002, 10:14 PM
Wow, thanks for the advice guys. Conrad nailed it with the turtle class :) I actually can't wait till I get to school tomorrow to try this out. Thanks!