- #include<iostream.h>
- #include<conio.h>
- #include<graphics.h>
- #include<stdio.h>
- #include<dos.h>
- void fill_right(int x,int y); void fill_left(int x,int y); void main()
- {
- int x,y,a[10][10]; int gd,gm,n,i; detectgraph(&gd,&gm);
- initgraph(&gd,&gm,"C:\\TC\\BGI"); cout<<"Enter no of edges of polygon:"; cin>>n;
- cout<<"\n Enter the coordinates of polygon"; for(i=0;i<n;i++)
- {
- cin>>a[i][0]>>a[i][1];
- }
- a[n][0]=a[0][0];
- a[n][1]=a[0][1];
- cout<<"\n Enter the seed point"; cin>>x>>y;
- cleardevice(); setcolor(WHITE); for(i=0;i<n;i++)
- {
- line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
- }
- fill_right(x,y); fill_left(x-1,y); getch();
- }
- void fill_right(int x,int y) //Right fill
- {
- if((getpixel(x,y) != WHITE) && (getpixel(x,y) != RED))
- {
- delay(1); putpixel(x,y,RED); fill_right(++x,y); x=x-1; fill_right(x,y+1); fill_right(x,y-1);
- }
- }
- void fill_left(int x,int y) //left fill
- {
- if((getpixel(x,y) != WHITE) && (getpixel(x,y) != RED))
- {
- putpixel(x,y,RED); delay(1); fill_left(--x,y); x=x+1; fill_left(x,y-1); fill_left(x,y+1);
- }
- }
Showing posts with label Computer Graphics. Show all posts
Showing posts with label Computer Graphics. Show all posts
Thursday, June 21, 2018
Implementation of polygon filling algorithm
Implementation of curve drawing using Bezier algorithm
- #include<dos.h>
- #include<stdio.h>
- #include<iostream.h>
- #include<graphics.h>
- #include<conio.h>
- int a[150],b[150],m,n=0,j,d; float bx[5000],by[5000],u; union REGS i,o;
- initmouse()
- {
- i.x.ax=0; int86(0x33,&i,&o); return(o.x.ax);
- }
- void showmouseptr()
- {
- i.x.ax=1; int86(0x33,&i,&o);
- }
- void getmousepos(int *button,int *x,int *y)
- {
- i.x.ax=3; int86(0x33,&i,&o);
- *button=o.x.bx;
- *x=o.x.cx;
- *y=o.x.dx; i.h.ah=0x02; i.h.dh=0;
- i.h.dl=0; int86(0x10,&i,&o); cout<<"X: "<<*x<<" Y: "<<*y; i.h.ah=0x02; int86(0x10,&i,&o);
- }
- void hidemouseptr()
- {
- i.x.ax=2; int86(0x33,&i,&o);
- }
- int compco()
- {
- int k,coeff=1;
- for(k=d+1;k<=n;k++) coeff=coeff*k; for(k=2;k<=n-d;k++) coeff=coeff/k; return coeff;
- }
- float blenval()
- {
- int k;
- float blend; blend=compco();
- for(k=1;k<=d;k++) blend=blend*u; for(k=1;k<=n-d;k++) blend=blend*(1.0-u); return blend;
- }
- void bezier()
- {
- float blend1;
- bx[j]=0;by[j]=0; for(d=0;d<=n;d++)
- {
- blend1=blenval(); bx[j]+=a[d]*blend1; by[j]+=b[d]*blend1;
- }
- }
- void curve()
- {
- for(j=0;j<m;j++) putpixel(bx[j],by[j],GREEN);
- }
- void drawi()
- {
- for(j=0;j<=m;j++)
- {
- u=(float)j/m; bezier();
- }
- curve();
- }
- void main()
- {
- int gd=DETECT,gm; int button,tx,ty; clrscr(); m=4990;
- flushall(); initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice();
- showmouseptr(); while(!kbhit())
- {
- getmousepos(&button,&tx,&ty); if((button & 1) == 1)
- {
- hidemouseptr(); setcolor(WHITE); a[n]=tx;
- b[n]=ty; putpixel(tx,ty,WHITE); circle(tx,ty,2);
- n++;
- delay(50); showmouseptr();
- }
- }
- n--;
- drawi();
- getch();
- }
Output:
Implementation of ellipse drawing algorithm
- #include<iostream.h>
- #include<conio.h>
- #include<graphics.h>
- #include<dos.h>
- void main()
- {
- clrscr();
- float rx=0,ry=0,x=0,y=0,dx=0,dy=0,d1=0,d2=0;
- float rxSqr=0,rySqr=0,rx2Sqr=0,ry2Sqr=0;
- int x1=0,y1=0; int gd,gm;
- detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi");
- //textbackground(15);
- //textcolor(8);
- //setbkcolor(15);
- //setcolor(8);
- cout<<"Enter pixels for centre of an ellipse (x,y): "; cin>>x1>>y1;
- cout<<"Enter rx radius: "; cin>>rx;
- cout<<"Enter ry radius: "; cin>>ry;
- line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy());
- x=0;
- y=ry;
- rxSqr=rx*rx; rx2Sqr=2*rxSqr; rySqr=ry*ry; ry2Sqr=2*rySqr; dx=ry2Sqr*x; dy=rx2Sqr*y;
- d1=rySqr-rxSqr*ry+(.25*rxSqr);
- do
- {
- putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1+y),8);
- putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1+y),8);
- putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1-y),8);
- putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1-y),8);
- if(d1<0)
- {
- x+=1;
- y=y; dx+=ry2Sqr; d1+=dx+rySqr;
- }
- else
- {
- x+=1;
- y-=1;
- dx+=ry2Sqr; dy-=rx2Sqr;
- d1+=dx-dy+rySqr;
- }
- delay(30);
- }while(dx<dy);
- d2=rySqr*((x+(.5))*(x+(.5)))+rxSqr*((y-1)*(y-1))-rxSqr*rySqr; do
- {
- putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1+y),8);
- putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1+y),8);
- putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1-y),8);
- putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1-y),8);
- if(d2>0)
- {
- x=x; y-=1;
- dy-=rx2Sqr; d2-=dy+rxSqr;
- }
- else
- {
- x+=1; y-=1;
- dy-=rx2Sqr; dx+=ry2Sqr; d2+=dx-dy+rxSqr;
- }
- delay(10);
- }while(y>0);
- getch();
- }
Output:
Implementation of circle drawing algorithm
- #include<iostream.h>
- #include<conio.h>
- #include<graphics.h>
- #include<dos.h>
- void main()
- {
- int gm,gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi");
- int x1,y1;
- int x=0,y=0,r=0; float d;
- //textbackground(15);
- //textcolor(8);
- //setbkcolor(15);
- //setcolor(8);
- cout<<"Enter pixels for centre of a circle (x,y): "; cin>>x1>>y1;
- cout<<"\nEnter radius of circle: "; cin>>r;
- x=0;
- y=r; d=1.25-r;
- line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy());
- do
- {
- putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1+y),8);
- putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1+y),8);
- putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1-y),8);
- putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1-y),8);
- putpixel(getmaxx()/2+(x1+y),getmaxy()/2-(y1+x),8);
- putpixel(getmaxx()/2+(x1+y),getmaxy()/2-(y1-x),8);
- putpixel(getmaxx()/2+(x1-y),getmaxy()/2-(y1-x),8);
- putpixel(getmaxx()/2+(x1-y),getmaxy()/2-(y1+x),8);
- if(d<0)
- {
- x+=1;
- y=y;
- d+=2*x+1;}
- else
- {
- x+=1;
- y-=1;
- d+=2*(x-y)+1;
- }
- delay(50);
- }while(x<y);
- getch();
- }
Bresenham's algorithm
- #include<iostream.h>
- #include<conio.h>
- #include<graphics.h>
- #include<math.h>
- #include<dos.h>
- void main()
- {
- clrscr();
- int gm,gd;
- int x1,y1,x2,y2,i;
- float x,y,dx,dy,len,e;
- detectgraph(&gd,&gm);
- initgraph(&gd,&gm,"c:\\tc\\bgi");
- //textbackground(15);
- //textcolor(8);
- //setbkcolor(15);
- //setcolor(8);
- cout<<"Enter starting pixels for point (x1,y1): "; cin>>x1>>y1;
- cout<<"\nEnter ending pixels for point (x2,y2): "; cin>>x2>>y2;
- line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy());dx=abs(x2-x1);
- dy=abs(y2-y1);
- x=x1;
- y=y1;
- e=2*dy-dx;
- for(i=1;i<=dx;i++)
- {
- putpixel(getmaxx()/2+x,getmaxy()/2-y,8); while(e>=0)
- {
- y++;
- e-=2*dx;
- delay(20)
- }
- x++;
- e+=2*dy;
- }
- getch();
- }
DDA algorithm (Implementation of line drawing algorithm)
- #include<iostream.h>
- #include<conio.h>
- #include<graphics.h>
- #include<math.h>
- #include<dos.h>
- void main()
- {
- clrscr();
- int x1,y1,x2,y2,i=1; float x,y,dx,dy,len;
- detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi");
- textbackground(15); textcolor(15); setbkcolor(15); setcolor(8);
- cout<<"Enter starting pixels for point (x1,y1):"; cin>>x1>>y1;
- cout<<"\nEnter ending pixels for point (x2,y2):"; cin>>x2>>y2; line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy());
- dx=abs(x2-x1);
- dy=abs(y2-y1);
- if(dy>dx)len=dy;
- else len=dx;
- dx=((x2-x1)/len);
- dy=((y2-y1)/len);
- x=x1+0.5*dx; y=y1+0.5*dy; while(i<=len)
- {
- putpixel(getmaxx()/2+x,getmaxy()/2-y,8);
- }
- x+=dx; y+=dy; i++;
- delay(20);
- }
- getch();
Introduction to graphics libraries in c++
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
//setbkcolor(15);
//setcolor(1); setfillstyle(HATCH_FILL,5);
circle(100,100,50);
outtextxy(75,170,"circle");
line(100,250,540,250);
outtextxy(300,260,"line");
ellipse(500,100,0,360,100,50);
outtextxy(480,170,"ellipse");
sector(150,400,30,300,100,50);
outtextxy(120,460,"sector");
int
poly[]={350,450,350,410,430,400,350,350,300,430,350,450};
fillpoly(6,poly);
outtextxy(340,460,"polygon");
rectangle(200,50,350,150);
outtextxy(530,460,"arc");
pieslice(120,320,45,120,40);
outtextxy(110,325,"pieslice");
bar(320,280,350,315);
outtextxy(320,325,"bar");
bar3d(480,280,520,320,10,15);
outtextxy(490,325,"bar3d");
getch();
closegraph();
}
Subscribe to:
Posts (Atom)



