#include<graphics.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm,NULL); line(0,300,640,300); setcolor(4); circle(100,285,15); circle(200,285,15); circle(100,285,5); circle(200,285,5); line(65,285,85,285); line(115,285,185,285); line(215,285,235,285); line(65,285,65,260); line(235,285,235,260); line(65,260,100,255); line(235,260,200,255); line(100,255,115,235); line(200,255,185,235); line(115,235,185,235); line(106,255,118,238); line(118,238,118,255); line(106,255,118,255); line(194,255,182,238); line(182,238,182,255); line(194,255,182,255); line(121,238,121,255); line(121,238,148,238); line(121,255,148,255); line(148,255,148,238); line(179,238,179,255); line(179,238,152,238); line(179,255,152,255); line(152,255,152,238); setcolor(4); //floodfill(150,200,4); getch(); closegraph(); }
Showing posts with label Graphics and Multimedia Lab. Show all posts
Showing posts with label Graphics and Multimedia Lab. Show all posts
Draw Car - Graphics Programming in gcc - C++ Program
Sun Rise - Graphics Program in GCC - C++ Program
Source code:
Output:
#include<graphics.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm,NULL); int midx,midy,r=10; midx=getmaxx()/2; while(r<=50) { cleardevice(); setcolor(WHITE); line(0,310,160,150); line(160,150,320,310); line(320,310,480,150); line(480,150,640,310); line(0,310,640,310); arc(midx,310,225,133,r); floodfill(midx,300,15); if(r>20) { setcolor(7); floodfill(2,2,15); setcolor(6); floodfill(150,250,15); floodfill(550,250,15); setcolor(2); floodfill(2,450,15); } delay(100); r+=2; } getch(); closegraph(); }
Output:
Walking Man - Graphics Programming - Libgraph - GCC - Graphics & Multimedia Lab - C++ Program
Walking Man
Graphics Programming
GCC - Graphics & Multimedia Lab - C++ Program
Source code:
#include<graphics.h> using namespace std; int main() { int gd=DETECT, gm; initgraph(&gd, &gm,NULL); int i,walk=0; setcolor(BROWN); bar3d(10,280,getmaxx()-50,320,10,50); outtextxy(200,300,"www.2k8618.blogspot.com"); setcolor(WHITE); circle(70,140,30); circle(60,130,3); circle(80,130,3); line(70,135,70,145); //line(65,155,75,155); arc(70,140,60,120,15); line(70,170,70,220); line(70,170,50,220); line(70,170,90,220); line(70,220,55,270); line(70,220,85,270); setcolor(9); fillellipse(150,100,60,40); outtextxy(110,90,"hi," ); outtextxy(110,100,"go for a walk?"); delay(5000); for(i=0;i<30;i++) { /* cleardevice(); walk+=5; bar3d(10,280,getmaxx()-50,320,10,50); circle(70+walk,140,30); line(70+walk,170,70+walk,220); line(70+walk,170,40+walk,220); line(70+walk,170,100+walk,220); line(70+walk,220,30+walk,270); line(70+walk,220,110+walk,270); delay(500);*/ walk+=5; cleardevice(); setcolor(BROWN); bar3d(10,280,getmaxx()-50,320,10,50); outtextxy(200,300,"www.2k8618.blogspot.com"); setcolor(WHITE); circle(70+walk,140,30); line(70+walk,170,70+walk,220); line(70+walk,170,50+walk,220); line(70+walk,170,90+walk,220); line(70+walk,220,55+walk,270); line(70+walk,220,85+walk,270); delay(250); walk+=5; cleardevice(); circle(70+walk,140,30); line(70+walk,170,70+walk,270); setcolor(BROWN); bar3d(10,280,getmaxx()-50,320,10,50); outtextxy(200,300,"www.2k8618.blogspot.com"); delay(250); walk+=5; cleardevice(); bar3d(10,280,getmaxx()-50,320,10,50); outtextxy(200,300,"www.2k8618.blogspot.com"); setcolor(WHITE); circle(70+walk,140,30); line(70+walk,170,70+walk,220); line(70+walk,170,60+walk,220); line(70+walk,170,80+walk,220); line(70+walk,220,55+walk,270); line(70+walk,220,85+walk,270); delay(250); } circle(60+walk,130,3); circle(80+walk,130,3); line(70+walk,135,70+walk,145); line(65+walk,155,75+walk,155); //arc(70,140,60,120,15); setcolor(8); fillellipse(walk-10,100,60,40); outtextxy(walk-30,100,"lonely..."); delay(2500); getch(); closegraph(); return 0; }
Face Drawing - Graphics Programming in Linux - C++ Program
Face Drawing
Graphics Programming in Linux - C++ Program
Source code:
#include<iostream> #include<graphics.h> using namespace std; int main() { int gd = DETECT, gm; initgraph(&gd, &gm, NULL); outtextxy(150, 15, "Face drawing..."); int i=0,p=300,q=100,k=80,m=0; circle(300,250,150); circle(250,200,25); circle(350,200,25); line(300,225,300,275); line(301,225,301,275); arc(300,250,60,120,25); arc(300,250,240,300,75); arc(300,250,60,120,75); getch(); closegraph(); return 0; }
Output:
Line Clipping - Cohen Sutherland Line Clipping Algorithm - Graphics.h in Ubuntu - C++ Program
Line Clipping
Cohen Sutherland Line Clipping Algorithm
Graphics & Multimedia Lab - C++ Program
Program:
#include<iostream> #include<graphics.h> using namespace std; const int TOP=8,BOTTOM=4,RIGHT=2,LEFT=1; typedef int outcode; outcode compute(int x, int y , int xmax, int ymax, int xmin, int ymin) { outcode oc=0; if(x<xmin) oc|=LEFT; else if(x>xmax) oc|=RIGHT; if(y>ymax) oc|=TOP; else if(y<ymin) oc|=BOTTOM; return oc; } void coh(int x1,int y1,int x2,int y2, int xmin,int ymin, int xmax, int ymax) { bool accept = false, done=false;double m; outcode o1,o2,ot; o1=compute(x1,y1,xmax,ymax,xmin,ymin); o2=compute(x2,y2,xmax,ymax,xmin,ymin); do{ if(!(o1 | o2)) { done=true; accept=true; } else if(o1&o2) { done=true; } else { int x,y; ot=o1?o1:o2; if(ot & TOP) { y=ymax; x = x1 + (x2-x1)*(ymax-y1)/(y2-y1); } else if(ot & BOTTOM) { y=ymin; x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1); } else if(ot & RIGHT) { x=xmax; y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1); } else if(ot & LEFT) { x=xmin; y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1); } if(ot==o1) { x1=x; y1=y; o1=compute(x1,y1,xmax,ymax,xmin,ymin); } else { x2=x; y2=y; o2=compute(x2,y2,xmax,ymax,xmin,ymin); } } }while(done==false); if(accept==true) { line(x1,y1,x2,y2); } } int main() { int gd=DETECT,gm,x,y,j,i; initgraph(&gd,&gm,NULL); outtextxy(150,15, "Cohen Sutherland"); int xmx=200,ymx=200,xmn=100,ymn=100; int ax1=50,ay1=75,bx1=50,by1=325; int ax2=150,ay2=75,bx2=150,by2=325; int ax3=250,ay3=75,bx3=250,by3=325; int ax4=50,ay4=75,bx4=250,by4=75; int ax5=50,ay5=175,bx5=250,by5=175; int ax6=50,ay6=275,bx6=250,by6=275; int ax7=50,ay7=75,bx7=275,by7=275; int ax8=250,ay8=75,bx8=75,by8=275; line(ax1,ay1,bx1,by1); line(ax2,ay2,bx2,by2); line(ax3,ay3,bx3,by3); line(ax4,ay4,bx4,by4); line(ax5,ay5,bx5,by5); line(ax6,ay6,bx6,by6); line(ax7,ay7,bx7,by7); line(ax8,ay8,bx8,by8); setcolor(4); rectangle(xmn,ymn,xmx,ymx); delay(1000); cleardevice(); setcolor(4); rectangle(xmn,ymn,xmx,ymx); setcolor(15); coh(ax1,ay1,bx1,by1,xmn,ymn,xmx,ymx); coh(ax2,ay2,bx2,by2,xmn,ymn,xmx,ymx); coh(ax3,ay3,bx3,by3,xmn,ymn,xmx,ymx); coh(ax4,ay4,bx4,by4,xmn,ymn,xmx,ymx); coh(ax5,ay5,bx5,by5,xmn,ymn,xmx,ymx); coh(ax6,ay6,bx6,by6,xmn,ymn,xmx,ymx); coh(ax7,ay7,bx7,by7,xmn,ymn,xmx,ymx); coh(ax8,ay8,bx8,by8,xmn,ymn,xmx,ymx); delay(1000); getch(); closegraph(); return 0; }
Output:
A man in rain - Graphics programming in ubuntu - Graphics & Multimedia Lab - C++ Program
A Man In Rain ...
Graphics programming in ubuntu - Graphics & Multimedia Lab - C++ Program
Source code:
#include<iostream> #include<graphics.h> using namespace std; int main() { int gd=DETECT,gm,x,y,j,i; initgraph(&gd,&gm,NULL); outtextxy(150,15, "A man in rain... "); int points[]={51,101,151,101,150,getmaxy(),50,getmaxy(),51,101 }; for(i=0;i<getmaxy()-10;i+=25) for(j=0;j<getmaxx()-10;j+=30) line(j,i,j+10,i+10); arc(100, 100, 135, 315, 50); line(50,100,135,63); setcolor(BLACK); fillpoly(5,points); setcolor(WHITE); line(95,78,135,175); arc(115, 185, 315, 495, 22); circle(120,130,15); line(120,145,120,250); line(120,250,100,280); line(120,250,140,280); line(120,170,100,200); line(120,170,140,200); getch(); closegraph(); return 0; }
Output:
nn@linuxmint ~ $ g++ g8.cpp -lgraph g8.cpp: In function ‘int main()’: g8.cpp:9: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out a.out: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) - (long) (dpy->request)) <= 0)' failed. Aborted nn@linuxmint ~ $
Rain & Umbrella - Graphics Programming - C++ Program
Rain & Umbrella - Graphics Programming - C++ Program
Source code:
#include<iostream> #include<graphics.h> using namespace std; int main() { int gd=DETECT,gm,x,y,j,i; initgraph(&gd,&gm,NULL); outtextxy(150,15, "Rain & Umbrella"); int points[]={50,100,150,100,150,getmaxy(),50,getmaxy(),50,100 }; for(i=0;i<getmaxy();i+=5) for(j=0;j<getmaxx();j+=15) putpixel(j,i,WHITE); setcolor(RED); arc(100, 100, 180, 360, 50); line(50,99,150,99); setcolor(BLACK); fillpoly(5,points); setcolor(WHITE); line(100,100,100,175); arc(80, 175, 0, 180, 20); getch(); closegraph(); return 0; }
Output:
nn@linuxmint ~ $ g++ g7.cpp -lgraph g7.cpp: In function ‘int main()’: g7.cpp:9: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out a.out: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) - (long) (dpy->request)) <= 0)' failed. nn@linuxmint ~ $
Colours in Graphics.h - GCC - Graphics & Multimedia Lab - C++ Program
Colours in Graphics.h
GCC - Graphics & Multimedia Lab - C++ Program
Source code:
#include<graphics.h> using namespace std; int main() { int i,j=1,k=0,gd=DETECT, gm,ych,xch; char* buf; char colours[18][15]={"BLACK","BLUE","GREEN","CYAN","RED","MAGENTA","BROWN","LIGHTGRAY","DARKGRAY","LIGHTBLUE","LIGHTGREEN","LIGHTCYAN","LIGHTRED","LIGHTMAGENTA","YELLOW","WHITE"}; initgraph(&gd, &gm,NULL); outtextxy(150,15, "Colours in Graphics.h."); ych=0;xch=0; for(i=0;i<8;i++) { setcolor(i); for(j=k;j<k+50;j++) { line(j+50,80,j+50,200); } k=j; outtextxy(xch+50,80+ych,colours[i]); ych+=15; xch+=50; } k=0;ych=0;xch=0; for(i=8;i<=15;i++) { setcolor(i); for(j=k;j<k+50;j++) { line(j+50,230,j+50,350); } k=j;outtextxy(xch+50,235+ych,colours[i]); ych+=15; xch+=50; } getch(); closegraph(); return 0; }
Output:
nn@linuxmint ~ $ g++ g5.cpp -lgraph gp5.cpp: In function ‘int main()’: gp5.cpp:11: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out a.out: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) - (long) (dpy->request)) <= 0)' failed. Aborted nn@linuxmint ~ $
Draw Polygons in Graphics.h using drawpoly() & fillpoly() - Linux Mint - Libgraph - Graphics & Multimedia Lab - C++ Program
Draw Polygons in Graphics.h using drawpoly() & fillpoly()
- Linux Mint - Libgraph - Graphics & Multimedia Lab - C++ Program
- drawpoly( number_of_vertices+1 , sequence_of_polygonpoints ) - draw polygon
- fillpoly( number_of_vertices+1 , sequence_of_polygonpoints ) - draw & fill polygon
#include<graphics.h> using namespace std; int main() { int gd=DETECT, gm; int triangle[8]={20,150, 60,70, 110,150, 20,150}; int rect[10]={150,60, 280,60, 280,150, 150,150, 150,60}; int pentagon[12]={340,150, 320,110, 360,70, 400,110, 380,150, 340,150}; int hexagon[14] ={ 360,260, 340,240, 360,220, 400,220, 420,240, 400,260, 360,260}; int octagon[18]={450,150, 430,120, 430,100, 450,70, 500,70, 520,100, 520,120, 500,150, 450,150}; int poly1[10]={50,400, 50,300, 150,400, 250,300, 250,400 }; int poly2[12]={350,430, 350,390, 430,350, 350,310, 300,410, 350,430 }; initgraph(&gd, &gm,NULL); outtextxy(150,15, "Polygon Drawing in Graphics.h using drawpoly() & fillpoly()."); drawpoly(4,triangle); outtextxy(30,160, "Triangle"); drawpoly(5,rect); outtextxy(190, 160, "Rectangle"); drawpoly(6,pentagon); outtextxy(330, 160, "Pentagon"); drawpoly(9,octagon); outtextxy(450, 160, "Octagon"); fillpoly(7,hexagon); outtextxy(362, 262, "Hexagon"); drawpoly(5,poly1); drawpoly(6,poly2); outtextxy(400, 400, "Polygon"); getch(); closegraph(); return 0; }
Output:
nn@linuxmint ~ $ g++ g4.cpp -lgraph g4.cpp: In function ‘int main()’: g4.cpp:18: warning: deprecated conversion from string constant to ‘char*’ g4.cpp:22: warning: deprecated conversion from string constant to ‘char*’ g4.cpp:25: warning: deprecated conversion from string constant to ‘char*’ g4.cpp:27: warning: deprecated conversion from string constant to ‘char*’ g4.cpp:29: warning: deprecated conversion from string constant to ‘char*’ g4.cpp:33: warning: deprecated conversion from string constant to ‘char*’ g4.cpp:37: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out a.out: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) - (long) (dpy->request)) <= 0)' failed. Aborted nn@linuxmint ~ $
Ellipse & Sector in Graphics.h - Linux - Libgraph - Graphics & Multimedia Lab - C++ Program
Ellipse & Sector in Graphics.h
Linux - Libgraph - Graphics & Multimedia Lab - C++ Program
- ellipse( x0, y0, start_angle, end_angle, x_axis, y_axis)
- sector( x0, y0, start_angle, end_angle, x_axis, y_axis)
Source code:
#include<graphics.h> using namespace std; int main() { int gd=DETECT, gm; initgraph(&gd, &gm,NULL); outtextxy(150,15, "Ellipse & Sector in Graphics.h"); ellipse(150,150,0,360,100,50); outtextxy(201,201, "Ellipse"); sector(300,300,0,270,100,50); outtextxy(405, 351, "Sector"); getch(); closegraph(); return 0; }
Output:
nn@linuxmint ~ $ g++ g3.cpp -lgraph g3.cpp: In function ‘int main()’: g3.cpp:11: warning: deprecated conversion from string constant to ‘char*’ g3.cpp:15: warning: deprecated conversion from string constant to ‘char*’ g3.cpp:18: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out a.out: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) - (long) (dpy->request)) <= 0)' failed. nn@linuxmint ~ $
Circle, Arc & Pieslice in Graphics.h - Ubuntu - Libgraph - Graphics & Multimedia Lab - C++ Program
Circle, Arc & Pieslice in Graphics.h
Ubuntu - Libgraph - Graphics & Multimedia Lab - C++ Program
- circle(x0,y0,radius) - circle draws a circle with centre at x0,y0 & given radius in the current drawing colour.
- arc(x0,y0,start_angle,end_angle,radius) - arc draws a circular arc with centre at x0,y0 , given starting angle and ending angle & given radius in the current drawing color.
- pieslice(x0,y0,start_angle,end_angle,radius) - It is just like arc() but it fills it using the current fill colour.
Source code:
#include<graphics.h> using namespace std; int main() { int gd=DETECT, gm; initgraph(&gd, &gm,NULL); outtextxy(150,15, " Circle, Arc & Pieslice in Graphics.h"); arc(400,150,0,270,50); outtextxy(506,201,"Arc"); circle(150,150,50); outtextxy(201,201, "Circle"); pieslice(300,300,0,270,50); outtextxy(365, 321, "Pieslice"); getch(); closegraph(); return 0; }
Output:
nn@linuxmint ~ $ g++ g2.cpp -lgraph g2.cpp: In function ‘int main()’: g2.cpp:11: warning: deprecated conversion from string constant to ‘char*’ g2.cpp:14: warning: deprecated conversion from string constant to ‘char*’ g2.cpp:17: warning: deprecated conversion from string constant to ‘char*’ g2.cpp:20: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out a.out: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) - (long) (dpy->request)) <= 0)' failed. Aborted nn@linuxmint ~ $
Basic Shapes in Graphics.h - Ubuntu - Graphics & Multimedia Lab - C++ Program
Basic Shapes in Graphics.h
1. Line (x0,y0,x1,y1)
2. Rectangle(left,top,right,bottom)
3. Circle(x0,y0,radius)
4. Ellipse(x0,y0,startangle,endangle,xaxis,yaxis)
Sourcecode:
#include<graphics.h> using namespace std; int main() { int gd=DETECT, gm; initgraph(&gd, &gm,NULL); outtextxy(150,15, "Some Basic Shapes in Graphics.h"); line(10,50,500,50); outtextxy(501,60,"Line"); circle(150,150,50); outtextxy(201,201, "Circle"); rectangle(350,100,500,200); outtextxy(505, 211, "Rectangle"); ellipse(300, 300,0,360, 100,50); outtextxy(440, 330, "Ellipse"); getch(); closegraph(); return 0; }Output:
nn@linuxmint ~ $ g++ g1.cpp -lgraph g1.cpp: In function ‘int main()’: g1.cpp:12: warning: deprecated conversion from string constant to ‘char*’ g1.cpp:15: warning: deprecated conversion from string constant to ‘char*’ g1.cpp:18: warning: deprecated conversion from string constant to ‘char*’ g1.cpp:21: warning: deprecated conversion from string constant to ‘char*’ g1.cpp:24: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0" after 121 requests (117 known processed) with 0 events remaining. nn@linuxmint ~ $
3D Transformations - Line - Graphics & Multimedia Lab - C++ Program
3D Transformations for a Line
Translation, Scaling , Rotation about X,Y & Z axis
Graphics & Multimedia Lab - C++ Program
Sourcecode:
input3d.txt
transinput.txt
scaleinput.txt
rotninput.txt
Output:
#include<graphics.h> #include<iostream> #include<math.h> using namespace std; int gd=DETECT,gm; int x1=0,y11=0,x2=0,y2=0,z1=0,z2=0,dx=0,dy=0,dz=0,px=0,c,y; float r,r1,rx1,ry1,rx2,ry2,rz1,rz2; FILE *fp,*op; void trans(); void scale(); void rotate_x(); void rotate_y(); void rotate_z(); int main() { initgraph(&gd,&gm,NULL); fp=fopen("input3d.txt","r"); op=fopen("output3d.txt","w"); fscanf(fp,"%d %d %d %d %d %d",&x1,&y11,&z1,&x2,&y2,&z2); y=getmaxy(); fprintf(op,"%s","\n-----3D TRANSFORMATION FOR LINE-----\n"); fprintf(op,"%s","\nEnd Points of Input Line:\n"); fprintf(op,"( %d %d %d) ( %d %d %d) \n",x1,y11,z1,x2,y2,z2); // Scanning coordinates of end points of input line form the file input3d.txt printf("\n\t\t-----3D TRANSFORMATION FOR LINE-----\nScanning coordinates of end points of input line from the file input3d.txt..."); printf("\n\nTranslation:"); trans(); printf("\n\nScaling:"); scale(); printf("\n\nRotation about X-axis:"); rotate_x(); printf("\n\nRotation about Y-axis:"); rotate_y(); printf("\n\nRotation about Z-axis:"); rotate_z(); fclose(fp); fclose(op); getch(); closegraph(); return 0; } void trans() { fp=fopen("transinput.txt","r"); fscanf(fp,"%d %d %d",&dx,&dy,&dz); printf("\nScanning translation amounts from the file transinput.txt..."); fprintf(op,"%s","\nTranslated Points:\n"); fprintf(op,"(%d %d %d) (%d %d %d)\n\n",x1+dx,y11+dy,z1+dz,x2+dx,y2+dy,z2+dz); } void scale() { printf("\nScanning scale factors from the file scaleinput.txt..."); fp=fopen("scaleinput.txt","r"); fscanf(fp,"%d%d%d",&dx,&dy,&dz); fprintf(op,"%s","Scaled Points:\n"); fprintf(op,"(%d %d %d) (%d %d %d)\n\n",x1*dx,y11*dy,z1*dz,x2*dx,y2*dy,z2*dz); } void rotate_x() { printf("\nScanning angle of rotation from the file rotninput.txt..."); fp=fopen("rotninput.txt","r"); fscanf(fp,"%f",&r); r1=r*3.14/180.; rx1=x1; ry1=y11*cos(r1)-z1*sin(r1); rz1=y11*sin(r1)+z1*cos(r1); rx2=x2; ry2=y2*cos(r1)-z2*sin(r1); rz2=y2*sin(r1)+z2*cos(r1); fprintf(op,"%s","Points after rotation about X-axis:\n"); fprintf(op,"%f %f %f %f %f %f\n\n",rx1,ry1,rz1,rx2,ry2,rz2); } void rotate_y() { printf("\nScanning angle of rotation from the file rotninput.txt..."); fp=fopen("rotninput.txt","r"); fscanf(fp,"%f",&r); r1=r*3.14/180.; rx1=z1*sin(r1)+x1*cos(r1); ry1=y11; rz1=z1*cos(r1)-x1*sin(r1); rx2=z2*sin(r1)+x2*cos(r1); ry2=y2; rz2=z2*cos(r1)-x2*sin(r1); fprintf(op,"%s","Points after rotation about Y-axis:\n"); fprintf(op,"%f %f %f %f %f %f\n\n",rx1,ry1,rz1,rx2,ry2,rz2); } void rotate_z() { printf("\nScanning angle of rotation from the file rotninput.txt..."); fp=fopen("rotninput.txt","r"); fscanf(fp,"%f",&r); r1=r*3.14/180.; rx1=x1*cos(r1)-y11*sin(r1); ry1=x1*sin(r1)+y11*cos(r1); rz1=z1; rx2=x2*cos(r1)-y2*sin(r1); ry2=x2*sin(r1)+y2*cos(r1); rz2=z2; fprintf(op,"%s","Points after rotation about Z-axis:\n"); fprintf(op,"%f %f %f %f %f %f\n\n",rx1,ry1,rz1,rx2,ry2,rz2); }Input:
input3d.txt
33 44 55 132 111 121
transinput.txt
10 20 30
scaleinput.txt
10 10 10
rotninput.txt
30.00
Output:
nn@linuxmint ~ $ g++ lab12.cpp -lgraph nn@linuxmint ~ $ ./a.out nn@linuxmint ~ $
-----3D TRANSFORMATION FOR LINE----- End Points of Input Line: ( 33 44 55) ( 132 111 121) Translated Points: (43 64 85) (142 131 151) Scaled Points: (330 440 550) (1320 1110 1210) Points after rotation about X-axis: 33.000000 10.623602 69.628578 132.000000 35.671371 160.279617 Points after rotation about Y-axis: 56.070572 44.000000 31.146282 174.805054 111.000000 38.835480 Points after rotation about Z-axis: 6.593334 54.603371 55.000000 58.858391 162.113205 121.000000
Cohen-Sutherland Line Clipping Algorithm - Graphics & Multimedia Lab - C++ Program
Cohen-Sutherland Line Clipping Algorithm (2)
C++ program - Graphics & Multimedia Lab
Sourcecode:
/* * File: cohsuthlineclip.cpp * Author: nn * * Created on 12 January, 2012, 11:30 AM * * Cohen–Sutherland clipping algorithm clips a line from *P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with *diagonal from (xmin, ymin) to (xmax, ymax). */ #include <cstdlib> #include <graphics.h> #define xmax 350 #define ymax 350 #define xmin 100 #define ymin 100 using namespace std; typedef int OutCode; const int INSIDE = 0; // 0000 const int LEFT = 1; // 0001 const int RIGHT = 2; // 0010 const int BOTTOM = 4; // 0100 const int TOP = 8; // 1000 OutCode ComputeOutCode(double x, double y) { OutCode code; code = INSIDE; // initialised as being inside of clip window if (x < xmin) // to the left of clip window code |= LEFT; else if (x > xmax) // to the right of clip window code |= RIGHT; if (y < ymin) // below the clip window code |= BOTTOM; else if (y > ymax) // above the clip window code |= TOP; return code; } void CohenSutherlandLineClipAndDraw(double x0, double y0, double x1, double y1) { OutCode outcode0 = ComputeOutCode(x0, y0); OutCode outcode1 = ComputeOutCode(x1, y1); bool accept = false; while (true) { if (!(outcode0 | outcode1)) {//Bitwise OR is 0. Trivially accept and get out of loop accept = true; break; } else if (outcode0 & outcode1) {//Bitwise AND is not 0. Trivially reject and get out of loop break; } else { double x, y; OutCode outcodeOut = outcode0? outcode0 : outcode1; if (outcodeOut&TOP){ // point is above the clip rectangle x = x0 + (x1-x0)*(ymax-y0)/(y1-y0); y = ymax; } else if(outcodeOut & BOTTOM) { // point is below the clip rectangle x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0); y = ymin; } else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0); x = xmax; } else if (outcodeOut & LEFT) { // point is to the left of clip rectangle y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0); x = xmin; } if (outcodeOut == outcode0) { x0 = x; y0 = y; outcode0 = ComputeOutCode(x0, y0); } else { x1 = x; y1 = y; outcode1 = ComputeOutCode(x1, y1); } } rectangle(xmin, ymin, xmax, ymax); line(x0, y0, x1, y1); getch(); // cleardevice(); } if(accept) { rectangle(xmin, ymin, xmax, ymax); line(x0, y0, x1, y1); printf("\n\tpoints p0=(%d,%d) p1=(%d,%d)",(int)x0,(int)y0,(int)x1,(int)y1); } } int main(int argc, char** argv) { int gd=DETECT,gm; initgraph(&gd,&gm,NULL); outtextxy(300,10,"BEFORE LINE CLIPPING"); rectangle(xmin,ymin,xmax,ymax); line(10,200,350,300); getch(); cleardevice(); outtextxy(300,10,"AFTER LINE CLIPPING"); CohenSutherlandLineClipAndDraw(10,200,350,300); getch(); closegraph(); return 0; }Output:
nn@linuxmint ~ $ g++ cohsuthlineclip.cpp -lgraph nn@linuxmint ~ $ g++ cohsuthlineclip.cpp -lgraph coh.cpp: In function ‘int main(int, char**)’: coh.cpp:105: warning: deprecated conversion from string constant to ‘char*’ coh.cpp:110: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out
Window To Viewport Transformation - C++ Program - Graphics & Multimedia Lab
Window To Viewport Transformation
C++ Program - Graphics & Multimedia Lab
C++ Program - Graphics & Multimedia Lab
Sourcecode:
#include<graphics.h> #include<iostream> using namespace std; int main() { FILE *ip,*op; float xwmin,xwmax,ywmax,ywmin; float xvmin,xvmax,yvmax,yvmin; float x[10],y[10],yv,xv,sx,sy; int gd=DETECT,gm,i; ip=fopen("w2vinput.txt","r"); op=fopen("w2voutput.txt","w"); fscanf(ip,"%f %f %f %f",&xwmin,&ywmin,&xwmax,&ywmax); fscanf(ip,"%f %f %f %f",&xvmin,&yvmin,&xvmax,&yvmax); for(i=0;i < 3;i++) { fscanf(ip,"%f %f",&x[i],&y[i]); } sx=((xvmax-xvmin)/(xwmax-xwmin)); sy=((yvmax-yvmin)/(ywmax-ywmin)); initgraph(&gd,&gm,NULL); outtextxy(40,10,"Window port"); rectangle(xwmin,ywmin,xwmax,ywmax); for(i=0;i <2;i++) { line(x[i],y[i],x[i+1],y[i+1]); } line(x[2],y[2],x[0],y[0]); getch(); for(i=0;i <3;i++) { x[i]=xvmin+((x[i]-xwmin)*sx); y[i]=yvmin+((y[i]-ywmin)*sy); } outtextxy(190,145,"View port"); rectangle(xvmin,yvmin,xvmax,yvmax); fprintf(op,"%s","Output points:\n"); for(i=0;i <2;i++) { fprintf(op,"x[%d]=%f y[%d]=%f\n",i,x[i],i,y[i]); line(x[i],y[i],x[i+1],y[i+1]); } line(x[2],y[2],x[0],y[0]); fprintf(op,"x[2]=%f y[2]=%f\n",x[2],y[2]); getch(); closegraph(); return 0; }
Output:
"w2vinput.txt"
"w2voutput.txt"
nn@linuxmint ~ $ g++ lab8.cpp -lgraph lab8.cpp: In function ‘int main()’: lab8.cpp:24: warning: deprecated conversion from string constant to ‘char*’ lab8.cpp:38: warning: deprecated conversion from string constant to ‘char*’ nn@linuxmint ~ $ ./a.out
"w2vinput.txt"
22 22 150 150 200 200 350 250 45 45 125 45 125 125
"w2voutput.txt"
Output points: x[0]=226.953125 y[0]=208.984375 x[1]=320.703125 y[1]=208.984375 x[2]=320.703125 y[2]=240.234375
Clip Lines - Cohen-Sutherland Line Clipping Algorithm - C++ Program - Graphics & Multimedia Lab
Cohen-Sutherland Line Clipping Algorithm
C++ program - Graphics & Multimedia Lab
Sourcecode:
#include<graphics.h> #include<iostream> using namespace std; #define MAX1 20 enum { TOP = 0x8, BOTTOM = 0x4, RIGHT = 0x2, LEFT = 0x1 }; enum { FALSE, TRUE }; typedef unsigned int outcode; int m=0; FILE *ip,*op; outcode compute_outcode(int x, int y, int xmin, int ymin, int xmax, int ymax) { outcode oc = 0; if (y > ymax) oc |= TOP; else if (y < ymin) oc |= BOTTOM; if (x > xmax) oc |= RIGHT; else if (x < xmin) oc |= LEFT; return oc; } void cohen_sutherland (float x1, float y1, float x2, float y2, float xmin, float ymin, float xmax, float ymax) { int accept; int done; outcode outcode1, outcode2; accept = FALSE; done = FALSE; outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax); outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax); do { if (outcode1 == 0 && outcode2 == 0) { accept = TRUE; done = TRUE; } else if (outcode1 & outcode2) { done = TRUE; } else { double x, y; double m=(y2-y1)/(x2-x1); int outcode_ex = outcode1 ? outcode1 : outcode2; if (outcode_ex & TOP) { x = x1 + (ymax - y1)/m; y = ymax; } else if (outcode_ex & BOTTOM) { x = x1 + (ymin - y1) /m; y = ymin; } else if (outcode_ex & RIGHT) { y = y1 + (xmax - x1)* m; x = xmax; } else { y = y1 + (xmin - x1)* m; x = xmin; } if (outcode_ex == outcode1) { x1 = x; y1 = y; outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax); } else { x2 = x; y2 = y; outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax); } } } while (done == FALSE); if (accept == TRUE) { fprintf(op,"\nx[%d] y[%d] x[%d] y[%d]=%.3f %.3f %.3f %.3f:(clipped line)",m,m,m+1,m+1,x1,y1,x2,y2); m=m+2; line (x1, y1, x2, y2); } else if((accept == FALSE) && (done == TRUE)) { fprintf(op,"\nThe line (%.3f,%.3f),(%.3f,%.3f)is trivially rejected.",x1,y1,x2,y2); } } int main() { int n; int i, j; int ln[MAX1][4]; int clip[4]; int gd = DETECT, gm; ip=fopen("csinput.txt","r"); op=fopen("csoutput.txt","w"); fscanf (ip,"%d", &n); for (i=0; i<n; i++) for (j=0; j<4; j++) fscanf (ip,"%d", &ln[i][j]); for (i=0; i<4; i++) fscanf (ip,"%d", &clip[i]); initgraph (&gd, &gm, NULL); rectangle (clip[0], clip[1], clip[2], clip[3]); for (i=0; i<n; i++) line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]); getch(); cleardevice(); printf("AFTER CLIPPING"); rectangle (clip[0], clip[1], clip[2], clip[3]); fprintf(op,"OUTPUT POINTS\n"); for (i=0; i<n; i++) { cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3], clip[0], clip[1], clip[2], clip[3]); getch(); } closegraph(); return 0; }Output:
nn@linuxmint ~ $ g++ lab7.cpp -lgraph nn@linuxmint ~ $ ./a.out
"csinput.txt"
4 40 40 125 220 10 10 40 210 75 80 175 175 60 230 210 45 55 55 200 2005
"csoutput.txt"
OUTPUT POINTS x[0] y[0] x[1] y[1]=55.000 71.765 115.556 200.000:(clipped line) The line (10.000,10.000),(40.000,210.000)is trivially rejected.
2D Transformations: Translation, Scaling, Rotation - C++ Implementation - Graphics & Multimedia Lab
2D Transformations: Translation, Scaling, Rotation
C++ program - Graphics & Multimedia Lab
Sourcecode:
#include<graphics.h> #include<iostream> #include<math.h> #include<stdio.h> using namespace std; void translate(); void scale(); void rotate(); int main() { int gd=DETECT,gm,ch,exitp=0; initgraph(&gd,&gm,NULL); int x0,x1,y0,y1; FILE *ip; ip=fopen("2Dip.txt","r"); fscanf(ip,"%d %d %d %d",&x0,&y0,&x1,&y1); rectangle(x0,y0,x1,y1); fclose(ip); translate(); scale(); rotate(); getch(); closegraph(); return 0; } void translate() { int x0,x1,y0,y1; FILE *op,*ip1,*ip2; ip1=fopen("2Dip.txt","r"); fscanf(ip1,"%d %d %d %d",&x0,&y0,&x1,&y1); setcolor(1); fclose(ip1); int tx,ty; ip2=fopen("2Dtransip.txt","r"); fscanf(ip2,"%d %d",&tx,&ty); fclose(ip2); op=fopen("2Dop.txt","a+"); rectangle(x0+tx,y0+ty,x1+tx,y1+ty); outtextxy((x0+tx+20),(y0+ty+20),(char*)"Translation"); fprintf(op,"\nTranslation:\n"); fprintf(op,"%d %d %d %d",x0+tx,y0+ty,x1+tx,y1+ty); fclose(op); } void scale() { int x0,x1,y0,y1; FILE *op,*ip1,*ip2; int sx,sy; setcolor(2); ip1=fopen("2Dip.txt","r"); fscanf(ip1,"%d %d %d %d",&x0,&y0,&x1,&y1); fclose(ip1); ip2=fopen("scaleip.txt","r"); fscanf(ip2,"%d %d",&sx,&sy); op=fopen("2Dop.txt","a+"); fclose(ip2); outtextxy(x0*sx+20,y0*sy+20,(char*)"Scaling"); rectangle(x0*sx,y0*sy,x1*sx,y1*sy); fprintf(op,"\nSclaing:\n"); fprintf(op,"%d %d %d %d",x0*sx,y0*sy,x1*sx,y1*sy); fclose(op); } void rotate() { setcolor(3); FILE *op,*ip1,*ip2; float theta; int x0,x1,x2,x3,x4; int y0,y1,y2,y3,y4; int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4; int refx,refy; ip1=fopen("2Dip.txt","r"); fscanf(ip1,"%d %d %d %d",&x0,&y0,&x1,&y1); fclose(ip1); ip2=fopen("Rotip.txt","r"); fscanf(ip2,"%f",theta); fclose(ip2); op=fopen("2Dop.txt","a+"); theta=theta*(3.14/180); fprintf(op,"\nRotation:\n"); refx=100; refy=100; ax1=refy+(x0-refx)*cos(theta)-(y1-refy)*sin(theta); ay1=refy+(x0-refx)*sin(theta)+(y1-refy)*cos(theta); ax2=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta); ay2=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta); ax3=refy+(x1-refx)*cos(theta)-(y0-refy)*sin(theta); ay3=refy+(x1-refx)*sin(theta)+(y0-refy)*cos(theta); ax4=refy+(x0-refx)*cos(theta)-(y0-refy)*sin(theta); ay4=refy+(x0-refx)*sin(theta)+(y0-refy)*cos(theta); line(ax1,ay1,ax2,ay2); fprintf(op,"%d %d %d %d\n",ax1,ay1,ax2,ay2); line(ax2,ay2,ax3,ay3); fprintf(op,"%d %d %d %d\n",ax2,ay2,ax3,ay3); line(ax3,ay3,ax4,ay4); fprintf(op,"%d %d %d %d\n",ax3,ay3,ax4,ay4); line(ax4,ay4,ax1,ay1); fprintf(op,"%d %d %d %d\n",ax4,ay4,ax1,ay1); fclose(op); outtextxy(ax1+20,ay1+20,(char*)"Rotation "); }
Output:
nn@linuxmint ~ $ g++ lab6.cpp -lgraph nn@linuxmint ~ $ ./a.out
"2Dip.txt"
50 60 100 75
"2Dtransip.txt"
250 250
"scaleip.txt"
3 3
"Rotip.txt"
90.0
"2Dop.txt"
Translation: 300 310 350 325 Sclaing: 150 180 300 225 Rotation: 124 49 124 99 124 99 139 99 139 99 139 49 139 49 124 49
Draw Ellipse - Midpoint Ellipse Algorithm - C++ program - Graphics & Multimedia Lab
Midpoint Ellipse Algorithm
Ellipse Drawing C++ program - Graphics & Multimedia Lab
Sourcecode:
#include<graphics.h> #include<iostream> using namespace std; int points[20000]; int i=0; int count=0; void ellipsePoints(int x, int y, int value) { int maxx = getmaxx()/2; int maxy = getmaxy()/2; putpixel(maxx+x,maxy+y,value); points[i]=maxx+x; points[i+1]=maxy+y; i+=2; putpixel(maxx-x,maxy+y,value); putpixel(maxx+x,maxy-y,value); putpixel(maxx-x,maxy-y,value); } void midPointEllipse(float a,float b,int value) { double d2; int x=0; int y=b; double dl=b*b-(a*a*b)+(0.25*a*a); putpixel(x,y,value); while((a*a*(y-0.5))>(b*b*(x+1))) { if(dl<0) dl+=b*b*(2*x+3); else { dl+=b*b*(2*x+3)+a*a*(-2*y+2); y--; } x++; ellipsePoints(x,y,value); } d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b; while(y>0) { if(d2<0) { d2+=b*b*(2*x+2)+a*a*(-2*y+3); x++; } else d2+=a*a*(-2*y+3); y--; ellipsePoints(x,y,value); } } int main() { int gd=DETECT, gm,np; FILE *fp; int ax1,ax2; initgraph(&gd, &gm, NULL); fp=fopen("elinput.txt","r"); if(fp==NULL) { printf("ERROR !"); getch(); exit(0); } fscanf(fp,"%d %d",&ax1,&ax2); fclose(fp); midPointEllipse(ax1,ax2,WHITE); count=i;np=0; fp=fopen("eloutput.txt","w"); fprintf(fp,"%s","Plotted points are:-\n"); for(i=0;i<count;i+=2) { fprintf(fp,"(%d %d) ",points[i],points[i+1]); np++; if(np==5){fprintf(fp,"\n");np=0;} } fclose(fp); getch(); closegraph(); return 0; }
Output:
nn@linuxmint ~ $ g++ lab4.cpp -lgraph nn@linuxmint ~ $ ./a.out
"elinput.txt"
75 50
"eloutput.txt"
Plotted points are:- (320 289) (321 289) (322 289) (323 289) (324 289) (325 289) (326 289) (327 289) (328 289) (329 289) (330 288) (331 288) (332 288) (333 288) (334 288) (335 288) (336 288) (337 288) (338 287) (339 287) (340 287) (341 287) (342 287) (343 286) (344 286) (345 286) (346 286) (347 285) (348 285) (349 285) (350 285) (351 284) (352 284) (353 284) (354 283) (355 283) (356 282) (357 282) (358 282) (359 281) (360 281) (361 280) (362 280) (363 279) (364 279) (365 278) (366 278) (367 277) (368 277) (369 276) (370 276) (371 275) (372 274) (373 274) (374 273) (375 272) (376 271) (377 271) (378 270) (379 269) (380 268) (381 267) (382 266) (383 265) (384 264) (385 263) (386 262) (386 261) (387 260) (388 259) (388 258) (389 257) (390 256) (390 255) (391 254) (391 253) (391 252) (392 251) (392 250) (392 249) (393 248) (393 247) (393 246) (393 245) (394 244) (394 243) (394 242) (394 241) (394 240) (394 239)
Midpoint Circle Algorithm - C++ Program - Graphics & Multimedia Lab
Midpoint Circle Algorithm
Circle Drawing - C++ program - Graphics & Multimedia Lab
Sourcecode:
"incircle.txt"
"outcircle.txt"
#include<graphics.h> #include<iostream> using namespace std; int points[10000]; int i=0; int count=0; void circlePoints(int x, int y, int value) { int maxx = getmaxx()/2; int maxy = getmaxy()/2; putpixel(maxx+x,maxy+y,value); points[i]=maxx+x; points[i+1]=maxy+y; i+=2; putpixel(maxx+y,maxy+x,value); putpixel(maxx-x,maxy+y,value); putpixel(maxx+y,maxy-x,value); putpixel(maxx+x,maxy-y,value); putpixel(maxx-y,maxy+x,value); putpixel(maxx-x,maxy-y,value); putpixel(maxx-y,maxy-x,value); } void midPointCircle(int radius,int value) { int x = 0, y = radius; double d = 5.0/4.0-radius; while(y>x) { if(d<0) d += 2.0*x+3.0; else { d += 2.0*(x-y)+5.0; y--; } x++; circlePoints(x,y,value); } } int main() { int gd=DETECT, gm,np; FILE *fp; int radius; initgraph(&gd, &gm, NULL); fp=fopen("incircle.txt","r"); if(fp==NULL) { printf("Error in reading file!"); getch(); exit(0); } fscanf(fp,"%d",&radius); fclose(fp); midPointCircle(radius, WHITE); count=i; np=0; fp=fopen("outcircle.txt","w"); fprintf(fp,"%s","Plotted points are:-\n"); for(i=0;i<count;i+=2) { fprintf(fp,"(%d %d) ",points[i],points[i+1]); np++; if(np==5){fprintf(fp,"\n");np=0;} } fclose(fp); getch(); closegraph(); return 0; }Output:
nn@linuxmint ~ $ g++ lab3.cpp -lgraph nn@linuxmint ~ $ ./a.out
"incircle.txt"
100
"outcircle.txt"
Plotted points are:- (320 339) (321 339) (322 339) (323 339) (324 339) (325 339) (326 339) (327 339) (328 339) (329 338) (330 338) (331 338) (332 338) (333 338) (334 338) (335 338) (336 338) (337 337) (338 337) (339 337) (340 337) (341 337) (342 336) (343 336) (344 336) (345 336) (346 335) (347 335) (348 335) (349 334) (350 334) (351 334) (352 333) (353 333) (354 333) (355 332) (356 332) (357 331) (358 331) (359 331) (360 330) (361 330) (362 329) (363 329) (364 328) (365 328) (366 327) (367 327) (368 326) (369 326) (370 325) (371 324) (372 324) (373 323) (374 323) (375 322) (376 321) (377 320) (378 320) (379 319) (380 318) (381 317) (382 317) (383 316) (384 315) (385 314) (386 313) (387 312) (388 311) (389 310) (390 309)
Subscribe to:
Posts (Atom)