Question

I need to create a circle of regularly spaced doors around a reference point in space. Each time the program is run, a different number of items will be in the circle, and thus it automatically rescales to accommodate the increased number of items. I know this is more of a mathematic question rather than a programming question, but I am stumped! The following code is as close as I got:

block->setPosition(core::vector3df(sin(segdeg*itemnumber)*radius+referencepoint.X,
                                   0,
                                   cos(segdeg*itemnumber)*radius+referencepoint.Z));

block is the object, and this code is run for each item. segdeg is 360/the number of items. that is to say, the SEGment DEGrees. Radius is how far away from the central point the items need to be. Itemnumber is the index of the item in question - Which number item it is.

For some reason, this code makes each door pretty close, but still about 10-15 degrees off. (The first door is always spot on though) The items are all the correct distance away from the centre point though. I'm sure that this is a really obvious answered question, but I haven't been able to solve it for hours and I've googled my arse off.

Can anyone fix my algorithm?

Was it helpful?

Solution

sin and cos take their arguments in radians, not degrees. You want segrad = 2 * pi / the_number_of_items (SEGment RADians) instead of segdeg = 360.0 / the_number_of_items (SEGment DEGrees).

Your implementation might provide a value for pi, but there's none in the standard so you might have to use boost::math::constants::pi<double>() or put your own in.

Your first item is correct because 0 is 0 in any measure.

OTHER TIPS

int doors = 5;
double rotation = 2 * M_PI / doors;
for(int door = 0; door < doors; ++door)
{
    double door_rotation = door * rotation;
    double door_x = reference_x + cos(door_rotation);
    double door_y = reference_y + sin(door_rotation);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top