Future Vision BIE Future Vision BIE


ONE STOP FOR ALL STUDY MATERIALS & LAB PROGRAMS


E MENU Whatsapp Share Join Telegram, to get Instant Updates
× NOTE! Click on MENU to Browse between Subjects...

Advertisement

COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT

[As per Choice Based Credit System (CBCS) scheme]

(Effective from the academic year 2017 - 2018)

SEMESTER - VI

Subject Code 17CSL68

IA Marks 40

Number of Lecture Hours/Week 01I + 02P

Exam Marks 60



17CSL68 - COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT

PROGRAM - 1

Implement Brenham's line drawing algorithm for all types of slope.

Code Credits Prof Shankar R, BMSIT

DESIGN Credits Mr K B Hemanth Raj - Admin




Advertisement



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<GL/glut.h>
#include<stdio.h>
int x1, y1, x2, y2;

void draw_pixel(int x, int y)
{
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_POINTS);
    glVertex2i(x, y);
    glEnd();
}

void bresenhams_line_draw(int x1, int y1, int x2, int y2)
{
    float dx = x2 - x1;
    float dy = y2 - y1;
    float m = dy/dx;

    if(m < 1)
    {
        int decision_parameter = 2*dy - dx;
        int x = x1;                 // initial x
        int y = y1;                 // initial y
        if(dx < 0)                 //  decide the first point and second point
         {
            x = x2;
            y = y2;
            x2 = x1;
        }
        draw_pixel(x, y);    // plot a point
        while(x < x2)           // from 1st point to 2nd point
        {
            if(decision_parameter >= 0)
            {
                x = x+1;
                y = y+1;
                decision_parameter=decision_parameter + 2*dy - 2*dx * (y+1 - y);
            }
            else
            {
                x = x+1;
                y = y;
                decision_parameter = decision_parameter + 2*dy - 2*dx * (y- y);
            }
            draw_pixel(x, y);
        }
    }

    else if(m > 1)
    {
        int decision_parameter = 2*dx - dy;
        int x = x1; // initial x
        int y = y1; // initial y
        if(dy < 0)
        {
            x = x2;
            y = y2;
            y2 = y1;
        }
        draw_pixel(x, y);
        while(y < y2)
        {
            if(decision_parameter >= 0)
            {
                x = x+1;
                y = y+1;
                decision_parameter=decision_parameter + 2*dx - 2*dy * (x+1 - x);
            }
            else
            {
                y = y+1;
                x = x;
                decision_parameter = decision_parameter + 2*dx - 2*dy * (x- x);
            }
            draw_pixel(x, y);
        }
    }

    else if (m == 1)
    {
        int x = x1;
        int y = y1;
        draw_pixel(x, y);
        while(x < x2)
        {
            x = x+1;
            y = y+1;
            draw_pixel(x, y);
        }
    }
}

void init()
{
    glClearColor(1,1,1,1);
    gluOrtho2D(0.0, 500.0, 0.0, 500.0);       // left ->0, right ->500, bottom ->0, top ->500
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    bresenhams_line_draw(x1, y1, x2, y2);
    glFlush();
}

int main(int argc, char **argv)
{
    printf( "Enter Start Points (x1,y1)\n");
    scanf("%d %d", &x1, &y1);                                   // 1st point from user

    printf( "Enter End Points (x2,y2)\n");
    scanf("%d %d", &x2, &y2);                                  // 2nd point from user

    glutInit(&argc, argv);                                          // initialize graphics system
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);   // single buffered mode with RGB colour variants
    glutInitWindowSize(500, 500);                        // 500 by 500 window size
    glutInitWindowPosition(220, 200);                  // where do you wanna see your window
    glutCreateWindow("Bresenham's Line Drawing - FVBIE");  // the title of your window

    init();                                                                     // initialize the canvas

    glutDisplayFunc(display);                                   // call display function

    glutMainLoop();                                                   // run forever
}

Advertisement

Case 1:

m < 1



output
Fig 1.1: m<1 shell prompt .


Advertisement

output
Fig 1.2: m<1 output line .

Case 2:

m > 1



output
Fig 1.3: m>1 shell prompt .

output
Fig 1.4: m>1 output line .

Case 3:

m = 1



output
Fig 1.5: m=1 shell prompt .

output
Fig 1.6: m=1 output line .

× Note Please Share the website link with Your Friends and known Students...

-ADMIN

× Note Page Number is specified to navigate between Pages...
T = Text book
QB = Question Bank
AS = Amswer Script


-ADMIN

Advertisement