Skip to content

Grafika Komputer – objek garis dengan algoritma DDA dan bresenham

April 16, 2011

kali ini coding praktikum dari mata kuliah grafika komputer yang ke 4 membuat obejak garis dengan algoritma DDA dan bresenham …..jikalau anda ingin mempelajari lebih jauh dan mencoba coding ini anda harus mempunyai master visual studio….kayaknya kalau anda belum punya mungkin di mbaaaaaah GOOGLE banyak deh heheheheh…..

langsung saja di liat yah dan di coba hehehe…..

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

void display(void)
{
glClearColor(1.0,1.0,1.0,0.0);
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();
}
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;
GLint twoDyMinusDx = 2 *(dy-dx);
GLint x,y;
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)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(0, 0);
glutCreateWindow(“Digital Differential Analyzer Algorithm”);
display();
glutDisplayFunc(drawMyLine);
glutMainLoop();
return 0;
}

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: