Feedjit

Articles for you

Saturday, June 22, 2013

Bresmen's Line Algorithm for Line Drawing in all vertices C++ Source Code OpengL VS-2012 Computer Graphics

#include <glut.h>
using namespace std;
void Start_Line( );
void Set_Graphics( int, int );
void Line_Algo( int, int, int, int );

int main( int argc, char** argv  )
{
   
    glutInit( &argc, argv );
    glutInitWindowSize( 800,600);
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow( " Line Drawing" );
    glutDisplayFunc( Start_Line );
    Set_Graphics( 100, 100 /*window_width, window_height*/ );
    glutMainLoop( );
    return 0;
}

void DrawPixel( int x_Axis, int y_Axis )
{
    glPointSize( 2.0 );
    glBegin( GL_POINTS );
    glVertex2i( x_Axis, y_Axis );
    glEnd( );
    glFlush( );
}

void Line_Algo( int starting_X, int starting_Y, int ending_X, int ending_Y )
{    int delta, deltaX, deltaY, x_Axis, y_Axis, slope, increment_E, increment_NE;

    if( starting_X > ending_X )
    {
        Line_Algo( ending_X, ending_Y, starting_X, starting_Y );
        return;
    }

    deltaX = ending_X - starting_X;
    deltaY = ending_Y - starting_Y;

    if( deltaY < 0 )
    {
        slope = -1;
        deltaY = - deltaY;
    }
    else
    {
        slope = 1;
    }

    increment_E = 2 * deltaY;
    increment_NE = 2 * ( deltaY - deltaX );

    delta = 2 * deltaY - deltaX;

    x_Axis = starting_X;
    y_Axis = starting_Y;

while( x_Axis < ending_X )
    {
        DrawPixel( x_Axis, y_Axis );

        if( delta < 0 )
        {
            delta += increment_E;
            x_Axis ++;
        }
        else
        {
            delta += increment_NE;
            y_Axis += slope;
            x_Axis++;
        }
       
    }
}
void Set_Graphics( int width, int height )
{
    glClearColor( 1.0, 1.0, 1.0, 0.0 );
    glColor3f( 0.0f, 0.0f, 0.0f );
    gluPerspective( 45, ( float ) width / height, 1.0, 0 );
    glMatrixMode( GL_MODELVIEW );
    glTranslatef( 0, 0, 5 * ( -height) );
}
void Start_Line( )
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    Line_Algo( 0, 0, 40,-40);
    glutSwapBuffers( );
}

Read More

Articles for you