CIRCULO
INSTITUTO TECNOLÓGICO DE CIUDAD JUÁREZ
INGENIERÍA EN SISTEMAS COMPUTACIONALES
GRAFICACIÓN TAREA 3 UNIDAD II
CÍRCULO CON ALGORITMO DE PUNTO MEDIO Y ALGORÍTMICA
LEYVA MONDRAGON FRANCISCO
ROSALES MORALES NOÉ RAMÓN
JUEVES 17 DE SEPTIEMBRE DE 2015
1
CIRCULO
INTRODUCCION Por medio de este breve ensayo se conocerá acerca de las librerías que usa al programar líneas con openGL en c++. Así como ejemplos de cómo crear un círculo con la ayuda del algoritmo de punto medio y el método con los trigonométricos seno y coseno.
2
CIRCULO
INDICE
PROGRAMACIÓN DE CÍRCULOS
INTRODUCCIÓN…………………..…………………...………….……………....Página 2 PROGRAMA BASE………………………………………..................................Página 4 CIRCULO CON PUNTO MEDIO.....................................................................Página 5 CIRCO CON ALGORITMICA..........................................................................Página 8 CONCLUSIÓN…….………………..……………………………………….…..….Página 10 BIBLIOGRAFÍA…….……………………………………………………….…..….Página 11
3
CIRCULO
PROGRAMA BASE El programa de OpenGL está organizado en tres procedimientos. El primero es el colocar todas las inicializaciones y los parámetros de configuración relacionados en el procedimiento init. El segundo será referenciado por la función de GLUT glutDisplayfunc. Y el tercer procedimiento es el procedimiento main que contiene las funciones GLUT que configuran la ventana de visualización y muestran el segmento en pantalla. Enseguida se muestra el código fuente de este ejemplo: #include #include #include #include #include
"stdafx.h" "stdafx.h"
void init (void) { glClearColor ( 1.0, 1.0, 1.0, 0.0); glMatrixMode (GL_PROJECTION); gluOrtho2D ( 0.0, 200.0, 0.0, 150.0); } void main ( int argc, char *argv ) { glutInit (&argc,&argv) ; glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (50 , 100 ); glutInitWindowSize (400 , 300 ); glutCreateWindow( "Programa de ejemplo de OpenGL" ) ; init ( ); glutDisplayFunc () ; glutMainLoop ( ) ; }
void setPixel (int x,int y) { glBegin(GL_POINTS); glVertex2i (x, y); // Vertice1 glEnd(); }
Con este código de base podemos programar lo que queramos dibujar en nuestra pantalla. En esta ocasión dibujaremos círculos pero formados por puntos utilizando las dos métodos. Enseguida se muestra el código fuente de todo el programa, donde se utilizan los métodos:
4
CIRCULO
CIRCULO CON ALGORITMO DE PUNTO MEDIO Y aquí se muestra el código de todo el algoritmo: #include #include #include #include
"stdafx.h"
class screenPt { private: GLint x, y; public: screenPt() { x = y = 0; } void setCoords(GLint xCoordValue, GLint yCoordValue){ x = xCoordValue; y = yCoordValue; } GLint getx() const{ return x; } GLint gety() const{ return y; } void incrementx(){ x++; } void decrementy(){ y--; } }; void void void void void
init(void); DibujaLinea(void); setPixel(GLint xCoord, GLint yCoord); circleMidpoint(GLint xc, GLint yc, GLint radius); circlePlotPoints(GLint xc, GLint yc, screenPt circPt);
void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(50, 100); // POSICION INICIAL glutInitWindowSize(400, 400); // TAMAÑO DE VENTANA glutCreateWindow("CIRCULO"); init(); glutDisplayFunc(DibujaLinea); glutMainLoop(); }
5
CIRCULO
void init(void) { glClearColor(1.0, 1.0, 1.0, 0.0); //Estamos asignando un color a la ventana, se basa en RGB + 1 color alfa. glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0, 400.0, 0.0, 400.0); } void DibujaLinea(void) { glClear(GL_COLOR_BUFFER_BIT); // Visualizamos el color de la ventana glColor3f(0.0, 0.0, 1.0); circleMidpoint(200, 200, 50); glFlush(); }
void setPixel(GLint xCoord, GLint yCoord) { glBegin(GL_POINTS); glVertex2i(xCoord, yCoord); glEnd(); } void circleMidpoint(GLint xc, GLint yc, GLint radius) { screenPt circPt; GLint p = 1 - radius; circPt.setCoords(0, radius); void circlePlotPoints(GLint, GLint, screenPt); circlePlotPoints(xc, yc, circPt); while (circPt.getx() < circPt.gety()) { circPt.incrementx(); if (p < 0) p += 2 * circPt.getx() + 1; else { circPt.decrementy(); p += 2 * (circPt.getx() - circPt.gety()) + 1; } circlePlotPoints(xc, yc, circPt); } } void circlePlotPoints(GLint xc, GLint yc, screenPt circPt) { setPixel(xc + circPt.getx(), yc + circPt.gety()); setPixel(xc - circPt.getx(), yc + circPt.gety()); setPixel(xc + circPt.getx(), yc - circPt.gety()); setPixel(xc - circPt.getx(), yc - circPt.gety());
6
CIRCULO
setPixel(xc setPixel(xc setPixel(xc setPixel(xc
+ + -
circPt.gety(), circPt.gety(), circPt.gety(), circPt.gety(),
yc yc yc yc
+ + -
circPt.getx()); circPt.getx()); circPt.getx()); circPt.getx());
}
Aquí se muestra lo que se imprime en pantalla:
7
CIRCULO
ALGORITMO O MÉTODO CON TRIGONOMÉTRICAS Y aquí se muestra el método en el cual se puede observar el código: #include #include #include #include
"stdafx.h"
void init(void); void DibujaLinea(void); void circulotrig(float xc, float yc, float rad); float xc, yc, rad, calx, caly; void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(50, 100); // POSICION INICIAL glutInitWindowSize(400, 300); // TAMAÑO DE VENTANA glutCreateWindow("CIRCULO"); init(); glutDisplayFunc(DibujaLinea); glutMainLoop(); } void init(void) { glClearColor(1.0, 1.0, 1.0, 0.0); //Estamos asignando un color a la ventana, se basa en RGB + 1 color alfa. glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0, 400.0, 0.0, 300.0); } void DibujaLinea(void) { glClear(GL_COLOR_BUFFER_BIT); // Visualizamos el color de la ventana circulotrig(200, 150, 50); glFlush(); } void circulotrig(float xc, float yc, float rad) { glColor3f(1.0, 0.0, 0.0); glBegin(GL_POINTS); for (float i = 0; i < 360; i++) { calx = rad*cos(i) + xc; caly = rad*sin(i) + yc; glVertex2f(calx, caly); }
8
CIRCULO
glEnd(); glFlush(); }
Aquí se muestra lo que se imprime en pantalla:
9
CIRCULO
CONCLUSIÓN En conclusión se puede decidir por el método de las trigonométricas ya que se utiliza menos código, es más sencillo y es menos propenso a errores.
10
CIRCULO
Bibliografía HEARN, D., & BAKER, M. P. (2006).
GRAFICOS POR COMPUTADORA CON OPENGL. MADRID:
PEARSON.
11