Category Archives: Computer Graphics

Computer Graphics : 2D Square Generation using putpixel function in C

/*====2D square generation using putpixel function====/*

#include<graphics.h>
#include<conio.h>
#include<stdio.h>

main()
{
int i,x,y,a,gd = DETECT, gm;
printf("Enter a value for the side of the square :\n");
scanf("%d",&a);
printf("Enter initial points :\n");
printf("Enter a value for x :\n");
scanf("%d",&x);
printf("Enter a value for y :\n");
scanf("%d",&y);
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
for(i=0;i<a;i++)
{
putpixel(x+i,y,15);
putpixel(x,y-i,15);
delay(10);
}
for(i=0;i<=a;i++)
{
putpixel(x+i,y-a,15);
putpixel(x+a,y-i,15);
delay(10);
}
getch();
closegraph();
return 0;
}

Output :

Untitled1

Untitled

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?

 

Computer Graphics : Midpoint Circle Drawing Algorithm

/*==== Midpoint Circle Drawing Algorithm ====/*

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int i,r,x,y,xc,yc;
float d;
clrscr();
printf("\n====Midpoint Circle Drawing Algorithm====\n\n");
printf("Enter Radius : ");
scanf("%d",&r);
printf("Enter centre of circle : \n");
printf("Enter x-coordinate : \n");
scanf("%d",&xc);
printf("Enter y-coordinate :\n");
scanf("%d",&yc);
initgraph(&gd,&gm,"C:\\TC\\BGI");
d=1.25-r;
x=0;
y=r;
do
{
if(d<0)
{
x=x+1;
d=d+2*x+1;
}
else
{
x=x+1;
y=y-1;
d=d+(2*x)-(2*y)+10;
}
putpixel(xc+x,yc+y,7);
putpixel(xc-y,yc-x,8);
putpixel(xc+y,yc-x,9);
putpixel(xc-y,yc+x,10);
putpixel(xc+y,yc+x,11);
putpixel(xc-x,yc-y,12);
putpixel(xc+x,yc-y,13);
putpixel(xc-x,yc+y,14);
delay(10);
}
while(x<y);
getch();
}

Output :

Screenshot (2) Screenshot (3)

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?

Computer Graphics : Bresenham’s Circle Drawing Algorithm

/*====Bresenham's Circle Drawing Algorithm====*/

# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void main()
{
int gd=DETECT,gm,r,x,y,p,xc=350,yc=250;
clrscr();
printf("\n====Bresenham's Circle Drawing Algorithm====\n\n");
printf("Enter the radius :");
scanf("%d",&r);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if (p<0)
{
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
delay(100);
}
getch();
closegraph();
}

Output :

Untitled Untitled1

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?


 

Computer Graphics : Bezier Curve Construction

/*====Bezier Curve====*/

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void bezier(int x[4],int y[4])
{
int gd=DETECT,gm,i;
double u;
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(15);
outtextxy(400,400,"-> Control Points");
outtextxy(400,430,"-> Polygon");
outtextxy(400,460,"-> Bezier Curve");
setcolor(RED);
setfillstyle(1,RED);
circle(390,400,5);
floodfill(390,400,RED);
setcolor(YELLOW);
setfillstyle(1,YELLOW);
circle(390,430,5);
floodfill(390,430,YELLOW);
setcolor(WHITE);
setfillstyle(1,WHITE);
circle(390,460,5);
floodfill(390,460,WHITE);
delay(10);
for(u=0.0;u<1.0;u+=0.0002)
{
double xu= pow(1-u,3)*x[0]+3*u*pow(1-u,2)*x[1]+3*pow(u,2)*(1-u)*x[2]+pow(u,3)*x[3];
double yu= pow(1-u,3)*y[0]+3*u*pow(1-u,2)*y[1]+3*pow(u,2)*(1-u)*y[2]+pow(u,3)*y[3];
putpixel(xu,yu,WHITE);
delay(1);
}
for(i=0;i<4;i++)
{
setcolor(RED);
setfillstyle(1,RED);
circle(x[i],y[i],4);
floodfill(x[i],y[i],RED);
delay(1);
}
for(i=0;i<3;i++)
{
setcolor(YELLOW);
line(x[i],y[i],x[i+1],y[i+1]);
}
}
void main()
{
int x[4],y[4],i;
clrscr();
printf("\n\n====Bezier Curve====\n\n");
printf("Enter the x and y coordinates for the four control points :\n");
for(i=0;i<4;i++)
{
printf("x%d and y%d are :\n",i+1,i+1);
scanf("%d %d",&x[i],&y[i]);
}
bezier(x,y);
getch();
closegraph();
}

Output :

Untitled1Untitled2

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?


 

Computer Graphics : Bresenham’s Line Drawing Algorithm

/*====Bresenham's Line Drawing Algorithm====*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int m,x,y,x1,y1,x2,y2,i,dx,dy,P,a;
int gd= DETECT,gm;
clrscr();
printf("\n ===Bresenham's Line Drawing Algorithm===\n\n");
printf(" Enter the co-ordinates of the first point :\n\tx1 : ");
scanf(" %d",&x1);
printf("\n y1 : ");
scanf(" %d",&y1);
printf(" Enter the co-ordinates of the second point :\n\tx2 : ");
scanf(" %d",&x2);
printf("\n y2 : ");
scanf(" %d",&y2);
dx=x2-x1;
dy=y2-y1;
m=(dy/dx);
dy=abs(dy);
dx=abs(dx);
initgraph(&gd,&gm,"C:\\TC\\BGI");
putpixel(x1,y1,15);
if(m<1)
{
P=(2*dy)-dx;
for(a=0;a<dx;a++)
{
if(P<0)
{
if(x1<x2)
x1++;
else
x1--;
P=P+(2*dy);
}
else
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
P=P+(2*(dy-dx));
}
putpixel(x1,y1,15);
delay(20);
}
}
else
{
P=(2*dx)-dy;
for(a=0;a<dy;a++)
{
if(P<0)
{
if(y1<y2)
y1++;
else
y1--;
P=P+(2*dx);
}
else
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
P=P+(2*(dx-dy));
}
putpixel(x1,y1,15);
delay(10);
}
}
getch();
closegraph();
}

Output :
Untitled Untitled1

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?


 

Computer Graphics : DDA Line Drawing Algorithm

/*====Digital Differential Analyzer Line Drawing Algorithm====*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main ()
{
int gd=DETECT,gm,x1,y1,x2,y2,dy,dx,temp;
float xinc,yinc;
printf("\n===DDA Line Drawing Algorithm===\n\n");
printf("Enter the co-ordinates of the first point :\nx1 : ");
scanf("%d",&x1);
printf("\ny1 : ");
scanf("%d",&y1);
printf("Enter the co-ordinates of the second point :\nx2 : ");
scanf("%d",&x2);
printf("\ny2 : ");
scanf("%d",&y2);
dy=y2-y1;
dx=x2-x1;
if(abs(dx)>=abs(dy))
temp=abs(dx);
else
temp=abs(dy);
xinc=dx/temp;
yinc=dy/temp;
initgraph(&gd,&gm,"C://TC//BGI");
while(x1!=x2 && y1!=y2)
{
putpixel(x1,y1,15);
x1+=xinc;
y1+=yinc;
delay(20);
}
getch();
closegraph();
}

Output :

Untitled

Untitled1

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?


 

Operating System & Computer Graphics : FCFS (First Come First Serve) Disk Scheduling

/*====FCFS Disk Scheduling====*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void denotion();
void main ()
{
int gd=DETECT,gm,m,a[30],i,n,j=100;
clrscr();
printf("== FCFS Disk Scheduling ==\n\n");
printf("Total no of cylinders in the disk : 0 to 100\n");
printf("Enter the total no. of pending requests :");
scanf("%d",&m);
printf("Enter the pending values : ");
for(i=1;i<=m;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the value where the head initially exists :");
scanf("%d",&n);
a[0]=n;
initgraph(&gd,&gm,"C://TC//BGI");
denotion();
for(i=0;i<=500;i+=5)
{
setcolor(WHITE);
rectangle(i,85,5+i,95);
if(i==0)
{
outtextxy(i,70,"0");
}
if(i==50)
{
outtextxy(i,70,"10");
}
if(i==100)
{
outtextxy(i,70,"20");
}
if(i==150)
{
outtextxy(i,70,"30");
}
if(i==200)
{
outtextxy(i,70,"40");
}
if(i==250)
{
outtextxy(i,70,"50");
}
if(i==300)
{
outtextxy(i,70,"60");
}
if(i==350)
{
outtextxy(i,70,"70");
}
if(i==400)
{
outtextxy(i,70,"80");
}
if(i==450)
{
outtextxy(i,70,"90");
}
if(i==500)
{
outtextxy(i,70,"100");
}
}
setcolor(YELLOW);
setfillstyle(1,YELLOW);
rectangle(a[0]*5,85,(a[0]*5)+5,95);
floodfill((a[0]*5)+3,90,YELLOW);
delay(10);
for(i=0;i<m;i++)
{
setcolor(WHITE);
line(a[i]*5,j,a[i+1]*5,50+j);
setcolor(8);
setfillstyle(1,8);
circle(a[i+1]*5,50+j,5);
floodfill(a[i+1]*5,50+j,8);
delay(1);
j=50+j;
if(i>=1)
{
setcolor(RED);
setfillstyle(1,RED);
rectangle(a[i]*5,85,(a[i]*5)+5,95);
floodfill((a[i]*5)+3,90,RED);
delay(100);
}
}
setcolor(RED);
setfillstyle(1,RED);
rectangle(a[m]*5,85,(a[m]*5)+5,95);
floodfill((a[m]*5)+3,90,RED);
getch();
closegraph();
}
void denotion()
{
outtextxy(200,20,"== FCFS Disk Scheduling ==");
setcolor(YELLOW);
setfillstyle(1,YELLOW);
rectangle(520,380,525,390);
floodfill(523,385,YELLOW);
outtextxy(530,383,"-> Initial");
outtextxy(544,395,"position");
delay(1);
setcolor(RED);
setfillstyle(1,RED);
rectangle(520,420,525,430);
floodfill(523,425,RED);
outtextxy(530,423,"-> Request");
outtextxy(536,435,"cylinders");
delay(1);
}

Output :

Untitled

That’s All. P)
Happy C-ing!

-Aayush Shrivastava


Are you satisfied?


 

Operating System : FIFO Page Replacement Algorithm

/*====First In First Out (Page Replacement)====*/

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main ()
{
int b[50],c[10],i,m,n,pgf=0,j,k,flag,p,l;
clrscr();
for(i=0;i<10;i++)
{
c[i]=-1;
}
printf("====Page Replacement Algorithm====\n\n");
printf("____First In First Out____\n\n");
printf("Enter the no of page frames :\n");
scanf("%d",&m);
printf("Enter the no of values you want to enter :\n");
scanf("%d",&n);
printf("Enter the values :\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
//Solution head
printf("Solution :\n");
for(i=0;i<m;i++)
{
printf("%d\t",i);
}
printf("| PageFault\n");
for(i=0;i<m;i++)
{
printf("-");
for(j=0;j<7;j++)
{
printf("-");
}
}
printf("|------------\n");
//Main solution
for(j=0;j<n;)
{
flag=0;
for(k=0;k<m;k++)
{
if(b[j]==c[k])
{
for(i=j;i<n;i++)
{
b[i]=b[i+1];
}
for(i=0;i<m;i++)
{
printf("%d\t",c[i]);
}
printf("|\n");
delay(500);
flag=1;
n--;
}
}
if(flag==0)
{
p=j%m;
c[p]=b[j];
for(i=0;i<m;i++)
{
printf("%d\t",c[i]);
}
printf("| *\n");
delay(500);
pgf++;
j++;
}
}
//Final Outcome
printf("Total no. of Page Faults : %d",pgf);
getch();
}

Output :

Untitled

That’s All. P)
Happy C-ing!

-Aayush Shrivastava


Are you satisfied?


 

Computer Graphics : Different E-num Fill Styles

/*===E-num Fill Styles===*/

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main ()
{
int i,gd=DETECT,gm,x,y;
initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);
x=300;
y=300;
for(i=0;i<12;i++)
{
setcolor(i);
setfillstyle(i,i);
circle(x,y,100);
floodfill(x,y,i);
delay(300);
}
getch();
closegraph();
}

Output : (fill style changes its design & colors with time according to given delay in the program)

Untitled1 Untitled2

That’s All. P)
Happy C-ing!

-Aayush Shrivastava


Are you satisfied?


 

Operating System & Computer Graphics : Circular-Scan Disk Scheduling

/*===C-Scan Scheduling===*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void denotion();
void main ()
{
int gd=DETECT,gm,m,a[30],i,n,j=100,o;
clrscr();
printf("==Circular-SCAN Disk Scheduling==\n\n");
printf("Total no of cylinders in the disk : 0 to 100\n");
printf("Enter the total no. of pending requests : ");
scanf("%d",&m);
printf("Enter the pending values : ");
for(i=1;i<=m;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the value where the head initially exists : ");
scanf("%d",&n);
a[0]=n;
for(i=0;i<m;i++)
{
for(j=i+1;j<=m;j++)
{
if(a[j]<a[i])
{
o=a[i];
a[i]=a[j];
a[j]=o;
}
}
}
initgraph(&gd,&gm,"C://TC//BGI");
denotion();
for(i=0;i<=500;i+=5)
{
setcolor(WHITE);
rectangle(i,85,5+i,95);
if(i==0)
{
outtextxy(i,70,"0");
}
if(i==50)
{
outtextxy(i,70,"10");
}
if(i==100)
{
outtextxy(i,70,"20");
}
if(i==150)
{
outtextxy(i,70,"30");
}
if(i==200)
{
outtextxy(i,70,"40");
}
if(i==250)
{
outtextxy(i,70,"50");
}
if(i==300)
{
outtextxy(i,70,"60");
}
if(i==350)
{
outtextxy(i,70,"70");
}
if(i==400)
{
outtextxy(i,70,"80");
}
if(i==450)
{
outtextxy(i,70,"90");
}
if(i==500)
{
outtextxy(i,70,"100");
}
}
j=100;
for(i=0;i<m;i++)
{
if(a[i]==n)
{
setcolor(WHITE);
line(a[i]*5,j,a[i+1]*5,20+j);
setcolor(8);
setfillstyle(1,8);
circle(a[i+1]*5,20+j,5);
floodfill(a[i+1]*5,20+j,8);
delay(1);
j+=20;
setcolor(YELLOW);
setfillstyle(1,YELLOW);
rectangle(a[i]*5,85,(a[i]*5)+5,95);
floodfill((a[i]*5)+3,90,YELLOW);
delay(10);
}
if(a[i]>n)
{
setcolor(WHITE);
line(a[i]*5,j,a[i+1]*5,20+j);
setcolor(8);
setfillstyle(1,8);
circle(a[i+1]*5,20+j,5);
floodfill(a[i+1]*5,20+j,8);
delay(1);
j+=20;
setcolor(RED);
setfillstyle(1,RED);
rectangle(a[i]*5,85,(a[i]*5)+5,95);
floodfill((a[i]*5)+3,90,RED);
delay(10);
}
}
setcolor(RED);
setfillstyle(1,RED);
rectangle(a[m]*5,85,(a[m]*5)+5,95);
floodfill((a[m]*5)+3,90,RED);
delay(10);
if(a[0]!=n)
{
setcolor(WHITE);
line(a[m]*5,j,500,j+20);
line(500,j+20,0,j+50);
line(0,j+50,a[0]*5,j+70);
setcolor(8);
setfillstyle(1,8);
circle(a[0]*5,j+70,5);
floodfill(a[0]*5,j+70,8);
delay(10);
j+=70;
for(i=0;a[i+1]<n;i++)
{
setcolor(WHITE);
line(a[i]*5,j,a[i+1]*5,20+j);
setcolor(8);
setfillstyle(1,8);
circle(a[i+1]*5,20+j,5);
floodfill(a[i+1]*5,20+j,8);
delay(1);
j+=20;
setcolor(RED);
setfillstyle(1,RED);
rectangle(a[i]*5,85,(a[i]*5)+5,95);
floodfill((a[i]*5)+3,90,RED);
delay(10);
}
setcolor(RED);
setfillstyle(1,RED);
rectangle(a[i]*5,85,(a[i]*5)+5,95);
floodfill((a[i]*5)+3,90,RED);
delay(10);
}
getch();
closegraph();
}
void denotion()
{
outtextxy(200,20,"== C-Scan Disk Scheduling ==");
setcolor(YELLOW);
setfillstyle(1,YELLOW);
rectangle(520,380,525,390);
floodfill(523,385,YELLOW);
outtextxy(530,383,"-> Initial");
outtextxy(544,395,"position");
delay(1);
setcolor(RED);
setfillstyle(1,RED);
rectangle(520,420,525,430);
floodfill(523,425,RED);
outtextxy(530,423,"-> Request");
outtextxy(536,435,"cylinders");
delay(1);
}

Output :

Untitled

That’s All. P)
Happy C-ing!

-Aayush Shrivastava


Are you satisfied?