Hello,
I am trying to make a GeometryUtil class that has methods to draw point,line ,polygon etc. I am trying to make a method to draw circle.
There are many ways to draw a circle. I have found two ways,
The one way:
public static void drawBresenhamCircle(PolygonSpriteBatch batch, int centerX, int centerY, int radius, ColorRGBA color)
{
int x = 0, y = radius;
int d = 3 - 2 * radius;
while (y >= x)
{
drawCirclePoints(batch, centerX, centerY, x, y, color);
if (d <= 0)
{
d = d + 4 * x + 6;
}
else
{
y--;
d = d + 4 * (x - y) + 10;
}
x++;
//drawCirclePoints(batch,centerX,centerY,x,y,color);
}
}
private static void drawCirclePoints(PolygonSpriteBatch batch, int centerX, int centerY, int x, int y, ColorRGBA color)
{
drawPoint(batch, centerX + x, centerY + y, color);
drawPoint(batch, centerX - x, centerY + y, color);
drawPoint(batch, centerX + x, centerY - y, color);
drawPoint(batch, centerX - x, centerY - y, color);
drawPoint(batch, centerX + y, centerY + x, color);
drawPoint(batch, centerX - y, centerY + x, color);
drawPoint(batch, centerX + y, centerY - x, color);
drawPoint(batch, centerX - y, centerY - x, color);
}
The other way:
public static void drawCircle(PolygonSpriteBatch target, Vector2 center, float radius, int lineWidth, int segments,
int tintColorR, int tintColorG, int tintColorB, int tintColorA)
{
Vector2[] vertices = new Vector2[segments];
double increment = Math.PI * 2.0 / segments;
double theta = 0.0;
for (int i = 0; i < segments; i++)
{
vertices[i] = new Vector2((float) Math.cos(theta) * radius + center.x, (float) Math.sin(theta) * radius
+ center.y);
theta += increment;
}
drawPolygon(target, vertices, lineWidth, segments, tintColorR, tintColorG, tintColorB, tintColorA);
}
In the render loop:
polygonSpriteBatch.begin();
Bitmap.drawBresenhamCircle(polygonSpriteBatch,500,300,200,ColorRGBA.Blue);
Bitmap.drawCircle(polygonSpriteBatch,new Vector2(500,300),200,5,50,255,0,0,255);
polygonSpriteBatch.end();
I am trying to choose one of them. So I thought that I should go with the one that does not involve heavy calculations and is efficient and faster. It is said that the use of floating point numbers , trigonometric operations etc. slows down things a bit. What do you think would be the best method to use? When I compared the code by observing the time taken by the flow from start of the method to the end, it shows that the second one is faster. (I think I am doing something wrong here ).
Please help!
Thank you.