opengl dibujar circulo
Creamos un nuevo proyecto de consola en c# y agregamos las refrencias de Tao.FreeGlut y Tao.OpenGl, en el main escribir el siguiente codigo:
Console.WriteLine("introduzca x");
xc = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("introduzca y");
yc = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("introduzca r");
r = Convert.ToDouble(Console.ReadLine());
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_SINGLE | Glut.GLUT_RGB);
Glut.glutInitWindowSize(500, 500);
Glut.glutCreateWindow("**************Circulo**************");
Glut.glutDisplayFunc(circulo);
Glut.glutMainLoop();
Creamos el metodo circulo:
public static void circulo()
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glColor3f(0.6F, 0.6F, 0.6F);
Gl.glLoadIdentity();
Gl.glPointSize(5f);
Gl.glBegin(Gl.GL_POINTS);
float z = -1, w = 1, c = 0;
for (int i = 0; i < 200; i++)
{
Gl.glColor3f(w, c, z);
Gl.glVertex2d(z, 0);
Gl.glVertex2d(0, w);
z += .01f;
w -= .01f;
c += .1f;
}
//mandar los puntos
circuloAlgoritmo(xc, yc,r);
Gl.glEnd();
}
Creamos el metodo circuloAlgoritmo:
public static void circuloAlgoritmo(double xc, double yc, double r)
{
double x = 0;
double y = r;
double p = 0.1f - r;
pintar(xc, yc, x, y);
while (x < y)
{
x+=.1f;
if (p < 0)
p += .2f * x + .1f;
else
{
y-=.1f;
p += .2f * (x - y) + .1f;
}
pintar(xc, yc, x, y);
}
}
Creamos el metodo pintar:
public static void pintar(double xc, double yc, double x, double y)
{
Gl.glColor3f(1f, .6f, 1f);
Gl.glVertex2d(xc + x, yc + y);
Gl.glVertex2d(xc - x, yc + y);
Gl.glVertex2d(xc + x, yc - y);
Gl.glVertex2d(xc - x, yc - y);
Gl.glColor3d(0f, .6f, 1f);
Gl.glVertex2d(xc + y, yc + x);
Gl.glVertex2d(xc - y, yc + x);
Gl.glVertex2d(xc + y, yc - x);
Gl.glVertex2d(xc - y, yc - x);
}
si corremos el programa nos mostrará algo asi:
el ejemplo de pueden descargar desde aqui
nota:vean el post del algoritmo DDA para la biblioteca
contacto:
