Aim: Write A Program To Draw A Line Using Bresenham's Algorithm and Give The Output Respective User Requirement Using Mouse Interface

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Advance Computer Graphics

Practical::2

Enrollment No-110510702012

Aim : Write a program to draw a line using Bresenhams Algorithm and give the Output respective User requirement using mouse interface.
# include # include # include # include # include // <iostream.h> <conio.h> <graphics.h> <dos.h> <math.h>

void bresenham (float ,float ,float ,float ); union REGS in,out; int x1,y1,startClick; int x2,y2,endClick; void callmouse() { in.x.ax=1; int86(0x33,&in,&out); } void mousePosition (int &xC,int &yC,int &click) { getch(); in.x.ax=3; int86(0x33,&in,&out); click=in.x.bx; xC=out.x.cx; yC=out.x.dx; cleardevice(); } void rest(int x1,int y1,int x2,int y2) { in.x.ax=7; in.x.cx=x1; in.x.dx=x2; int86(0x33,&in,&out); in.x.ax=8; in.x.cx=y1; in.x.dx=y2; int86(0x33,&in,&out); } void main() { int gDriver=DETECT, gMode; initgraph(&gDriver,&gMode,"c:\\turboc3\\BGI"); callmouse(); rest(0,0,625,475); cout<<endl<<" Click to set starting point : "; mousePosition(x1,y1,startClick); clrscr(); cout<<endl<<" Click to set end point : "; mousePosition(x2,y2,endClick); bresenham(x1, y1, x2, y2); cout<<endl<<" Bresenham Line Algorithm : "; cout<<endl<<" ------------------------------- ";

M.Tech(CE)Semister-1

2011-2012

1 of 4

Advance Computer Graphics

Practical::2

Enrollment No-110510702012
x1 x2

cout<<endl<<" Start Point ( X , Y ) : ( "<< << " , "<<y1<<" )"; cout<<endl<<" End Point ( X , Y ) : ( " << << " , "<<y2<<" )"; getch(); } // Actual Logic Of Bresenham Algorithm

void bresenham (float x1,float y1,float x2,float y2) { int startX,startY,endPoint,p,diffX,diffY,increment=0; diffY = abs(y2 - y1); diffX = abs(x2 - x1); if(diffX > diffY) { p = (2*diffY) - diffX; if( x1 < x2 ) { startX = x1; startY = y1; endPoint = x2; if(y1 < y2) increment = 1; if(y1 > y2) increment = -1; } else { startX = x2; startY = y2; endPoint = x1; if(y2 < y1) increment = 1; if(y2 > y1) increment = -1; } while(startX <= endPoint) { putpixel(startX,startY,15); if(p < 0) p = p + (2 * diffY); else { startY = startY + increment; p = p + 2 * (diffY - diffX); } startX++; } } else { p = (2*diffX) - diffY; if(y1 < y2) {

M.Tech(CE)Semister-1

2011-2012

1 of 4

Advance Computer Graphics

Practical::2

Enrollment No-110510702012

startX = x1; startY = y1; endPoint = y2; if(x1 < x2) increment = 1; if(x1 > x2) increment = -1; } else { startX = x2; startY = y2; endPoint = y1; if(x2 < x1) increment = 1; if(x2 > x1) increment = -1; } while(startY <= endPoint) { putpixel(startX,startY,15); if(p < 0) p = p + (2*diffX); else { startX = startX + increment; p = p + 2*(diffX - diffY); } startY++; } }

M.Tech(CE)Semister-1

2011-2012

1 of 4

Advance Computer Graphics

Practical::2

Enrollment No-110510702012

M.Tech(CE)Semister-1

2011-2012

1 of 4

You might also like