uses crt,graph;
const f_n='bitmap.bmp';
label sakums;
const
korig:array[0..15]of byte=(0,4,2,6,1,5,3,7,8,12,10,14,9,13,11,15);
stiga:array[1..4,1..8]of string=(('New','Open','Save','','','','',''),
('Cursor','Lines','','','','','',''),('Pixel','Line','Ellipse','Hyperbole',
'Parabole','Axes','Grid','Polynomial'),('Set Focus','Lock Draw','Delete Focus',
'Show Focus','Edit Cursor','Change Step','Undo',''));
gab:array[1..4]of integer=(3,2,8,6);
menu=22;
aa:array[1..7,1..7]of byte=((0,0,1,1,1,0,0),(0,0,0,1,0,0,0),(1,0,0,1,0,0,1),
(1,1,1,0,1,1,1),(1,0,0,1,0,0,1),(0,0,0,1,0,0,0),(0,0,1,1,1,0,0));
var
a:array[1..7,1..7]of byte;
zaib:integer;
backup:array[1..129,1..200]of byte;
sss,s:string;
izv,izv2:byte;
bb:array[1..21,1..21]of byte;
p:byte;
w,c,kods:boolean;
r,t:extended;
px,py:array[1..100]of longint;
q:char;
b:array[1..7,1..7]of byte;
d,m:integer;
x,y,k,i,solis,color,color2,n,j:longint;
fails:text;

procedure target(x,y,color:integer);
begin
for d:=1 to 7 do
for m:=1 to 7 do
 if a[d,m]=1 then putpixel(x+d-4,y+m-4,color);
end;

procedure atvaino;
var
dd,mm:integer;
begin
for dd:=1 to 200 do
for mm:=1 to 100 do
begin
backup[mm,dd]:=getpixel(GetMaxX div 2-100+dd,GetMaxY div 2-50+mm);
if random(3)=0 then putpixel(GetMaxX div 2-100+dd,GetMaxY div 2-50+mm,8);
end;
setcolor(4);
rectangle(GetMaxX div 2-99,GetMaxY div 2-49,GetMaxX div 2+100,GetMaxY div 2+50);
SEtcolor(14);
settextstyle(0,0,2);
outtextxy(GetMaxX div 2-80,GetMaxY div 2-25,'Opcija nav');
outtextxy(GetMaxX div 2-70,GetMaxY div 2+15,'pieejama!');
settextstyle(0,0,0);
readkey;
for dd:=1 to 200 do
for mm:=1 to 100 do
putpixel(GetMaxX div 2-100+dd,GetMaxY div 2-50+mm,backup[mm,dd]);
q:=#0;
end;

procedure vards(n:integer);
begin
setcolor(8);
outtextxy(45,7,'IMAGE');
outtextxy(173,7,'COLOR');
outtextxy(304,7,'DRAW');
outtextxy(424,7,'CURSOR');
outtextxy(560,7,'EXIT');
setcolor(15);
case n of
1:outtextxy(45,7,'IMAGE');
2:outtextxy(173,7,'COLOR');
3:outtextxy(304,7,'DRAW');
4:outtextxy(424,7,'CURSOR');
5:outtextxy(560,7,'EXIT');
end;
end;

function palete:byte;
var
xm,ym,dd,mm:Integer;
begin
xm:=GetMaxX div 2;
ym:=GetMaxY div 2;
for d:=1 to 100 do
for m:=1 to 100 do
backup[d,m]:=Getpixel(xm+d-50,ym+m-50);
setcolor(7);
setfillstyle(1,8);
rectangle(xm-49,ym-49,xm+50,ym+50);
bar(xm-48,ym-48,xm+49,ym+49);

for d:=0 to 3 do
for m:=0 to 3 do
 begin
 setfillstyle(1,4*m+d);
 bar(xm-44+d*24,ym-44+m*24,xm-27+d*24,ym-27+m*24);
 setcolor(0);
 if d+m=0 then setcolor(15);
 rectangle(xm-46+d*24,ym-46+m*24,xm-25+d*24,ym-25+m*24);
 end;
 d:=0;
 m:=0;
 repeat
 q:=readkey;
 case ord(q) of
 72:m:=(m+3) mod 4;
 75:d:=(d+3) mod 4;
 77:d:=(d+1) mod 4;
 80:m:=(m+1) mod 4;
 end;
 for dd:=0 to 3 do
 for mm:=0 to 3 do  begin
 setcolor(0);
 if (d=dd)and(m=mm) then setcolor(15);
 rectangle(xm-46+dd*24,ym-46+mm*24,xm-25+dd*24,ym-25+mm*24);
 end;
 until(q=#27)or(q=#13);
 for dd:=1 to 100 do
 for mm:=1 to 100 do
putpixel(xm+dd-50,ym+mm-50,backup[dd,mm]);
 if q=#13 then palete:=4*m+d;
 if q=#27 then palete:=17;
end;

procedure editbox(var s9:string;minc,maxc,liel:Byte;var kodo:boolean);
var
x9,y9,d9,m9:integer;
begin
x9:=GetMaxX div 2;
y9:=GetMaxY div 2;
for d9:=1 to 100 do
for m9:=1 to 20 do
backup[d9,m9]:=GetPixel(x9-50+d9,y9-10+m9);
setfillstyle(1,7);
bar(x9-49,y9-9,x9+50,y9+10);
setcolor(8);
rectangle(x9-49,y9-9,x9+50,y9+10);
s:='';
setcolor(0);
repeat
q:=readkey;
if (ord(q) in [minc..maxc])and(length(s)<liel) then begin
s:=s+q;
outtextxy(x9-40,y9-3,s);
end;
if (q=#8)and(length(s)<>0) then begin
delete(s,length(s),1);
setfillstyle(1,7);
bar(x9-49,y9-9,x9+50,y9+10);
setcolor(8);
rectangle(x9-49,y9-9,x9+50,y9+10);
setcolor(0);
outtextxy(x9-40,y9-3,s);
end;
until(q=#27)or(q=#13);
if q=#13 then kodo:=true else kodo:=false;
if length(s)=0 then kodo:=false;
q:=#0;
for d9:=1 to 100 do
for m9:=1 to 20 do
putPixel(x9-50+d9,y9-10+m9,backup[d9,m9]);

end;

procedure saglaba;
var
dfx:real;
q5:char;
fails2:text;
begin
for d:=1 to 100 do
for m:=1 to 20 do
backup[d,m]:=GetPixel(320-50+d,GetMaxY div 2-10+m);
setfillstyle(1,7);
bar(320-49,GetMaxY div 2-9,320+50,GetMaxY div 2+10);
setcolor(15);
rectangle(320-49,GetMaxY div 2-9,320+50,GetMaxY div 2+10);
setcolor(0);
outtextxy(285,237,'Saving...');
assign(fails,f_n);
rewrite(fails);
assign(fails2,'begin.dat');
reset(fails2);
repeat
read(fails2,q5);
write(fails,q5);
until EOF(fails2);
setfillstyle(1,4);
dfx:=320-47;

for d:=456 downto 1 do
begin
for m:=1 to 319 do
if(d<GetMaxY div 2-31)or(d>GetMaxY div 2-12)or(m<136)or(m>185)then
write(fails,char(16*korig[getpixel(2*m-1,d+22)]+korig[getpixel(2*m,d+22)]))
else
write(fails,char(16*korig[backup[2*(m-136)+1,d-GetMaxY div 2+32]]+korig[backup[2*(m-136)+2,d-GetMaxY div 2+32]]));
write(fails,#0);
dfx:=dfx+95/456;
bar(320-47,GetMaxY div 2-7,round(dfx),GetMaxY div 2+8);
outtextxy(285,237,'Saving...');
end;
close(fails2);
close(fails);
for d:=1 to 100 do
for m:=1 to 20 do
putPixel(320-50+d,GetMaxY div 2-10+m,backup[d,m]);
end;

procedure atvert;
var
q5:char;
fails2:text;
begin
assign(fails,f_n);
reset(fails);
assign(fails2,'begin.dat');
reset(fails2);
repeat
read(fails,q5);
read(fails2,q5);
until EOF(fails2);
for d:=456 downto 1 do
begin
for m:=1 to 319 do
begin
read(fails,q5);
putpixel(2*m-1,d+22,korig[ord(q5)div 16]);
putpixel(2*m,d+22,korig[ord(q5)mod 16]);
end;
if not EOF(fails) then read(fails,q5);
end;
close(fails);
close(fails2);
for d:=1 to 7 do
for m:=1 to 7 do
b[d,m]:=GetPixel(x-4+d,y-4+m);
end;

procedure kursor;
var
aaa:array[1..7,1..7]of integer;
xpo,ypo:integer;
begin
for d:=1 to 80 do
for m:=1 to 80 do
backup[d,m]:=GetPixel(280+d,200+m);
setfillstyle(0,0);
bar(281,201,360,280);
setcolor(15);
rectangle(281,201,360,280);
setcolor(8);
for d:=0 to 6 do
for m:=0 to 6 do
begin
rectangle(285+10*d,206+10*m,295+10*d,216+10*m);
setfillstyle(1,2);
if a[d+1,m+1]=1 then
bar(286+10*d,207+10*m,294+10*d,215+10*m);
aaa[d+1,m+1]:=a[d+1,m+1];
end;
xpo:=1;
ypo:=1;
repeat
for d:=0 to 6 do
for m:=0 to 6 do
begin
setcolor(8);
rectangle(285+10*d,206+10*m,295+10*d,216+10*m);

if aaa[d+1,m+1]=1 then
setfillstyle(1,2) else setfillstyle(1,0);
bar(286+10*d,207+10*m,294+10*d,215+10*m);
end;

setcolor(15);
rectangle(285+10*(xpo-1),206+10*(ypo-1),295+10*(xpo-1),216+10*(ypo-1));
q:=readkey;
case ord(q) of
72:dec(ypo);
75:dec(xpo);
77:inc(xpo);
80:inc(ypo);
32:if aaa[xpo,ypo]=1 then aaa[xpo,ypo]:=0 else aaa[xpo,ypo]:=1;
end;
xpo:=(xpo+6)mod 7+1;
ypo:=(ypo+6)mod 7+1;
until(q=#13)or(q=#27);
if q=#13 then
for d:=1 to 7 do
for m:=1 to 7 do
a[d,m]:=aaa[d,m];
for d:=1 to 80 do
for m:=1 to 80 do
putpixel(280+d,200+m,backup[d,m]);
end;
{----------------------------------------------}
begin
sakums:
for d:=1 to 7 do
for m:=1 to 7 do
a[d,m]:=aa[d,m];
c:=false;
p:=1;
n:=1;
izv:=1;
w:=false;
solis:=5;
color2:=15;
color:=4;
for d:=1 to 7 do
for m:=1 to 7 do
b[d,m]:=0;

d:=detect;
initgraph(d,m,'');
rectangle(0,menu,GetMaxX,GetMaxY);
rectangle(0,0,GetMaxX,menu-2);
for i:=1 to 5 do
rectangle(3+(i-1)*(GetMaxX div 5),2,i*(GetMaxX div 5),menu-4);
vards(0);
x:=GetMaxX div 2+4;
y:=(GetMaxY) div 2+4;
target(x,y,4);
{--------------------------}
repeat
q:=readkey;
while ord(q)=0 do q:=readkey;
if ord(q) in [49..57] then solis:=ord(q)-48;
if ord(q) in [120..131] then color:=ord(q)-118;
if upcase(q)='L' then w:=not w;
if ord(q) in [72,75,77,80,13,83] then p:=n-1;
for d:=1 to 7 do
for m:=1 to 7 do
putpixel(x+d-4,y+m-4,b[d,m]);

if q=#68 then
begin
vards(izv);
repeat
q:=readkey;
if q=#77 then inc(izv);
if q=#75 then dec(izv);
izv:=(izv+4) mod 5+1;
vards(izv);
until(q=#13)or(q=#27);
if q=#27 then q:=#0;
if(q=#13)and(izv<>5)then
begin
for d:=1 to 129 do
for m:=1 to gab[izv]*20+3 do
backup[d,m]:=getpixel((izv-1)*(GetMaxX div 5)+d,menu+1+m);
setfillstyle(1,0);
bar(1+(izv-1)*(GetMaxX div 5),menu+2,izv*(GetMaxX div 5)+2,menu+gab[izv]*20+4);
setcolor(7);
rectangle(1+(izv-1)*(GetMaxX div 5),menu+2,izv*(GetMaxX div 5)+2,menu+gab[izv]*20+4);
for i:=1 to gab[izv] do
begin
setcolor(15);
setfillstyle(1,15);
bar(3+(izv-1)*(GetMaxX div 5),menu+4+(i-1)*20,izv*(GetMaxX div 5),menu+i*20+2);
setcolor(7);
if i=1 then setcolor(0);
outtextxy(7+(izv-1)*(GetMaxX div 5),menu+9+(i-1)*20,stiga[izv,i]);
end;

izv2:=1;
repeat
q:=readkey;
if q=#72 then dec(izv2);
if q=#80 then inc(izv2);
if izv2=0 then izv2:=gab[izv];
if izv2>gab[izv] then izv2:=1;
 for i:=1 to gab[izv] do
 begin
 setcolor(7);
 if i=izv2 then setcolor(0);
 outtextxy(7+(izv-1)*(GetMaxX div 5),menu+9+(i-1)*20,stiga[izv,i]);
 end;
until(q=#27)or(q=#13);
end;
if q=#27 then izv2:=0;
if q<>#0 then
for d:=1 to 129 do
for m:=1 to gab[izv]*20+3 do
putpixel((izv-1)*(GetMaxX div 5)+d,menu+1+m,backup[d,m]);
vards(0);
if q=#27 then q:=#0;
if q<>#0 then
case izv of
1:case izv2 of
  1:q:='n';
  2:begin atvert;q:=#0;n:=1; end;
  3:begin saglaba;q:=#0; end;
  end;

2:case izv2 of
  1:begin d:=palete;if d<>17 then color:=d; q:=#0;end;
  2:begin d:=palete;if d<>17 then color2:=d; q:=#0;end;
  end;

3:case izv2 of
  1:begin putpixel(x,y,color); b[4,4]:=color;q:=#0; end;
  2:begin atvaino; end;
  3:begin q:=#32 end;
  4:begin atvaino; end;
  5:begin atvaino; end;
  6:begin atvaino; end;
  7:begin atvaino; end;
  8:begin atvaino; end;
  end;

4:case izv2 of
  1:begin q:=#13; end;
  2:begin atvaino; end;
  3:begin q:=#83 end;
  4:begin q:=#59 end;
  5:begin kursor;q:=#0; end;
  6:begin editbox(s,47,57,2,kods);
  if kods and (s<>'0')and(s<>'00') then
  begin
  val(s,solis,zaib);
  end;
   end;
  7:begin atvaino; end;
  end;

5:q:=#27;
end;
end;

case ord(q) of

72:
for i:=1 to solis do
if y>4+menu then
begin
for d:=7 downto 2 do
for m:=1 to 7 do
b[m,d]:=b[m,d-1];
for d:=1 to 7 do
b[d,1]:=getpixel(x+d-4,y-4);
dec(y,1);
end;

75:

for i:=1 to solis do
if x>4 then
begin
for d:=7 downto 2 do
for m:=1 to 7 do
b[d,m]:=b[d-1,m];
for d:=1 to 7 do
b[1,d]:=getpixel(x-4,y+d-4);
dec(x,1);
end;

77:
for i:=1 to solis do
if x<GetMaxX-4 then
begin
for d:=1 to 6 do
for m:=1 to 7 do
b[d,m]:=b[d+1,m];
for d:=1 to 7 do
b[7,d]:=getpixel(x+4,y+d-4);
inc(x);
end;

80:
for i:=1 to solis do
if y<GetMaxY-4 then
begin
for d:=1 to 6 do
for m:=1 to 7 do
b[m,d]:=b[m,d+1];
for d:=1 to 7 do
b[d,7]:=getpixel(x+d-4,y+4);
inc(y);
end;

13:
begin
px[n]:=x;
py[n]:=y;
putpixel(x,y,color);
b[4,4]:=color;
inc(n);
end;

32:
if n<>1 then
begin
r:=0;
k:=1;
for j:=1 to n-1 do
r:=r+sqrt(sqr(x/k-px[j]/k)+sqr(y/k-py[j]/k))*k;

for d:=1 to GetMaxX-1 do
for m:=menu+1 to GetMaxY-1 do

 begin
 t:=0;
  for j:=1 to n-1 do
  t:=t+sqrt(sqr(d/k-px[j]/k)+sqr(m/k-py[j]/k))*k;
 if abs(round(r-t))<p then putpixel(d,m,color2);
 end;
 inc(p);
for d:=1 to 7 do
for m:=1 to 7 do
b[d,m]:=getpixel(x+d-4,y+m-4);
end;

59:begin
for i:=1 to n-1 do
begin
for d:=1 to 21 do
for m:=1 to 21 do
bb[d,m]:=getpixel(px[i]+d-11,py[i]+m-11);
setcolor(2);
for d:=1 to 3 do begin
circle(px[i],py[i],d*3);
delay(100);
end;
for d:=1 to 21 do
for m:=1 to 21 do
putpixel(px[i]+d-11,py[i]+m-11,bb[d,m]);
end;
end;

83: n:=1;
end;


target(x,y,color);

if w then begin
setcolor(color);
circle(x,y,10);end;

until(q=#27)or(upcase(q)='N');
if q<>#27 then goto sakums;
{d:=GetMaxX;
m:=GetMaxY; }
closegraph;
{writeln(d,'  ',m);
readln; }
end.