0% found this document useful (0 votes)
41 views25 pages

Abcd

The document contains code for implementing various graphics transformations and line drawing algorithms in C using graphics.h library. It includes code to: 1) Draw the block diagram of a computer using rectangles and lines. 2) Draw the Indian flag using bars of different colors and a white circle. 3) Draw various flowchart and DFD symbols like start/end, input/output, process, decision etc. 4) Implement Bresenham's line drawing algorithm to draw a line between two points. 5) Implement Bresenham's circle drawing algorithm to draw a circle of given radius. 6) Implement DDA line drawing algorithm to draw a line between two points. 7) Implement

Uploaded by

kissmeusually
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views25 pages

Abcd

The document contains code for implementing various graphics transformations and line drawing algorithms in C using graphics.h library. It includes code to: 1) Draw the block diagram of a computer using rectangles and lines. 2) Draw the Indian flag using bars of different colors and a white circle. 3) Draw various flowchart and DFD symbols like start/end, input/output, process, decision etc. 4) Implement Bresenham's line drawing algorithm to draw a line between two points. 5) Implement Bresenham's circle drawing algorithm to draw a circle of given radius. 6) Implement DDA line drawing algorithm to draw a line between two points. 7) Implement

Uploaded by

kissmeusually
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

//Practical-1:Draw the following patterns using standard graphics library

//A).Block diagram of Computer Roll no:


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(50,300,150,250);//input
line(150,275,250,275);
outtextxy(75,275,"INPUT");
rectangle(200,450,450,100);//cpu
rectangle(250,200,400,150);//ALU
outtextxy(310,175,"ALU");
outtextxy(246,272,">");
line(300,200,300,250);
outtextxy(297,244,"v");
line(350,200,350,250);
outtextxy(347,200,"^");
rectangle(250,300,400,250);
line(300,300,300,350);
outtextxy(280,275,"Control unit");
outtextxy(297,344,"v");
line(350,350,350,300);
outtextxy(347,300,"^");
rectangle(250,400,400,350);//storage
line(400,275,500,275);
outtextxy(300,375,"Storage");
rectangle(500,300,600,250);
outtextxy(525,275,"OUTPUT");
outtextxy(496,272,">");
outtextxy(225,75,"BLOCK DIAGRAM OF COMPUTER");
outtextxy(310,465,"CPU");
getch();
}
/*Practical-1:Draw the following pattern using Standard graphics
library
B)Flag of India Roll No:
*/

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,mode,i;
initgraph(&gd,&mode,"c:\\turboc3\\bgi");
setfillstyle(SOLID_FILL,6);
bar(50,50,300,100);
setfillstyle(SOLID_FILL,WHITE);
bar(50,100,300,150);
setfillstyle(SOLID_FILL,GREEN);
bar(50,150,300,200);
setfillstyle(SOLID_FILL,8);
bar(40,20,50,460);//flag pole
setcolor(1);
setfillstyle(SOLID_FILL,WHITE);
for(i=0;i<360;i=i+15)
{
pieslice(175,125,i,i+15,25);
}
getch();
}
Output:-
/*Draw a following pattern using standard graphics library
C. Flow Chart Symbols, DFD Symbols, E-R Diagram Symbols*/
Roll No:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(60,60,180,60);//start end
line(60,90,180,90);//start end
arc(60,75,90,270,15);
arc(180,75,270,90,15);
outtextxy(85,73,"START/END");
line(60,150,180,150);//IO
line(35,210,155,210);//IO
line(60,150,35,210);//IO
line(180,150,155,210);//IO
outtextxy(60,180,"INPUT/OUTPUT");
rectangle(50,270,170,330);
outtextxy(80,300,"PROCESS");
line(300,50,260,90);//DCS
line(260,90,300,130);//DCS
line(300,50,340,90);//DCS
line(340,90,300,130);//DCS
outtextxy(272,30,"DECISION");
line(300,130,300,170);
outtextxy(297,165,"v");
outtextxy(310,140,"YES");
line(340,90,380,90);
outtextxy(378,87,">");
outtextxy(350,70,"NO");
line(280,190,280,250);//fl
line(280,190,270,200);//fl
line(280,190,290,200);//fl
line(310,190,310,250);//fl
line(300,240,310,250);//fl
line(320,240,310,250);//fl
outtextxy(330,210,"FLOW-LINE");
circle(420,140,15);//cn
line(420,100,420,125);//cn
line(420,155,420,180);//cn
outtextxy(450,140,"CONNECTOR");
line(410,110,420,120);//cn
line(430,110,420,120);//cn
line(420,155,420,180);//loop
line(380,270,450,270);//loop
line(380,290,450,290);//loop
line(380,270,370,280);//loop
line(370,280,380,290);//loop
line(450,270,460,280);//braces
line(460,280,450,290);//loop
outtextxy(400,275,"LOOP");
line(415,290,415,320);
line(405,305,415,315);//arrow
line(425,305,415,315);
outtextxy(430,320,"YES");
line(340,280,370,280);
line(350,280,360,270);//arrow
line(350,280,360,290);
outtextxy(320,280,"NO");
rectangle(20,20,550,350);
outtextxy(230,370,"FLOW CHART");
getch();
closegraph();
}
Output:-
/*Practical No: Roll No:
Implement Bresenham’s Line Drawing Algorithm */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void breline(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(WHITE);
setcolor(6);
printf("Enter the value of x1 & y1 :-");u
scanf("%d%d",&x1,&y1);
printf("Enter the value of x2 &y2:-");
scanf("%d%d",&x2,&y2);
breline(x1,y1,x2,y2);
getch();
}
void breline(int x1,int y1,int x2,int y2)
{
float dx,dy,p;
int i,x,y ,xend;
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,10);
while(x<xend)
{
x=x+1;
if(p<0)
{
p=p+(2*dy);
}
else
{
y=y+1;
p=+((2*dy)-(2*dx));
}
putpixel(x,y,10);
}
}

Output:-
Practical 3. Implement Bresenham’s Circle Drawing Algorithm
Date:26/09/2023 ROLL NO:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,r,i,p,xc,yc;
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\BGI");
printf("Enter the CO-ORDINATES");
scanf("%d%d",&xc,&yc);
printf("\n Enter the value of Radius:");
scanf("%d",&r);
print_origin();
xc=xc+320;
yc=240-yc;
cir(xc,yc,r);
return(0);
}
cir(xc,yc,r)
{
x=0;
y=r;
plotpoints(xc,yc,x,y);
p=3-2*r;
while(x<y)
{
if(p<0)
{
x=x+1;
y=y;
}
else
{
x=x+1;
y=y-1;
}
if(p<0)
{
p=p+4*x+6;
}
else
p=p+4*(x-y)+10;
plotpoints(xc,yc,x,y);
}
getch();
return(0);
}
plotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
delay(200);
putpixel(xc-x,yc+y,15);
delay(200);
putpixel(xc+x,yc-y,15);
delay(200);
putpixel(xc-x,yc-y,15);
delay(200);
putpixel(xc+y,yc+x,15);
delay(200);
putpixel(xc-y,yc+x,15);
delay(200);
putpixel(xc+y,yc-x,15);
delay(200);
putpixel(xc-y,yc-x,15);
delay(200);
return(0);
}
/*CODE FOR DRAWING ORIGIN*/
print_origin()
{
line(1,240,640,240);
line(320,1,320,640);
outtextxy(320,242,"(0,0)");
return(0);
}
OUTPUT
Write program to Impliment DDA line drawing algorithm
Roll No:*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2,i=0;
float x,y,xincr,yincr,length;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
printf("\n Enter co-ordinate x1 & y1:");
scanf("%d%d",&x1,&y1);
printf("\n Enter co-ordinate x2 & y2:");
scanf("%d%d",&x2,&y2);
length=x2-x1;
if((y2-y1)>length)
length=y2-y1;
xincr=(x2-x1)/length;
yincr=(y2-y1)/length;
x=x1+0.5;
y=y1+0.5;
for(i=1;i<=length;i++)
{
putpixel((int)x,(int)y,WHITE);
x=x+xincr;
y=y+yincr;
getch();
}
getch();
}
Output:-
Practical-5: Implement Translation transformation on polygon
Date:26/09/2023 Roll No:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x[10],y[10],n,i;
int x1[10],y1[10];
int t1,t2;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("\t Original object \n");
printf("\n Enter no of points: ");
scanf("%d",&n);
printf("\t Enter the %d points:", n);
for(i=0;i<n;i++)
{
printf("\n Enter the co-ordinates of %d points",i+1);
printf("\n x=");
scanf("%d",&x[i]);
printf("y=");
scanf("%d",&y[i]);

x1[i]=x[i];
y1[i]=y[i];
}
for(i=0;i<n-1;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[n-1],y[n-1],x[0],y[0]);
getch();

//cleardevice();
printf("\n Enter the translation factor : ");
scanf("%d%d",&t1,&t2);
for(i=0;i<n;i++)
{
x1[i]=x1[i]+t1;
y1[i]=y1[i]+t2;
}
for(i=0;i<n-1;i++)
{
line(x1[i],y1[i],x1[i+1],y1[i+1]);
}
line(x1[n-1],y1[n-1],x1[0],y1[0]);
getch();
closegraph();
}

Output:-
Practical-6: Implement Scaling transformation on polygon
Date:26/09//2023 Roll No:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x[10],y[10],n,i;
int x1[10],y1[10];
int s1,s2;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("\t Original object \n");
printf("\n Enter no of points: ");
scanf("%d",&n);
printf("\t Enter the %d points:", n);
for(i=0;i<n;i++)
{
printf("\n Enter the co-ordinates of %d points",i+1);
printf("\n x=");
scanf("%d",&x[i]);
printf("y=");
scanf("%d",&y[i]);

x1[i]=x[i];
y1[i]=y[i];
}
for(i=0;i<n-1;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[n-1],y[n-1],x[0],y[0]);
getch();

//cleardevice();
printf("\n Enter the scaling factor : ");
scanf("%d%d",&s1,&s2);
for(i=0;i<n;i++)
{
x1[i]=x1[i]*s1;
y1[i]=y1[i]*s2;
}
for(i=0;i<n-1;i++)
{
line(x1[i],y1[i],x1[i+1],y1[i+1]);
}
line(x1[n-1],y1[n-1],x1[0],y1[0]);
getch();
closegraph();
}

Output:-
Practical-7:Implement Rotation Transformation on polygons
Date:03/10/2023 Roll No:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
int n,x[10],y[10],i;
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the number of vertices :-");
scanf("%d",&n);
printf("Enter %d co-ordinates :", n*2);
for(i=0;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
print_origin();
draw();
Rotation();
draw();
getch();
}
draw()
{
for(i=0;i<n-1;i++)
{
line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]);
}
line(320+x[0],240-y[0],320+x[n-1],240-y[n-1]);
return;
}
Rotation()
{
float ang1;
printf("Enter angle :- ");
scanf("%f",&ang1);
ang1=ang1*3.14/180;
print_origin();
for(i=0;i<n;i++)
{
x[i]=(x[i]*cos(ang1))+(y[i]*sin(ang1));
y[i]=(y[i]*sin(ang1))+(y[i]*cos(ang1));
}
return;
}
print_origin()
{
setcolor(WHITE);
line(1,240,639,240);
line(320,1,320,639);
outtextxy(320,240,"O(0,0)");
outtextxy(317,1,"^");
outtextxy(1,237,"<");
outtextxy(632,237,">");
outtextxy(317,472,"v");
return(0);
}

Output:-
Practical-8:Implement Cohen-Sutherland line clipping algorithm
Roll No:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
typedef unsigned int outcode;
enum {TOP=0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8};

void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax)
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept=0,done=0;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1=calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do
{
if(!(code0|code1))
{
{accept=1;done=1;}
}
else
if(code0&code1)
done=1;
else
{
float x,y;
codeout=code0?code0:code1;
if(codeout &TOP)
{
x=x0+(x1-x0)*(ywmax-y0)/(y1-y0);
y=ywmax;
}
else
if(codeout &BOTTOM)
{
x=x0+(x1-x0)*(ywmin-y0)/(y1-y0);
y=ywmin;
}
else
if(codeout &RIGHT)
{
y=y0+(y1-y0)*(ywmax-x0)/(x1-x0);
x=xwmax;
}
else
if(codeout &LEFT)
{
y=y0+(y1-y0)*(ywmin-x0)/(x1-x0);
x=xwmin;
}
if(codeout==code0)
{
x0=x;
y0=y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1=x;
y1=y;
code1=calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
}
while(done==0);
// clrscr();
if(accept)
{
cleardevice();
getch();
line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
}
getch();
}
int calcode(x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code=0;
if(y>ywmax)
code|=TOP;
else if(y<ywmin)
code|=BOTTOM;
else if(x>xwmax)
code|=RIGHT;
else if(x<xwmin)
code|=LEFT;
return(code);
}
void main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(WHITE);
setcolor(1);
printf("Enter the co-ordinates of line:");
printf("\n x1,y1 :");
scanf("%f%f",&x1,&y1);
printf("\n x2,y2 :");
scanf("%f%f",&x2,&y2);
printf("Enter the co-ordinates of window:");
printf("\n xwmin,ywmin :");
scanf("%f%f",&xwmin,&ywmin);
printf("\n xwmax,ywmax :");
scanf("%f%f",&xwmax,&ywmax);
cleardevice();
setcolor(1);
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax);
getch();
}

Output:-
Before clipping:-

After clipping:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy