- Home /
Possible resolutions of a circle
Greetings,
I've created a script to make a Circle as a mesh. The script, at the moment, has a slider that controls the 'resolution' of the circle between 3 and 72. The resolution of a circle is equal to the number of it's edges.
Now this is all working really fine. For the exception that several resolutions don't make a circle.
Like this one, with a resolution of '46'
I think it's down to the way I find the positions for the vertices of the circle. I just rotate a Vector3 clockwise with Quaternion.AngleAxis. Where the Angle is equal to 360 / Resolution.
Again. some resolutions do work. Like 3, 4, 6, 8, 12, 18, 36 or 72.
Can you help me find a workarround for this problem? I'm willing to provide more info if needed.
Edit: Constructor of the Circle
public Circle(float radius, float border, int offset, int resolution, Direction direction) {
Radius = radius;
Border = border;
Offset = offset;
Resolution = resolution;
Direction = direction;
Vector3 oPoint;
Vector3 rPoint;
Vector3 dir;
switch (Direction) {
default:
oPoint = Vector3.zero;
rPoint = Vector3.zero;
dir = Vector3.forward;
break;
case Direction.Back:
oPoint = new Vector3(Radius + 0.1f, 0.0f, 0.0f);
rPoint = new Vector3(Radius, 0.0f, 0.0f);
dir = Vector3.forward;
break;
case Direction.Forward:
oPoint = new Vector3(-(Radius + 0.1f), 0.0f, 0.0f);
rPoint = new Vector3(-(Radius), 0.0f, 0.0f);
dir = Vector3.back;
break;
case Direction.Left:
oPoint = new Vector3(0.0f, 0.0f, Radius + 0.1f);
rPoint = new Vector3(0.0f, 0.0f, Radius);
dir = Vector3.right;
break;
case Direction.Right:
oPoint = new Vector3(0.0f, 0.0f, -(Radius + 0.1f));
rPoint = new Vector3(0.0f, 0.0f, -(Radius));
dir = Vector3.left;
break;
case Direction.Up:
oPoint = new Vector3(Radius + 0.1f, 0.0f, 0.0f);
rPoint = new Vector3(Radius, 0.0f, 0.0f);
dir = Vector3.down;
break;
case Direction.Down:
oPoint = new Vector3(-(Radius + 0.1f), 0.0f, 0.0f);
rPoint = new Vector3(-(Radius), 0.0f, 0.0f);
dir = Vector3.up;
break;
}
OffsetVector = Quaternion.AngleAxis(Offset, dir) * oPoint;
Vertices = new Vector3[((Resolution + 1) * 2) + 1];
int cVerts = Resolution + 1;
float step = 360 / Resolution;
Vertices[0] = Vector3.zero;
float lerp = Border / Radius;
for (int i = 1; i < Vertices.Length; i++) {
Quaternion rot = Quaternion.AngleAxis(Offset - (step * (i - 1)), dir);
if (i < (cVerts + 1)) Vertices[i] = rot * rPoint;
else Vertices[i] = Vector3.Lerp(Vertices[i - cVerts], Vertices[0], lerp);
}
}
Answer by Bonfire-Boy · Jun 19, 2016 at 03:03 PM
The fact that it works ok for resolutions that divide exactly into 360 is hinting that the issue is a floating point to integer conversion.
I think the specific issue in your case is your calculation of step, where you're using ints to compute a float.
float step = 360 / Resolution;
360/46 = 7.826087... but When Resolution = 46, the above code does an integer division and then casts it to a float, resulting in 7 (the true answer, but truncated). This is throwing all your angles out. When Resolution divides exactly into 360, it's not a problem because the truncation makes no difference.
Just change it to
float step = 360f / Resolution;
and a floating-point division will be performed giving you the correct angle.
And then try to get into the habit of always putting the f at the end of floating point literals.
Answer by Fire_Cube · Jun 19, 2016 at 02:20 PM
Maybe if you provide the script, because the problem is that the angles are too small, since there are 46 triangles making the circle. If you can't provide the script, try looking around that.
I'm not sure, but I think for the resolution, you use the int / operator, ins$$anonymous$$d of the float one, try to do 300f / Resoltuion.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083238im_/https://answers.unity.com/themes/thub/images/avi.jpg)