Kamis, 11 April 2013

Garis Algoritma Bresenham





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

void display(void){
    glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,1000.0,0.0,1000.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,1.0);
    glPointSize(4.0);
    GLint x0 = 100;
    GLint y0 = 100;
    GLint xEnd = 200;
    GLint yEnd = 100;
    lineBres(x0,y0,xEnd,yEnd);
 
    glColor3f(0.98,0.625,0.12);
    glPointSize(4.0);
    GLint x2 = 100;
    GLint y2 = 200;
    GLint xEnd2 = 200;
    GLint yEnd2 = 200;
    lineBres(x2,y2,xEnd2,yEnd2);
 
    glColor3f(0.0,1.0,0.0);
    glPointSize(4.0);
    GLint x3 = 100;
    GLint y3 = 300;
    GLint xEnd3 = 200;
    GLint yEnd3 = 300;
    lineBres(x3,y3,xEnd3,yEnd3);
 
    glColor3f(0.98,0.625,0.12);
    glPointSize(4.0);
    GLint x4 = 300;
    GLint y4 = 350;
    GLint xEnd4 = 500;
    GLint yEnd4 = 500;
    lineBres(x4,y4,xEnd4,yEnd4);
 
    glColor3f(0.1,0.0,0.0);
    glPointSize(4.0);
    GLint xw = 500;
    GLint yw = 500;
    GLint xEndw = 350;
    GLint yEndw = 500;
    lineBres(xw,yw,xEndw,yEndw);
 
 
    glColor3f(0.60,0.40,0.12);
    glPointSize(4.0);
    GLint x5 = 300;
    GLint y5 = 100;
    GLint xEnd5 = 500;
    GLint yEnd5 = 500;
    lineBres(x5,y5,xEnd5,yEnd5);
 
    glColor3f(0.60,0.40,0.12);
    glPointSize(4.0);
    GLint x6 = 600;
    GLint y6 = 50;
    GLint xEnd6 = 700;
    GLint yEnd6 = 50;
    lineBres(x6,y6,xEnd6,yEnd6);
 
    glColor3f(0.60,0.40,0.12);
    glPointSize(4.0);
    GLint x7 = 600;
    GLint y7 = 100;
    GLint xEnd7 = 700;
    GLint yEnd7 = 100;
    lineBres(x7,y7,xEnd7,yEnd7);
 
    glColor3f(0.60,0.40,0.12);
    glPointSize(4.0);
    GLint x8 = 600;
    GLint y8 = 150;
    GLint xEnd8 = 700;
    GLint yEnd8 = 150;
    lineBres(x8,y8,xEnd8,yEnd8);
 
    glColor3f(0.60,0.40,0.12);
    glPointSize(4.0);
    GLint x9 = 600;
    GLint y9 = 200;
    GLint xEnd9 = 700;
    GLint yEnd9 = 250;
    lineBres(x9,y9,xEnd9,yEnd9);
 
    glColor3f(1.0,0.0,1.0);
    glPointSize(4.0);
    GLint x10 = 600;
    GLint y10 = 300;
    GLint xEnd10 = 700;
    GLint yEnd10 = 350;
    lineBres(x10,y10,xEnd10,yEnd10);
 
    glColor3f(0.0,1.0,0.0);
    glPointSize(4.0);
    GLint x11 = 600;
    GLint y11 = 400;
    GLint xEnd11 = 700;
    GLint yEnd11 = 450;
    lineBres(x11,y11,xEnd11,yEnd11);  
}

int main(int argc, char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(0,0);
    glutCreateWindow("09018210 Agus Suroyo Garis");
    display();
    glutDisplayFunc(drawMyLine);
    glutMainLoop();
    return 0;
}

0 komentar:

Posting Komentar