Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<alloc.h>
#include<math.h>
int x,y,button,color,x1,x2,y1,y2,prevx2,prevy2;
union REGS i,o,r,out,in,dc;
//function to check errors
void init()
{
int gd=DETECT,gm,ec;
initgraph(&gd,&gm,"c:\\tc\\bgi");
ec=graphresult();
if(ec!=grOk)
{
printf("graph Error: %s",grapherrormsg(ec));
getch();
exit(1);
}
clrscr();
}
void f(int xx,int yy,int BC,int NC) {
if(getpixel(xx,yy) == BC || getpixel(xx,yy) == NC)
return;
putpixel(xx,yy,NC);
f(xx,yy+1,BC,NC);
f(xx,yy-1,BC,NC);
f(xx+1,yy,BC,NC);
f(xx-1,yy,BC,NC);
}
//function to detect color
int detectcolor();
//function freehand
void freehand();
//function to create colors bar and fill backgroud
void createsides() ;
//function to get postion from mouse
void getmousepos();
// function to create colors
void createcolors();
//function to create shapes bar and fill backgroud
void createshapesbar();
//function to create shapes
void createshapes();
//function to create cursor mouse
void mousecursor();
//function to create line
void lin1();
//function to create sequre
void sequre();
//function to create circle
void cr();
//function to create mkhms
void msds();
//function to create motlat
void motlat();
//functuion to fill background
void fillbg(int x,int y,int BC,int NC);
void bg();
void show_mouse();
void hide_mouse();
void get_mouse_pos1(int *x,int *y,int *button);
void get_mouse_pos(int *x,int *y,int *button);
//function to erase all
void erase();
int getshape();
void switchershape(int nshape);
void main()
{
int zz=99;
init();
outtextxy(300,5,"Paint ");
outtextxy(500,5,"By: Kamel & Saleh");
while(!kbhit())
{
createsides();
createcolors();
createshapesbar();
createshapes();
mousecursor();
do{
// printf("Press enter and choose a shape");
detectcolor();
}
while(color>15 && color<0);
// setcolor(2);
// freehand(); //working freehand
// lin1(); // working line
// sequre(); //working
// cr(); //working
// msds(); //working
// motlat(); //working
// bg();
//circle(300,300,10);
//erase();
zz=getshape();
gotoxy(10,10);
printf("the shape is %d",zz);
switchershape(zz);
getch();
}
closegraph();
}
void createsides()
{
setfillstyle (1,8); //color backgroud for frames
bar (0,419,640,480); //down bar for colors
bar (0,0,60,418); // left bar for shapes
// setfillstyle (1,15); //color backgroud for board
// bar(61,20,639,419);
// rectangle(61,20,639,419);
}
void createcolors()
{
//color slide color from 1 to 16
for(int colorr=0;colorr<16;colorr++)
{
setfillstyle(1,colorr);
bar((40*colorr)+2,430,(40*colorr)+35,465);
rectangle((40*colorr)+2,430,(40*colorr)+35,465);
}
}
void createshapes()
{
setcolor(15);
//line
line(10,13,45,45);
//circle
circle(30,80,15);
//pencil
line(23,122,29,122);
line(23,123,28,123);
line(23,124,27,124);
line(23,125,26,125);
line(23,126,25,126);
line(23,127,24,127);
line(23,122,23,128);
line(29,122,38,131);
line(23,128,32,137);
line(38,131,32,137);
line(29,122,23,128);
line(26,125,35,134);
//msds
line(20,162,40,162);
line(20,162,30,176);
line(30,176,20,190);
line(40,162,50,176);
line(50,176,40,190);
line(20,190,40,190);
//sequre
line(10,245,50,245);
line(10,215,50,215);
line(10,215,10,245);
line(50,215,50,245);
//motlat
line(50,290,30,265);
line(10,290,30,265);
line(10,290,50,290);
//fill background
setfillstyle(1,4);
bar(25,325,35,340);
setcolor(4);
circle(30,320,7);
setfillstyle(5,14);
bar(27,327,33,337);
setfillstyle(1,0);
gotoxy(2,26);
printf("Clear");
// bar(8,400,48,415);
}
void erase()
{
while(!kbhit())
{
//printf("welcome\n");
get_mouse_pos(&x1,&y1,&button);
// printf("%d:%d\n",x1,y1);
if(button ==1)
if(x1>8&&x1<48&&y1>400 && y1<415)
{
hide_mouse();
createsides();
createcolors();
createshapesbar();
createshapes();
show_mouse();
}
}
}
void createshapesbar()
{
//for shapes
for(int k=0;k<7;k++)
{
setfillstyle(1,0);
bar(5,5+(50*k)+3,55,(50*k)+50);
}
//for the secound options (fill background , clear all)
setfillstyle(0,0);
bar(5,360,35,390); //bar for color
setcolor(0);
rectangle(5,360,35,390);
}
void hide_mouse()
{
i.x.ax=2;
int86(0x33,&i,&o);
}
void show_mouse()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
void mousecursor()
{
//to init mouse
r.x.ax=0;
int86(0x33,&r,&r);
//to show mouse
r.x.ax=1;
int86(0x33,&r,&r);
/*
while(!kbhit())
{
r.x.ax = 3;
int86(0x33,&r,&r);
gotoxy(72,26);
if(r.x.cx>61 && r.x.dx>21 && r.x.cx<639 && r.x.dx<419 )
printf("%3d:%3d",r.x.cx,r.x.dx);
else
{
r.x.cx=0;
r.x.dx=0;
printf("%3d:%3d",r.x.cx,r.x.dx);
}
}
*/
}
int detectcolor()
{
// union REGS dc;
int cl,cxx,dxx;
while (!kbhit())
{
//dc.x.ax = 5;
//int86 (0x33,&dc,&dc);
get_mouse_pos1(&x1,&y1,&button);
if(y1 > 430 && y1 < 465)
{
if (button == 1) //set color
{
/*cxx=dc.x.cx;
dxx=dc.x.dx;
*/
for(int k=0;k<16;k++)
{
if(x1>2+40*k && x1<35+40*k)
cl=k;
}
if(cl != 15 && cl != 8)
{
color=cl;
setfillstyle(1,color);
bar(5,360,35,390);
rectangle(5,360,35,390);
}
if(cl==15 && x1 >602 && x1 <635)
{
color=15;
setfillstyle(1,color);
bar(5,360,35,390);
rectangle(5,360,35,390);
}
if(cl == 8 && x1 >322 && x1 <355)
{
color=8;
setfillstyle(1,color);
bar(5,360,35,390);
rectangle(5,360,35,390);
}
// gotoxy(28,26);
// printf("Press enter and choose a shape");
}
}
}
hide_mouse();
setcolor(color);
show_mouse();
// printf("bye color");
return (color);
}
int click(int *x,int *y)
{
r.x.ax=5;
int86(0x33,&r,&r);
if(r.x.bx &1){
*x=r.x.cx;
*y=r.x.dx;
return 1;
}
return 0;
}
void get_mouse_pos(int *x,int *y,int *button)
{
i.x.ax=3;
int86(0x33,&i,&o);
*x=o.x.cx;
*y=o.x.dx;
*button=o.x.bx&1;
}
void freehand()
{
while(!click(&x1,&y1));
//get_mouse_pos1(&x1,&y1,&button);
while(x1>61 && x1<639 && y1>20 && y1<419)
{
get_mouse_pos(&x1,&y1,&button);
x2=x1;
y2=y1;
while(button==1 && x1>61 && x1<639 && y1>20 && y1<419)
{
hide_mouse();
line(x1,y1,x2,y2) ;
x1=x2;
y1=y2;
get_mouse_pos(&x2,&y2,&button);
show_mouse();
}
}
}
void get_mouse_pos1(int *x,int *y,int *button)
{
in.x.ax=5;
int86(0x33,&in,&out);
*x=out.x.cx;
*y=out.x.dx;
*button=out.x.bx&1;
}
void lin1()
{
int flag=1;
while(flag)
{
while(!click(&x1,&y1));
while(x1>61 && x1<639 && y1>20 && y1<419)
{
while(!click(&x2,&y2));
if(x2>61 && x2<639 && y2>20 && y2<419)
{
hide_mouse();
line(x1,y1,x2,y2);
show_mouse();
}
while(!click(&x1,&y1));
//printf("x1:%d y1:%d x2:%d y2:%d " ,x1,y1,x2,y2);
}
flag=0;
}
}
void sequre()
{
while(!click(&x1,&y1));
while(x1>61 && x1<639 && y1>20 && y1<419)
{
while(!click(&x2,&y2));
if(x2>61 && x2<639 && y2>20 && y2<419)
{
hide_mouse();
rectangle(x1,y1,x2,y2);
show_mouse();
}
while(!click(&x1,&y1));
}
}
void cr()
{ int r;
while(!click(&x1,&y1));
while(x1>61 && x1<639 && y1>20 && y1<419)
{
while(!click(&x2,&y2));
if(x2>61 && x2<639 && y2>20 && y2<419)
{
r=pow(x2-x1,2)+pow(y2-y1,2);
r=sqrt(r);
//gotoxy(10,10);
//printf("%d:%d %d:%d r=%d",x1,y1,x2,y2,r);
if(r+x1<639 && r+x1>61 && r+y1<419 && r+y1>20 && r+x2<639 && r+x2>61 && r+y2<419 && r+y2>20 && abs(r-y1)>20 && abs(r-y1)<419&& abs(r-y2)>20 && abs(r-y2)<419 && abs(r-x1)>61 && abs(r-x1)<639 && abs(r-x2)>61 && abs(r-x2)<639)
{
// printf("ok");
hide_mouse();
circle(x1,y1,r);
show_mouse();
}
//else
// printf("not ok");
}
while(!click(&x1,&y1));
}
}
void msds()
{
int fx,fy,ax,ay,flag=0;
while(!click(&x1,&y1));
while(x1>61 && x1<639 && y1>20 && y1<419)
{
for(int i=0;i<5;i++)
{
while(!click(&x2,&y2));
if(x2>61 && x2<639 && y2>20 && y2<419)
{
if(flag==0)
{
flag=1;
fx=x1;
fy=y1;
}
hide_mouse();
line(fx,fy,x2,y2);
show_mouse();
fx=x2;
fy=y2;
}
else
i--;
}
hide_mouse();
line(x1,y1,fx,fy);
show_mouse();
break;
}
}
void motlat()
{
int fx,fy,ax,ay,flag=0;
while(!click(&x1,&y1));
while(x1>61 && x1<639 && y1>20 && y1<419)
{
for(int i=0;i<2;i++)
{
while(!click(&x2,&y2));
if(flag==0)
{
flag=1;
fx=x1;
fy=y1;
}
hide_mouse();
line(fx,fy,x2,y2);
show_mouse();
fx=x2;
fy=y2;
//printf("x1:%d y1:%d x2:%d y2:%d " ,x1,y1,x2,y2);
}
hide_mouse();
line(x1,y1,fx,fy);
show_mouse();
break;
}
}
void bg()
{
show_mouse();
gotoxy(10,12);
printf("bacground function");
while(!click(&x1,&y1));
while(x1>61 && x1<639 && y1>20 && y1<419)
{
f(x1,y1,15,RED);
show_mouse();
}
printf("bye bg");
}
int getshape()
{
int cl,flag=1;
while (flag)
{
// printf("welcome to get shape");
//dc.x.ax = 5;
//int86 (0x33,&dc,&dc);
get_mouse_pos1(&x1,&y1,&button);
if(x1 > 5 && x1 < 55)
{
if (button == 1)
{
/*cxx=dc.x.cx;
dxx=dc.x.dx;
*/
for(int k=0;k<7;k++)
{
if(y1>8+50*k && y1<50+50*k)
{
cl=k;
flag=0;
}
}
}
}
}
printf("the shape is %d",cl);
return (cl);
}
void switchershape(int nshape)
{
switch(nshape)
{
case 0 : { lin1(); }
case 1 :{ cr(); }
case 2 :{ freehand();}
case 3 :{ msds(); }
case 4 :{ sequre(); }
case 5 : { motlat(); }
case 6 :{ bg(); }
}
}