# Liang Barsky Line Clipping Algorithm (C++)

#include <conio.h> #include <iostream.h> #include <graphics.h> void main(){ int gdriver = DETECT, gmode; initgraph(&gdriver,&gmode,”C:\TC\BGI”); setcolor(BLUE); int xl,yl,xh,yh; cout<<“Enter bottom left and top right co-ordinates of the window: “; cin>>xl>>yl>>xh>>yh; rectangle(xl,yl,xh,yh); int x1,y1,x2,y2; cout<<“Enter endpoints of the line: “; cin>>x1>>y1>>x2>>y2; line(x1,y1,x2,y2); getch(); int p[4],q[4],i,accept=1;                               // To decide if line has to … Continue reading Liang Barsky Line Clipping Algorithm (C++)

# Cohen-Sutherland Line Clipping Algorithm (C++)

#include #include #include static int LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,xl,yl,xh,yh; int getcode(int x,int y){ int code = 0; //Peform Bitwise OR to get outcode if(yyl) code |=BOTTOM; if(xxh) code |=RIGHT; return code; } void main(){ int gdriver = DETECT,gmode; initgraph(&gdriver,&gmode,”C:\TC\BGI”); setcolor(BLUE); cout<>xl>>yl>>xh>>yh; rectangle(xl,yl,xh,yh); int x1,y1,x2,y2; cout<>x1>>y1>>x2>>y2; line(x1,y1,x2,y2); getch(); int outcode1=getcode(x1,y1), outcode2=getcode(x2,y2); int accept = 0;                             … Continue reading Cohen-Sutherland Line Clipping Algorithm (C++)

# 2-D Transformations for an Object (C++)

Problem Definition: Write a program to implement 2-D Transformations viz., (a) Translation (b) Rotation (c) Scaling for an Object. #include <conio.h> #include <iostream.h> #include <graphics.h> #include <math.h> void main(){ int x1=200,y1=200,x2=250,y2=250,x3=180,y3=270,option; int gdriver = DETECT,gmode; initgraph(&gdriver,&gmode,”C:\TC\BGI”); do{ cleardevice(); gotoxy(1,1); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); cout<<“\n1.Translation 2.Scaling 3.Rotation 4.Exit\nEnter your choice: “; cin>>option; switch(option){ case 1: float tx,ty; cout<<“Enter tx & ty: “; cin>>tx>>ty; x1+=tx;x2+=tx;x3+=tx; y1+=ty;y2+=ty;y3+=ty; break; case 2: float sx,sy; … Continue reading 2-D Transformations for an Object (C++)

# Flood Fill Algorithm (C++)

#include <iostream.h> #include <conio.h> #include <graphics.h> #include <dos.h> void ffill(int x,int y,int o_col,int n_col){ int current = getpixel(x,y); if(current==o_col){ delay(1); putpixel(x,y,n_col); ffill(x+1,y,o_col,n_col); ffill(x-1,y,o_col,n_col); ffill(x,y+1,o_col,n_col); ffill(x,y-1,o_col,n_col); } } void main(){ int xc,yc,r; int gdriver = DETECT,gmode; initgraph(&gdriver,&gmode,”C:\TC\BGI”); cout<<“Enter co-ordinates of the centre: “; cin>>xc>>yc; cout<<“Enter radius of circle: “; cin>>r; circle(xc,yc,r); cout<<“Press any key to fill circle…”; getch(); ffill(xc,yc,BLACK,RED); getch(); closegraph(); } Continue reading Flood Fill Algorithm (C++)

# Boundary Fill Algorithm (C++)

#include <iostream.h> #include <conio.h> #include <graphics.h> #include <dos.h> void bfill(int x,int y,int f_col,int b_col){ int current = getpixel(x,y); if(current!=f_col&&current!=b_col){ delay(1); putpixel(x,y,f_col); bfill(x+1,y,f_col,b_col); bfill(x-1,y,f_col,b_col); bfill(x,y+1,f_col,b_col); bfill(x,y-1,f_col,b_col); } } void main(){ int xc,yc,r; int gdriver = DETECT,gmode; initgraph(&gdriver,&gmode,”C:\TC\BGI”); cout<<“Enter co-ordinates of the centre: “; cin>>xc>>yc; cout<<“Enter radius of circle: “; cin>>r; circle(xc,yc,r); cout<<“Press any key to fill circle…”; getch(); bfill(xc,yc,RED,WHITE); getch(); closegraph(); } Continue reading Boundary Fill Algorithm (C++)