Selamat Datang Di Blogg Saya

Jumat, 20 April 2012

Membuat Objek dan Garis dengan DDA dan Bresenham

/* pratikum 04
*membuat objek dan garis dengan DDA dan Bresenham
*/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <glut.h>
#include <math.h>

void display(void)
{
//set display-window background color to white
glClearColor(1.0, 1.0, 1.0, 0.0);
//set projection parameters
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 300.0, 0.0, 300.0);
}
void setPixel(GLint xCoordinate, GLint yCoordinate)
{
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
glFlush();
}


//procedure bresenham line-drawing untuk |m| < 1.0
void lineBres(GLint x0, GLint y0, GLint xEnd, GLint yEnd)
{
GLint dx = (float) fabs ((float) xEnd - x0);
GLint dy = (float) fabs ((float) yEnd - y0);
GLint p = 2 * dy - dx;
GLint twoDy = 2 * (dy-dx);
GLint x,y;
//determine which endpoint to use as start position
if (x0 > xEnd) {
x = xEnd;
y = yEnd;
xEnd = x;
}else{
x = x0;
y = y0;
}
setPixel(x,y);
while(x<xEnd){
x++;
if (p<0)
p += twoDy;
else{
y++;
p += twoDyMinusDx;
}
setPixel(x,y);
}
}
void drawMyLine(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(4.0);
GLint x0 = 100;
GLint y0 = 150;
GLint xEnd = 200;
GLint yEnd = 200;
lineBres(x0, y0, xEnd, yEnd);
}
int main(int argc, char** argv);
{
//initialize GLUT
glutInit(&argc,argv);
//initialize display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//set display-window width & height
glutInitWindowSize(400,400);
//set display-window upper-letf position
glutInitWindowPosition(0.0);
//create display-window with a title
glutCreateWindow("Digital Differential Analyzer Algorithm");
//initilze OpenGL
display();
//call graphics to be displayed on the window
glutDisplayFunc(drawMyLine);
//display everything and wait
glutMainLoop();
return 0;

}

1 komentar:

  1. Gan, gimana caranya membuat program DDA dan Bresenham di visual basic.

    Tolong konfirmasi secepatnya ya gan. :D

    BalasHapus