BSC IT PRACTICALS: COMPUTER GRAPHICS
Showing posts with label COMPUTER GRAPHICS. Show all posts
Showing posts with label COMPUTER GRAPHICS. Show all posts

Monday, January 15, 2018

Develop the program for Bresenham’s Line drawing algorithm in C++

January 15, 2018 0
  Develop the program for Bresenham’s Line drawing algorithm in C++

CODE:

#include<graphics.h>
#include<iostream.h>
#include<math.h>
#include<conio.h>

void main( )
{
    float x,y,x1,y1,x2,y2,dx,dy,e;
    int i,gd,gm;
    gd=DETECT;
    initgraph(&gd,&gm,"c:\\turboc3\\bgi");

    cout<<"Enter the value of x1 and y1 : ";
    cin>>x1>>y1;
    cout<<"Enter the value of x2 and y2: ";
    cin>>x2>>y2;

    dx=abs(x2-x1);
    dy=abs(y2-y1);
    x=x1;y=y1;
e=(dy/dx)-0.5;
putpixel(x,y,15);
for(i=1;i<=dx;i++){
while(e>=0){
y=y+1;
e=e-1;

}
x=x+1;
e=dy/dx+e;
putpixel(x,y,15);

}
getch();
closegraph();

}

Develop the program for DDA Line drawing algorithm in c++

January 15, 2018 0
Develop the program for DDA Line drawing algorithm in c++

CODE:

#include <graphics.h>
#include <iostream.h>
#include <math.h>


void main( )
{
    float x,y,x1,y1,x2,y2,dx,dy,step;
    int i,gd=DETECT,gm;

    initgraph(&gd,&gm,"c:\\turboc3\\bgi");

    cout<<"Enter the value of x1 and y1 : ";
    cin>>x1>>y1;
    cout<<"Enter the value of x2 and y2: ";
    cin>>x2>>y2;

    dx=abs(x2-x1);
    dy=abs(y2-y1);

    if(dx>=dy)
        step=dx;
    else
        step=dy;

    dx=dx/step;
    dy=dy/step;

    x=x1;
    y=y1;

    i=1;
    while(i<=step)
    {
        putpixel(x,y,5);
        x=x+dx;
        y=y+dy;
        i=i+1;
        delay(100);
    }

    closegraph();

}