Draw Brasenham Line Drawing Algorithms with Random Lines and Colors

Code:

#include <windows.h>
#include <GL\glut.h>
#include <stdlib.h>
#include<iostream>
using namespace std;

int x, y, a, b, nol, R, G, B;

void myInit (void)

{

    glClearColor(0.0,0.0,0.0,0.0); // sets background color to white

    // sets a point to be 4x4 pixels

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(0.0, 500.0, 0.0, 400.0); // the display area in world coordinates.

}
void brasenham(int x1, int y1, int x2, int y2)
{

    int dx = x2-x1;
    int dy = y2-y1;
    int p = (2*dy)-dx;
    for(int i =0; i < dx; i++)
    {
        if(p < 0)
        {
            x1++;
            glVertex2i(x1, y1);
            p = p+(2*dy);

        }
        else if(p>0)
        {
            x1++;
            y1++;
            glVertex2i(x1, y1);
            p = p+(2*dy)-(2*dx);

        }


    }

}

void myDisplay(void)

{
    int nol = rand() % 100;

     glClear(GL_COLOR_BUFFER_BIT); // clears the screen
    // setsthe drawing colour

     glPointSize(4.0);

    for(int i = 0; i<nol; i++){
        int x = rand() % 750;
        int y = rand() % 850;
        int a = rand() % 950;
        int b = rand() % 680;
        int R = rand() % 255;
        int G = rand() % 255;
        int B = rand() % 255;
        glColor3ub(R, G, B) ;
     glBegin(GL_POINTS);
        brasenham(x, y, a, b);
     glEnd();
    }


     glFlush(); // sends all output to display;

}


int main (int argc, char **argv)
{

//scanf("%d,%d,&d,&d",&x,&y,&a,&b);



    glutInit (&argc, argv); // to initialize the toolkit;

    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // sets the display mode

    glutInitWindowSize (640, 480); // sets the window size

    glutInitWindowPosition (10, 10); // sets the starting position for the window

    glutCreateWindow ("My first OpenGL program!"); // creates the window and sets the title

    glutDisplayFunc (myDisplay);
    myInit(); // additional initializations as necessary

    glutMainLoop(); // go into a loop until event occurs
    return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *