- Home /
Find center point of regular polygon
Hi all, I've written a script to generate an N sided regular polygon shape consisting of cubes.However I want to position the shape so that the camera is at its center point. I was thinking that if I found a point that is the radius of the shape distant from the camera position along an angle of 360- interior angle/2 as the starting point of the shape generation that would work. So for a 4 sided shape, the interior angle is 90 degrees between sides. So half that is 45degrees. So if i started generation at a point the radius distant from the camera position along the direction of -45 degrees that would ensure the camera is central. I know how to find the radius and angle. But I cant seem to get the position right:
Quaternion theAngle = Quaternion.Euler(0, -angle/2, 0);
Vector3 tdirection = theAngle * Vector3.forward;
initPosition += tdirection * tunnelRadius;
initPosition is the position of the camera. Any ideas? Thanks.
Uhm, i'm a bit confused. Why do you create a regular polygon this way? Usually you would define the center and go around n times with (360/n)°
Also how do you form a polygon out of cubes? You either have a gap between the cubes, or the cubes have to intersect each other.
Answer by Bunny83 · Mar 29, 2015 at 04:48 PM
Regardless of my questions in the comment above, one way how you can find the center of a regular polygon is by simply take the arithmetic mean of all corners. So simply add up all corner positions and divide by the count.
edit
Something like that:
Transform[] cubes;
Vector3 FindCenter()
{
Vector3 pos = Vector3.zero;
foreach(Transform t in cubes)
{
pos += t.position;
}
return pos / cubes.Length;
}
Another way is, since you should know the iteration angle and the iteration step size (probably 1.0?!?) You can simply figure out the radius by using:
float r = s / $$anonymous$$athf.Sin($$anonymous$$athf.PI / n);
To get the direction from one of the corners you can use half of the interior angle (like you have mentioned) and do one of your iteration steps. The next direction would be your desired direction. Don't forget to normalize the direction before you multiply with the radius.
If you want any further help you should include your actual generation code in your question since my answers are purely based on speculations on how you actually generate your polygon. A screenshot would also be nice ^^
Hmmm. What I need is just the starting point to draw the the shape. I have the shape drawing dandy so no issues there. I have the formula to get the radius no problem, I have the angle and the center point of the shape. Its literally just getting the starting point for the construction is what I need. So for example using the angle and radius on a square shape, it should find the top left corner. I suppose what I need is to be able to set the direction and move the length of the radius out along it.
Im not sure but it seems like the measurements of the camera position etc seem like they might not be fully consistent with the placed cubes. Here is a webplayer of the tunnel that is generated https://t.co/kT7HwTZUDt