You are on page 1of 2

DDA.

htm

/* * Universidade Federal da Paraba * Aluno: Carlos Andr Soares Correia * Matrcula: 11011827 * Disciplina: Introduo Computao Grfica * Professora: Liliane */ #include #include #include #include extern extern extern extern <stdio.h> <GL/glut.h> <math.h> <time.h> display(); inicializa(); retaDDA(int xa, int ya, int xb, int yb); plotaPonto(int x, int y);

void void void void

int main (int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(300, 300); glutCreateWindow("Algoritmo DDA"); glutDisplayFunc(display); inicializa(); glutMainLoop(); } void display() { clock_t inicio, fim; double tempo; int i = 0; glClear(GL_COLOR_BUFFER_BIT); glColor3d(1, 0, 1); glBegin(GL_POINTS); inicio = clock(); for (i = 0; i < 10000; ++i) retaDDA(0, 0, 100, 100); fim = clock(); tempo = (double)(fim - inicio) / CLOCKS_PER_SEC; printf("Algoritmo DDA -> Tempo: %f\n\n", tempo); glEnd(); glFlush(); } void inicializa() { glClearColor(1.0, 1.0, 1.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-200.0, 200.0, -200.0, 200.0); } void plotaPonto(int x, int y) { glVertex2d(x, y); } void retaDDA(int xa, int ya, int xb, int yb) {

1 de 2

DDA.htm

int dx, dy, passos, k; float somax, somay, x, y; dx = xb - xa; dy = yb - ya; if (fabs(dx) > fabs(dy)) passos = fabs(dx); else passos = fabs(dy); somax = (float)dx / passos; somay = (float)dy / passos; x = xa; y = ya; plotaPonto((int)x, (int)y); int a, b; for (k = 1; k <= passos; k++) { x += somax; y += somay; a = x; b = y; plotaPonto(a, b); } plotaPonto((int)x, (int)y); }

2 de 2

You might also like