You are on page 1of 57

MT S THUT TON HNH HC C BN

Trong mn Tin hc nhng bi ton c cha yu t hnh hc l nhng bi ton c


c th ring, vic gii n i hi nhng thut ton hnh hc ring bit.
i tng nghin cu ca hnh hc l im, ng thng, on thng, tia, mt
phng v cc hnh. Gi s rng c gi bit cc khi nim c bn nh:
+ im, ng thng, on thng, mt phng, v tr tng i ca hai ng
thng trn mt phng, v tr tng i ca hai ng thng trong khng gian, v tr
tng i ca hai mt phng.
+ Trc s, h to -cc trn mt phng v trong khng gian. Khong cch
gia hai im, khong cch t mt im n mt ng thng.
+ To im, to vc t, phng trnh ng thng, phng trnh mt
phng.
+ Tam gic, t gic, a gic, a gic li.
+ ng trn v hnh trn, cc ng trn ni tip, ngoi tip v bng tip ca
mt tam gic.
+ ng cao v trc tm ca mt tam gic.
+ Trung trc v tm ng trn ngoi tip ca mt tam gic.
+ Phn gic trong v tm ng trn ni tip mt tam gic, phn gic ngoi.
+ Mt s hnh khng gian thng gp: lp phng, hnh hp ch nht, t din
u...
y chng ta xem xt thm v mt s khi nim nh: ng gp khc, a gic
li, bao li... l nhng khi nim thng c cp trong cc bi ton Tin, n m t
qu o ca mt loi chuyn ng thng dng no hoc lin quan ti din tch mt
vng no ...
Mt ng gp khc trn mt phng gm mt dy lin tip cc on thng

[A1,A2], [A2,A3],..., [AK-1Ak], mi on thng c gi l mt cnh, cc u mt ca


cc on thng c gi l nh. Mt a gic l mt ng gp khc khp kn tc l
im AK trng vi A1. Mt a gic c gi l t ct nu c hai cnh khng lin tip
c im chung. Mt a gic c gi l li nu a gic lun nm cng mt pha i
vi ng thng i qua mt cnh bt k. Mt a gic li th khng t ct.
Ngi ta chng minh c rng: Vi mi tp hu hn M cc im trn mt

phng ta lun c th tm c mt tp con H ca M sao cho H l tp cc nh ca


mt a gic li p m mi im ca M u thuc hnh a gic ny. p c gi l bao
li ca tp im M. Vic xc nh p tng ng vi vic xc nh H nn i khi ta
cng gi H l bao li ca M.
Ngoi ra, mt s vn thng gp l: xt v tr tng i gia cc i tng
hnh hc, tm giao gia chng, tm din tch mt hnh hoc hp cc hnh, tm di
on thng, tm cc khong cch gn nht gia hai im trong mt tp im, tm chu
1

vi cchnh, tnh ln cc gc, xoay cc hnh theo cc gc no tp cc hnh


cho trc tho mn mt trng thi cho trc no , xt s chuyn ng ca mt
im, mt vt th trong khng gian hnh hc, chiu vt th khng gian trn mt
phng, ph phn mt phng bi mt tp cc hnh phng cho trc...
Cc thut ton hnh hc rt quan trng trong cc h thng phn tch v thit k
vt th v chuyn ng ca n (kiu i tng vt l). Hu ht cc thut ton ca cc
bi ton khc (khng mang yu t hnh hc) u tp trung vo x l vn bn v con
s, chng c thit k v x l sn trong phn ln cc mi trng lp trnh; trong
nhiu php ton c bn cn thit c ci t trong phn cng ca a s cc h
my tnh. Nhng khi gii cc bi ton hnh hc li gp nhng vn khc hn: cc
php ton s cp cho hnh hc th hu nh cha c sn, k c trong nhiu mi trng
lp trnh (thng ch gp trong cc phn mm ho). Do khi gii cc bi ton
hnh hc ngi lp trnh thng phi t xy dng cc php ton c bn. V l do
phn u chng hnh hc chng ta xy dng nhng php ton hnh hc c bn trc
khi nu cc bi tp. Trong phm vi khun kh ca chuyn chng ta cng ch c th
cp c mt s vn c bn nht ca loi ton ny.
c bit, khi vit chng trnh gii cc bi ton hnh hc, chng ta cn lu
mt s im sau:
+ Phng php to v vc t thng c dng gii cc bi ton hnh hc,
v vy chng ta s chn mt h trc to thch hp nh v hoc xc nh cc yu
t hnh hc lin quan.
+ Gi s trn mt phng mi im c th hin bi mt cp s thc l to
ca im. Sau cn thc hin mt s php ton s hc i vi cc to ny. Mt
sai lm ph bin thng gp l khi thc hin php chia ta khng bin lun
trng hp s chia c th bng 0 nn b li chia cho khng (Division by zero). Do
khi kim tra cc iu kin hnh hc th hin qua cc lin h to , ta nn trnh vic
thc hin php chia cho s c gi tr khng lun khc 0 v tm cch thay bng cc
iu kin khng dng php chia.
V d
iu kin ba im A(x1,y1); B(x2, y2); C(x3,y3) thng hng vit di dng t l thc
l
(X1 - X2)/(Y1 - Y2) = (X1 - x3)/ (Y1 - Y3)

(*)

nhng khi lp trnh, ta nn dng iu kin


(X1 - X2)*(Y1 - Y3) = (X1 - X3)* (Y1 - Y2)

(**)

iu kin (**) va tng qut hn (v bao gm c trng hp ng thng song song


vi cc trc to ) va trnh dc li chia cho 0.

PHN I.
KHI NIM HNH HC V CC I TNG HNH HC C BN
1. Khi nim hnh hc.
a s cc thut ton u tp trung vo vn bn v cc con s, chng c thit k v
x l sn trong phn ln cc mi trng lp trnh. i vi cc bi ton hnh hc th
tnh hung khc hn, ngay c cc php ton s cp trn im v on thng cng c
th l mt thch thc v tnh ton.
Cc bi ton hnh hc th d hnh dung mt cch trc quan nhng chnh iu li c
th l mt tr ngi. Nhiu bi ton c th gii quyt ngay lp tc bng cch nhn vo
mt mnh giy nhng li i hi nhng chng trnh khng n gin.
V d: Bi ton kim tra mt im c nm trong a gic hay khng?
2. i tng hnh hc c bn.
Trong cc bi ton tin hc thuc loi hnh hc c 3 i tng c bn l: im, on
thng v a gic.
- im: c xc nh l cp (x,y) trong h to cc.
- on thng: L cp im c ni vi nhau bng mt phn ca ng thng.
- a gic: L dy cc im m 2 im lin tip ni vi nhau bi on thng v im
u ni vi im cui to thnh ng gp khc khp kn.
3. Mt s kin thc cn nh
r
r
3.1 Gi s c cc im A(x1,y1); B(x2, y2); C(x3,y3); u ( x1 , y1 ) ; v ( x2 , y2 ) . Ta c:

uuu
r
AB =(x2-x1,y2-y1)

AB =

( x2 x1 ) 2 (y 2 - y1 ) 2

Phng trnh ng thng i qua 2 im A, B l :

(X-x1) (y2-y1)=(Y-y1)(x2-x1) (y2-y1)X (x2-x1)Y= x1y2-x2y1


Tch v hng

rr
u.v

rr
r r
r r
r r
u.v
x1 x2 y1 y2
= u . v cos(u, v) = x1 x2 y1 y2 cos(u, v) r r
u.v
( x12 y12 )( x2 2 y2 2 )
3

r r

r r

r r

Tch cho u x v = x1 y2 x2 y1 = u . v sin(u, v)

r r
r r uxv
x1 y2 x2 y1
sin(u, v) r r
u.v
( x12 y12 )( x2 2 y2 2 )

uuur uuur

iu kin 3 im A, B, C thng hng l: AB x AC 0


(x1-x2)* (y1-y3)= (x1-x3)* (y1-y2)
4. Biu din hnh hc trn my tnh.
y khng phi l mt vn kh khn. Nhng ta nn c mt cch biu din
thng nht cho ring mnh, nh vy s d dng trong vic th hin thut ton. Thng
thng ngi ta c cch biu din nh sau:
4.1 im:
Point = record
x, y: Real;
end;
4.2 on thng:
Line = Record
p1, p2: Point;
end;
4.3 a gic:
Thng thng chng ta dng mt mng biu din mt a gic, d rng trong
mt s trng hp ta c th dng danh sch lin kt hay cc kiu khc.
Polygon = array[1..n] of Point;
Var P: Polygon;
thun li th khi biu din a gic ta nn thm hai nh u v cui: nh 0
bng nh n v nh n + 1 bng nh 1:
P[0]:=P[n]; P[n+1]:=p[1];
T y ta cng thng nht vi cch khai bo ny cho cc on chng trnh c th
dng n.
* Din tch tam gic, din tch a gic:
- Cho tam gic ABC vi A(xA,yA); B(xB, yB); C(xc,yc) ta c
uuur uuur
( xB xA )( yC y A ) ( xC x A )( yB y A )
AB x AC

S=
2
2

- Cho a gic li P = P1 P2...... Pn trong im Pi = (xi,yi). B sung thm im P0


Pn; Pn+1 P1 khi din tch a gic P c tnh bng cng thc:

1
S=
2

(x
i 1

i 1

xi 1 ) yi

Ch : Ta d dng nhn thy rng cc php ton thc hin gii quyt bi ton hnh
hc th hu ht l phi lm vic vi s thc. V vy ta cng cn phi ch mt s
mo nh khi khai bo d liu v bin dch. Tu vo kch thc v yu cu v
chnh xc ca kt qu bi ton ta phi c nhng chn la hp l. Bng di y l
nhng kiu s thc m Pascal c sn:
Kiu
Single
Real
Double
Extended

Gii hn
1.5e-45..3.4e38
2.9e-39..1.7e38
5.0e-324..1.7e308
3.4e-4932..1.1e4932

Ch s c ngha
7-8
11-12
15-16
19-20

Kch thc (Byte)


4
6
8
10

c bit, mc d ch khi ta dng Double hoc Extended ta mi phi khai bo


bin dch {$N+}, nhng ta nn lc no cng lm nh vy. V khi my tnh s dng
b ng x l ton hc, cc php ton vi s thc s thc hin nhanh chng km g
so vi s nguyn (thm ch cn nhanh hn nu ta dng kiu s thc Double). Tt nht
hy dng th v t so snh, s thy ngay s khc bit.
Mt iu na cng cn ch l sai s trong tnh ton. Lm vic vi s thc bao
gi ta cng phi chp nhn vi nhng sai s nht nh. V vy khi so snh hai gi tr
vi nhau ta ch khng c dng du =, m phi xt tr tuyt i hiu hai gi tr
vi mt gi tr Epsilon no . y, Epsilon l mt s tng i b, tu vo yu
cu ca bi ton m ta c chn la v gi tr ca n (VD Esp =1E-6).
V d: Khng c dng: if x1 = x2 then
m phi dng
: if Abs(x1 x2) < Eps then

PHN II. MT S BI TON C BN


Bi ton 1: Tm v tr tng i ca im M(X0, Y0) so vi ng thng i qua 2

im A(X1, Y1) v B(X2, Y2).


Hng dn
Phng trnh tng qut ca ng thng c dng AX + BY + c = 0 vi A v B khng
ng thi bng 0.
Vic th hin ng thng bng phng trnh tng qut c nhiu ci li. D v tr
no trn mt phng to , ng thng cng c phng trnh dng tng qut.
t F(X, Y) = AX + BY + C. ng thng (d) c phng trnh F(X, Y) = 0 s chia
mt phng lm ba phn ring bit: mt phn gm cc im m khi thay to vo
5

F(X, Y), gi tr nhn c m, mt phn gm cc im m khi thay to vo F(X,


Y), gi tr nhn c dng; mi phn ny c gi l mt pha ca ng thng,
phn th ba ngn cch hai phn gm cc im trn ng thng m khi thay to
cc im thuc phn ny vo F(X, Y) gi tr nhn c l 0.
Gi s A(X1,Y1) v B(X2,Y2) l hai im phn bit. Phng trnh tng qut ca
ng thng i qua A v B l : (Y1 Y2)X + (X2 X1)Y + (X1 Y2 - X2Y1) = 0.
t F(X, Y) = (Y1 - Y2)X + (X2 X1)Y + (X1Y2 - X2Y1). Gi tr F(X0, Y0) s cho ta
kt lun v v tr tng i ca im M(X0, Y0) so vi ng thng AB.
- im M thuc ng thng AB khi F(X0,Y0) = 0
H qu
M(X3, Y3) v N(X4, Y4) khc pha nhau so vi ng thng AB khi v ch khi
F(X3, Y3). F(X4, Y4) < 0.
Bi ton 2: im M(X0, Y0) c thuc on thng ni 2 im A(X 1,Y1) v B(X2,
Y2) hay khng?
Hng dn
im M(X0, Y0) thuc on thng ni 2 im A(X1,Y1) v B(X2, Y2) nu tho mn:
F(X0, Y0) = 0 , Min(X1, X2) X0 Max(X1, X2) v Min(Y1, Y2) Y0 Max(Y1, Y2).

Chng trnh:
Chng trnh
var a, b, c, x1, y1, x2, y2, x0, y0 : real;
function max(a,b : real): real;
begin
if a>b then max:= a else max := b;
end ;
function min(a,b: real):real;
begin
if a<b then min := a else min := b;
end;
procedure nhap;
Begin
write('nhap toa

do diem

A(x1,y1): '); readln(x1,y1);


6

write ('nhap toa do diem

B(x2,y2): '); readln (x2, y2);

write('nhap toa

M(x0,y0):');

do diem

readln(x0,y0);

a:=y2-y1;
b:=x1-x2;
c:= x2*y1 - x1*y2;
end;
Function f(x,y : real) : real;
Begin
f:=a*x+b*y+c
End;
Function thuoc(x0,y0 : real) : boolean;
var ok, ok1, ok2 : boolean;
begin
ok

:= f(x0,y0)=0;

ok1 := (min(x1,x2)<=x0) and (x0<=max(x1,x2));


ok2 := (min(y1,y2)<=y0) and (y0<=max(y1,y2));
thuoc:=ok and (ok1 and ok2);
end;
BEGIN
nhap ;
if thuoc(x0,y0) then write('M thuoc doan thang AB')
else write('M khong thuoc doan thang AB');
readln
END.

Bi ton 3:
im M(X0, Y0) C thuc tia AB hay khng? D liu vo nhp t bn phm, kt
qu hin trn mn hnh.

Hng dn
im M(X0, Y0) thuc tia AB nu M thuc ng thng AB v AM=kAB vi
k 0. Do to ca M phi tho mn cc iu kin:
F(x0, y0) = 0, (X0 X1)(X2 X1) 0 v (Y0 Y1)(Y2 Y1) 0.
Chng trnh
var a, b, c, x1, y1, x2, y2, x0, y0 : real;
7

procedure nhap;
Begin
write('nhap toa

do diem

A(x1,y1): '); readln(x1,y1);

write ('nhap toa do diem

B(x2,y2): '); readln (x2, y2);

write('nhap toa

M(x0,y0):');

do diem

readln(x0,y0);

a:=y2-y1;
b:=x1-x2;
c:= x2*y1 - x1*y2;
end;
Function f(x,y : real) : real;
Begin
f:=a*x+b*y+c
End;
Function thuoc(x0,y0 : real) : boolean;
var ok, ok1, ok2 : boolean;
begin
ok

:= f(x0,y0)=0;

okl := (x0-x1)*(x2-x1)>=0;
ok2 := (y0-y1)*(y2-y1)>=0;
thuoc:=ok and (ok1 and ok2);
end;
BEGIN
nhap ;
if thuoc(x0,y0) then write('M thuoc tia AB')
else write('M khong thuoc tia AB');
readln
END.

Bi ton 4: Tm giao im (nu c) ca hai ng thng c phng


trnh tng qut:

A1x + B1y + C1 = 0 v A2X + B2y + C2 = 0


D liu vo nhp t bn phm, kt qu hin trn mn hnh.
Hng dn
Bc 1: Tnh D=a1b2 - a2b1, Dx = c2b1-c1b2, Dy = a2c1-a1c2
Bc 2: Xt 3 kh nng:
+ Nu D = Dx = Dy = 0 th kt lun 2 ng thng trng nhau
+ Nu D = 0 v ((Dx 0) hoc (Dy 0)) th kt lun 2 ng thng song song
+ Nu D 0 th kt lun 2 ng thng ct nhau ti im c (Dx/D, Dy/D)
Chng trnh:
Chng trnh
uses crt;
var a1, b1, c1, a2, b2, c2, x, y: real;
procedure Nhap;
begin
write('nhap cac he so a1, b1, c1: '); readln(a1,b1,c1);
write ('nhap cac he so a2, b2 ,c2:') ; readln (a2,b2,c2);
end;
function giaodiem(al,bl,cl,a2,b2,c2:real;var x,y: real):
integer; var d,dx,dy: real;
begin
giaoiem:= -1; {Hai ng thng song song}
d := a1*b2-a2*b1;
dx:= c2*b1-c1*b2;
dy:= a2*c1-a1*c2;
if d<>0 then
begin
x:= dx/d; y:= dy/d; {(x. y) l to giao im}
giaodiem: = 1; {Hai ng thng ct nhau}
end
else
if (dx=0) and (dy=0) then
giaoiem:= 0; {Hai ng thng trng nhau}
end;
procedure ketluan; var gd: byte;
begin
gd:= giaodiem(a1,b1,c1,a2,b2,c2,X,y);
9

if gd=1 then
write('cat nhau tai diem (',X:10:2,',',y:10:2, ') ')
else
if gd=0 then
write('hai duong thang trung nhau')
else
write('hai duong thang song song voi nhau');
end;
BEGIN
clrscr; nhap ; ketluan; readln
END.

Bi ton 5. Cho 2 on thng AB v CD vi A(X1,Y1) , B(X2, Y2), C(X3,Y3)


, D(X4, Y4). Tm giao im (nu c) ca 2 on thng

Hng dn
a) Cch 1
Tm giao im M ca 2 ng thng AB v CD, sau kim tra xem M c
thuc ng thi c hai on thng AB v CD hay khng.
b) Cch 2
on thng AB ct on thng CD trong cc trng hp sau:
- A hoc B thuc on thng CD.
- C hoc D thuc on thng AB.
- on thng AB v on thng CD giao nhau ti im M khc u mt ca
hai on thng ny khi: A, B khc pha nhau so vi ng thng CD, ng thi C,
D khc pha nhau so vi ng thng AB. Gi phng trnh ng thng AB l
F(x, y) = 0, phng trnh ng thng CD l G(x, y) = 0 th iu kin on
thng AB v on thng CD giao nhau l :
F(X3, Y3)F(X4, Y4) < 0 v G(X1, Y1)G(X2, Y2) < 0.
Chng trnh:
Bi ton 6. Cho tia AM cha im B (khc A) v on thng CD bit
to cc im l A(X1,Y1) , B(X2, Y2), C(X3,Y3) , D(X4, Y4). Tm giao
im (nu c) ca tia AM vi on thng CD.
Hng dn
Bc 1. Tm giao im N ca 2 ng thng AB v CD

10

Bc 2. Kim tra N c thuc tia AM v on thng CD hay khng. Nu c l giao


im cn tm, ngc li kt lun khng c.

Chng trnh:
Chng trnh
uses crt;
type diem = record x,y : real; end;
ml
var d

= array[1..4] of diem;
: ml;

al,bl,cl,a2,b2,c2. : real;
m : diem;
procedure nhap;
var i : byte;
begin
for i:=1 to 4 do
begin
write('nhap toa do x,y cua diem thu
readln(d[i].x,d[i].y);

,i:2, ' : ');

end;
al := d[2].y-d[1].y;
bl := d[1].x-d[2].x;
cl :=d[l].y*d[2].x-d[l].x*d[2].y;
a2 := d[4].y-d[3].y;
b2 := d[3].x-d[4].X;
c2 := d[3].y*d[4].x-d[3].x*d[4].y;
end;
function f(x,y:real): real;
begin
f := al*x + bl*y + cl;
end;
function g(x,y : real) : real;
begin
g := a2*x + b2*y + c2;
end;
function khacphiaAB(c,d: diem) : boolean;
begin
11

khacphiaAB := f(c.X,c.y)*f(d.X,d.y)<0;
end;
function max(a,b : real): real;
begin
if a>b then max:= a else max := b;
end ;
function min(a,b: real):real;
begin
if a<b then min := a else min := b;
end;
function khacphiaCD(a,m: diem) : boolean;
begin
khacphiaCD := g(a .x,a.y)*g(m.x,m.y)<0;
end;
procedure tim_M(var m: diem) ; {Tm im M trn tia AM m M kh xa A}
var k : integer; xmax,ymax : real;
begin
k := 1;
xmax:=max(abs(d[3].x),abs(d[4].x));
ymax:=max(abs(d[3].y),abs(d[4].y));
while (abs(d[l].x+ k*(d[2].x-d[l].x)) <=xmax)
and (abs(d[l].y+ k*(d[2].y-d[1].y))<=ymax)do
inc(k);
m.x := d[l].x +k* (d[2].x-d[l].x);
m.y := d[l].y +k* (d[2].y-d[1].y);
end;
function thuoctia(d3 : diem) : boolean;
var ok,okl,ok2 : boolean;
begin
ok := f(d3.x,d3.y)=0;
okl := (d3.x-d[1].x)*(d[2].x-d[1].x)>=0;
ok2 :=(d3.y-d[1] .y)*(d[2].y-d[1].y)>= 0;
thuoctia := ok and (okl and ok2);
end ;
BEGIN
clrscr;
nhap ;
tim_M(m);
12

if(g(d[1].x(d[1].y)=0)and
(min(d[3].x,d[4].x)<=d[l].x) and
(d[1].x<=max,(d [3].X,d[4].x)) and
(min(d[3].y,d[4].y)<=d[1].y) and
(d[l].y<=max(d[3].y,d[4].y))
or thuoctia(d[3]) or thuoctia(d[4])
or (khacphiaAB(d[3],d[4]))and(khacphiaCD(d[1],m))
then write('Tia AB cat doan CD')
else write('Tia AB khong cat doan CD');
readln
END.

PHN III. MT S DNG BI TON HNH HC THNG GP


Dng 1. Mi quan h gia im, on thng, a gic.
Phng php: y l mt trong s dng bi ton hnh hc n gin nht. Vic gii
bi ton dng ny ch yu s dng cc kin thc hnh hc c bn ( trnh by y
trong phn trn)

VD1 Ba im thng hng


Cho N im, hy kim tra xem c bao nhiu b 3 im thng hng.
Input: Cho trong tp vn bn DL.INP
- Dng th 1 ghi s N
- N dng tip theo, mi dng ghi to ca mt im.
Output: Ghi vo tp KQ.OUT cha mt s duy nht l s b 3 im thng hng.

(Gii hn: 1<=N<=2000, to cc im c gi tr tuyt i khng qu 10000)


13

Chng trnh:

14

VD2. ng thng ct nhau


Cho n ng thng AiBi (1 i n) phn bit vi Ai, Bi l cc im cho trc. Hy
thng bo ra mn hnh cc cp ng thng i mt ct nhau.
D liu: Cho trong file DL.INP gm N dng (N khng bit trc). Dng th i ghi 4
s thc xAi yAi xBi yBi. Cc s trn cng mt dng ghi cch nhau t nht mt du cch.

Hng dn
Bc 1: Tnh D=a1b2 - a2b1, Dx = c2b1-c1b2, Dy = a2c1-a1c2
Bc 2: Xt 3 kh nng:
+ Nu D = Dx = Dy = 0 th kt lun 2 ng thng trng nhau
+ Nu D = 0 v ((Dx 0) hoc (Dy 0)) th kt lun 2 ng thng song song
+ Nu D 0 th kt lun 2 ng thng ct nhau ti im c (Dx/D, Dy/D)
Chng trnh:

15

3. V tr ca im so vi a gic
Bi ton 7. Cho a gic gm N nh d1, d2,....dN v im M. Xc nh v tr tng
i ca M vi min trong a gic.
Phng php:
Bc 1. Kim tra M c thuc cnh no ca a gic hay khng, nu c th kt lun M
thuc min trong a gic v kt thc

16

Bc 2. Ngc li: K on thng MN song song trc honh sao cho honh ca N
ln hn max ca cc honh nh a gic. Xt giao cc cnh ca a gic vi on
thng MN. Nu s giao im l l th M trong a gic, l chn th M ngoi a gic.
Xt cnh (d[i], d[i+l]), nhng trng hp sau y c coi nh tng thm 1 giao
im:
+ nh d[i] khng thuc on thng MN, nh d[i+1] nm trn on thng MN, 2
nh d[i] v d[i+2] khc pha so vi ng thng MN.
+ nh d[i-1], d[i+2] ngoi on thng MN, hai nh d[i] v d[i+1] thuc on MN,
d[i-1] v d[i+1] khc pha so vi ng thng MN
+ nh d[i] v d[i+1] khng thuc MN v cnh (d[i],d[i+1]) ct on thng MN
d[i]
M

d[i-1]
d[i+1]

d[i]

d[i+2]

d[i+1] N

d[i]
M

d[i+2]

d[i+1]

Chng trnh:
Cho a gic khng t ct A1A2...AN vi cc nh Ai(xi,yi) nguyn. Vi im M(xM,yM)
cho trc, hy xc nh xem M c nm trong a gic cho hay khng (Trong
trng hp trn cnh a gic xem nh nm trong a gic)
D liu: Cho trong tp Dagiac.inp
+ Dng u l s N
+ N dng tip theo mi dng ghi xi,yi l to Ai
+ Dng n+2 ghi 2 s xM v yM
D liu l cc s nguyn.
Kt qu: a ra mn hnh thng bo im A c nm trong a gic hay khng
Bi ton 8:
Kim tra mt a gic N nh c l a gic li hay khng. D liu vo t file vn bn
'dgjoi.inp' dng du l s N, N dng tip theo mi dng l hai s honh d v tung
ca cc nh ln lt t 1 dn N. Kt qu hin trn mn hnh: dng ch "da giac loi"
hoc "da giac khong loi" (a gic li hoc a gic khng li).
17

Hng dn
a gic l li nu vi mi cnh ni nh i (1 < i < N) vi nh i + 1 (nh N + 1 coi
nh nh 1) th mi nh j (1 < j < N) v nh i + 2 phi lun lun cng pha so vi
ng thng cha cnh (i, i + 1).
Chng trnh
uses crt ;
const max = 20;
fi = dg_loi.inp;
type toao = array[1..max] of real;
var x,y

: toado;

n : word;
procedure input;
var i : word;
f : text;
begin
assign(f,fi); reset(f);
readln(f,n);
for i:=1 to n do readln(f,x[i],y[i]);
close(f) ;
end;
function cungfia(x1,x2,x3,x4,y1,y2,y3,y4:rea1):boo1ean;
var d1,d2: rea1;
begin
d1:=(y3-y1)*(x2-x1)-(x3-x1)*(y2-y1);
d2:=(y4-y1)*(x2-x1)-(x4-x1)*(y2-y1);
cungfia:=d1*d2>=0;
end;
function dg_loi: boolean;
var i,j,k,1 : word;
begin
for i:=1 to n do
begin
k : = i + 2 ;
1 : = i + 1 ;
if k=n+l then k:= 1;
if l=n+l then 1:= 1;
for j:=1 to n do
18

if (j<>i) and
(not cungfia( x [i],X [l], X [j],x[k],y[i],y[l],y[j],y[k]))then
begin
dg_loi:=- false;
exit;

end ;
end ;
dg_loi := true;
end ;
BEGIN
clrscr;
input;
if dg_loi then writelni'da giac loi ')
else writeln('da giac khong loi ');
END.

19

VD4. m s im c to nguyn thuc a gic


Cho a gic gm n nh (x1,y1), (x2,y2), ..., (xn,yn), bit xi v yi(i=1,...,n) l cc s
nguyn trong on [-106,106]. Cc nh c lit k theo th t cng chiu kim ng h.
Vit chng trnh tm s im c to nguyn nm trong hay trn bin a gic.
D liu: Cho trong tp tin DL.INP.
- Dng u cha s nguyn duy nht cho bit s nh.
- Tip theo l cc dng, trn mi dng c 2 s nguyn cch nhau
mt khong trng ln lt l honh , tung cc nh a gic.
Kt qu: Xut ra mn hnh s im c to nguyn nm trong hay trn bin a gic
tng:
- Tnh a,b theo cng thc:

- Xc nh s im c to nguyn: S=round(abs(a/2)+b/2+1)
Chng trnh:

Trang 20

Dng 2.Tnh din tch a gic


Phng php: Gi s cho a gic c n nh v to cc nh lu vo mng a. tnh
din tch a gic ta lm nh sau:
thun li th khi biu din a gic ta nn thm hai nh u v cui: nh 0
bng nh n v nh n + 1 bng nh 1:
P[0]:=P[n]; P[n+1]:=p[1];
* Din tch tam gic, din tch a gic:
- Cho tam gic ABC vi A(xA,yA); B(xB, yB); C(xc,yc) ta c
S=

uuur uuur
( xB x A )( yC y A ) ( xC x A )( y B y A )
AB x AC

2
2

- Cho a gic li P = P1 P2...... Pn trong im Pi = (xi,yi). B sung thm im


P0 Pn; Pn+1 P1 khi din tch a gic P c tnh bng cng thc:

1
S=
2

(x
i 1

i 1

xi1 ) yi

Trang 21

VD1. Xc nh din tch a gic


Cho N nh a gic li A1A2A3...AN-1AN vi cc nh Ai(xi,yi) c to nguyn. Hy
tnh din tch a gic trn.

D liu: Cho trong file DL.INP gm 2 dng


- Dng 1: Cha s nguyn dng N
- Dng 2: Cha 2xN s nguyn dng x1 y1 x2 y2...xN yN l to cc nh ca a
gic. Mi s ghi cch nhau mt du cch.
Kt qu: Xut ra mn hnh din tch a gic.
tng:
- Lu to cc nh a gic vo mng toado
- S dng cng thc tnh din tch a gic:

Trang 22

VD2. Kim tra a gic c t ct


Cho N nh a gic A1A2A3...AN-1AN vi cc nh Ai(xi,yi) c to nguyn. Hy cho
bit a gic cho c t ct hay khng?

Trang 23

VD3. Dy hnh ch nht


Trong mt phng to trc chun, cho N hnh ch nht c cc cnh song song vi trc
to . Mi HCN c xc nh bi to nh di bn tri v nh trn bn phi ca
n. Hy a ra dy cc hnh ch nht theo th t tng dn din tch .
D liu: Cho trong file HCN.inp gm N+1 dng.
- Dng 1. Cha s N
-Dng i+1 (1 i N): Ghi 4 s nguyn x1, y1, x2 ,y2 ln
lt l to nh di bn tri v nh trn bn phi ca
HCN i. (Cc s ghi trn mt dng cch nhau t nht mt du cch)
Kt qu: Ghi vo tp HCN.out dy cc hnh ch nht sau khi sp xp.
tng:
- Lu to cc nh a gic vo mng a
- Tnh din tch hnh ch nht theo cng thc:

- Sp xp mng a tng dn theo din tch

Trang 24

Dng 3. Xc nh din tch ph bi cc hnh ch nht


Phng php: Gi s c n hnh ch nht. tnh din tch ph bi n hnh ch nht ta
lm nh sau:
Bc 1. S dng a,b ln lt l cc mng lu honh v tung cc nh hnh ch
nht (mi hnh ch nht ch cn lu to 2 nh i din qua tm hnh ch nht).
Bc 2. Sp xp mng a,b theo th t tng dn
Bc 3. Ln lt kim tra cc hnh ch nht c to nh trn bn phi (x i+1,yi+1) v
to nh di bn tri l (xi,yi) vi 1 i n-1. Nu hnh ch nht ny thuc mt trong
cc hnh ch nht ban u th cng thm vo phn din tch ang cn tm din tch ca
hnh ch nht con ny.
V d 1. Din tch ph bi cc hnh ch nht
Trong mt phng to trc chun, cho N hnh ch nht c cc cnh song song vi trc
to . Mi HCN c xc nh bi to nh di bn tri v nh trn bn phi ca
n. Hy tnh din tch phn mt phng b ph bi cc HCN trn.
Trang 25

D liu: Cho trong file HCN.inp gm N+1 dng.


- Dng 1: Cha s N
- Dng i+1 (1 i N): Ghi 4 s nguyn x1,y1,x2,y2 ln
lt l to nh di bn tri v nh trn bn phi ca
HCN i. Cc s ghi trn mt dng cch nhau t nht mt
du cch.
Kt qu: a ra mn hnh din tch phn mt phng b ph bi hnh ch nht trn.
tng:
- Lp mng X[1..2n], Y[1..2n] ln lt cha honh , tung cc hnh ch nht
- Lu to ban u cc hnh ch nht vo mng a
- Sp xp mng X,Y tng dn
- Ln lt kim tra cc hnh ch nht c to nh trn bn phi (x i+1,yi+1) v to
nh di bn phi l (xi,yi) vi 1 i n-1. Nu hnh ch nht ny thuc mt trong cc
hnh ch nht ban u th cng thm vo phn din tch ang cn tm din tch ca hnh
ch nht con ny.
Chng trnh:

Trang 26

Trang 27

V d 2. Ph S
Trn mt phng ta , mt hnh ch nht vi cc cnh song song vi cc trc to
c xc nh bi hai im i tm: nh gc trn bn tri v nh gc di bn phi.
Cho N hnh ch nht song song vi cc trc to . Ph S ca cc hnh ch nht c din
tch nh nht cha N hnh ch nht cho.
D liu vo: c t tp PHUCN.INP c cu trc:
- Dng u tin cha N (N 30);
- Trong N dng tip theo, mi dng ghi 4 s l to ca hai nh i tm ca mt hnh
ch nht, cc s ny l cc s nguyn c tr tuyt i khng qu 100.
Kt qu: Ghi ra tp vn bn PHUCN.OUT
- Dng 1 ghi to hai nh i tm ca ph S cc
hnh ch nht
- Dng 2 ghi din tch ca phn hnh S khng nm
trong hnh ch nht no trong N hnh cho
- tng:
- Xc nh hnh ch nht H nh nht bao tt c cc hnh ch nht ban u:
Gi minx,maxx ln lt l honh nh nht v ln nht trong cc honh cc nh
hnh ch nht cho; miny, maxy ln lt l tung nh nht v ln nht trong cc
tung cc nh hnh ch nht cho. Khi hnh H c to nh di tri l
(minx,miny) v nh trn phi l (max,maxy). l ph S cn tm.
- Tnh din tch hnh H l (maxx-minx)(maxy-miny)
- Tnh din tch s ph bi cc hnh ch nht ( nu r phng php chung)
- Phn din tch cn tm l: s1:=abs((maxx-minx)*(maxy-miny))-s
- Chng trnh:

Trang 28

Trang 29

V d 3. Din tch ph bi cc hnh trn


Trn mt phng cho N hnh trn. Tnh din tch phn mt phng b ph bi cc hnh trn
trn.
D liu: Cho trong file INP.BL3 dng u l s lng hnh
trn, t dng th 2 tr i mi dng cha 3 s nguyn dng
l ta x, y ca tm v bn knh ca tng hnh trn (cc s
trn cng mt dng ghi cch nhau t nht 1 du cch)
Kt qu: Xut ra mn hnh
- tng:
- Tm hnh ch nht nh nht c cc cnh song song vi cc trc to v cha ton b
N hnh trn
- Chia hnh ch nht ny thnh li cc vung c cnh 0.1 n v, vi mi thuc
hnh ch nht kim tra xem ny c thuc vo hnh trn no hay khng, nu c th
tng din tch cn tnh ln 0.01 n v.
- Chng trnh:

Trang 30

Dng 4. Xc nh a gic nh nht bao tt c cc im, a gic cho


Phng php: Cho N im A1, A2, ..., AN trn mt phng. xc nh mt a gic
khng t ct cha mt s im cho v bao tt c cc im cn li ta lm nh sau:
Bc 1. Tm im c tung nh nht. im s l nh a gic
Bc 2. Gi s ta chn c im PM. Tm im Pi sao cho gc hp bi PMPi v trc
honh l nh nht v ng thi gc ny phi ln hn gc hp bi P MPM-1 v trc honh.
im Pi s l mt nh ca a gic.
Bc 3. Ly kt qu l dy cc nh P tm c.
Lu : Vi bi ton tm a gic bao nhau th cn ghi nh a gic a bao a gic b khi mi
im trong a gic b u nm trong a gic a.

Trang 31

V d 1. a gic khng t ct
Cho N im A1, A2, ..., AN trn mt phng. Cc im u c to nguyn v khng c
3 im bt k trong chng thng hng. Hy vit chng trnh thc hin cc cng vic
sau y: Xc nh mt a gic khng t ct c nh l mt s im trong cc im
cho v cha tt c cc im cn li v c chu vi nh nht. Hy tnh din tch a gic
ny.
D liu: cho trong tp HCN.INP gm n+1 dng
+ Dng 1: Cha s N
+ Dng i+1 (1 i N): Ghi 2 ch s nguyn xi,yi l to nh
Ai.
Cc s trn cng mt dng cch nhau mt khong trng.
Kt qu: Xut ra tp HCN.Out
+ Dng 1: Ghi 3 s K, V, S vi K l s nh a gic tm c, V l chu vi, S l din tch
ca n.
+ Dng i+1(1 i K): Ghi to ca nh a gic.
- tng:
- Tm im c tung nh nht. im s l nh a gic
- Gi s ta chn c im P M. Tm im Pi sao cho gc hp bi PMPi v trc honh
l nh nht v ng thi gc ny phi ln hn gc hp bi P MPM-1 v trc honh. im
Pi s l mt nh ca a gic.

Trang 32

Trang 33

V d 2. a gic bao nhau


Cho N a gic tho mn cc tnh cht
- Vi 2 a gic bt k lun c mt a gic m mi im ca n nm trong a gic kia.
- Cc cnh ca chng khng c im chung.
Bi ton t ra l: Vi mi a gic i, c bao nhiu a gic bao n? (i nm trong bao
nhiu a gic)
D liu vo: Ghi trong tp tin vn bn Dagiac.Inp.
- Dng u tin ghi s t nhin N (3N10000).
- Trn N dng tip theo: Dng th i+1 ghi thng tin v a gic c s hiu th i. Bao gm
s u tin Si l s nh ca a gic (S i3), Si cp s nguyn tip theo ln lt l honh
v tung cc nh ca a gic.
Cc s trn cng dng cch nhau bi t nht mt
khong trng.
D liu ra: Ghi trong tp tin Dagiac.Out
- Gm N dng
- Dng th i: Ghi s lng a gic bao a gic i.
- tng:
- S dng cc mng a,vt,kq (vi a[i] lu gi tr honh nh nht ca cc nh ca a
gic th i, vt[i] ch a gic th i, mng kq lu kt qu)
- Thc hin sp xp cc a gic theo th t tng dn ca gi tr honh nh nht ca
cc nh ca cc a gic.
- Do theo iu kin bi ton l vi 2 a gic bt k lun c mt a gic m mi im
ca n nm trong a gic kia nn KQ[vt[i]] =i-1
- Chng trnh:

Trang 34

V d 3. Hnh ch nht bao nhau


Cho N hnh ch nht trn mt phng m cc cnh song song vi cc trc to . Bit
hnh ch nht i bao hnh ch nht j nu c 4 nh ca hnh ch nht j u nm trong
hnh ch nht i hoc nm trn cnh ca hnh ch nht i.
Mt dy cc hnh ch nht c gi l hnh ch nht bao nhau chiu di k (k1) nu
dy ny gm cc hnh ch nht H1, H2, ..., Hk sao cho hnh ch nht i bao hnh ch
nht i+1 vi i=1 ... (k-1). Hy tm s k ln nht ni trn.
D liu vo: c cho trong tp tin HCN.INP
- Dng th nht ghi s N (1N1000).

Trang 35

- N dng tip theo, dng th i ghi 4 s nguyn x1, y1, x2,


y2 (-10000< x1,y1,x2,y2<10000) ln lt l honh ,
tung cc nh tri trn, phi di ca hnh ch nht.
Kt qu: c ghi vo tp vn bn HCN.OUT gm mt
dng cha s nguyn duy nht l s k tm c hoc s
-1 nu khng tn ti s k tho iu kin bi.
- tng:
- Tnh din tch cc hnh ch nht (HCN)
- Sp xp li cc HCN theo th t khng gim ca din tch cc HCN
- Lp hm kim tra HCN i bao HCN j, tho mn iu kin:
(x1[i]<=x1[j]) and (y1[i]>=y1[j]) and (x2[i]>=x2[j]) and (y2[i]<=y2[j])
- Xc nh s lng cc HCN bao HCN i v lu vo phn t mng kq[i] bit rng:
nu th kq[i]:=kq[j]+1
- Chng trnh:

Trang 36

Trang 37

V d 4. Hnh ch nht bao nhau


Hnh ch nht A bao hnh ch nht B nu mi im thuc hnh ch nht B u nm
trong hoc thuc hnh ch nht A.
Trong mt phng Oxy cho n hnh ch nht c cc cch song song vi cc trc to .
Yu cu:
1. Tm s hnh ch nht bao nhau nhiu nht (s lng hnh phi >1).
2. Trong s cc tp hnh ch nht bao nhau, tm tp cc hnh ch nht bao nhau c tng
din cc din tch ca cc hnh ch nht trong tp l ln nht.
3. Tm hiu din tch ca hnh ch nht nh nht bao tt c cc hnh ch nht cho vi
din tch ca phn mt phng b ph bi cc hnh ch nht cho.
D liu vo: Cho trong tp vn bn HCN.IN
- Dng u: n (s lng hnh ch nht, 1N1000)
- n dng tip theo: Mi dng cha 4 s dng a, b, c, d (a, b, c, d l cc s nguyn) vi (a,
b) v (c, d) l to ca hai nh i tm ca hnh ch nht.
Kt qu: Xut ra tp vn bn HCN.Out
- Dng u: Cho bit tp cc hnh ch nht bao nhau nhiu nht theo dng k l m n ... vi
ngha-Hnh ch nht th k thuc hnh ch nht th l, hnh ch nht th l thuc hnh
ch nht th m, hnh ch nht th m thuc hnh ch nht n ... nu khng c th ghi t
"khong"
- Dng th 2: Cho bit tp cc hnh ch nht bao nhau c tng cc din tch ca cc
hnh ch nht trong tp hp l ln nht theo qui cch nh dng th nht. Nu khng th
ghi s 0.
- Dng th 3: Cha con s biu din hiu din tch hnh ch
nht nh nht bao tt c hnh ch nht cho vi din tch ca
phn mt phng b ph bi cc hnh ch nht cho.
- tng:
- Lu ta cc nh hnh ch nht vo 4 mng x1,y1,x2,y2
- Dng mng bao(n,n) nh du cc hnh ch nht bao nhau: Vi bao[i,j]=1 c ngha
hnh ch nht i bao hnh ch nht j, bao[i,j]=0 th ngc li.
- Dy hnh ch nht th i, k, j gi l bao nhau nu (Bao[i,k]+bao[k,j]>bao[i,j]) v
(bao[i,k]>0) v (bao[k,j]>0). S dng mng KQ lu cc hnh ch nht k tho mn
- Hnh ch nht nh nht bao tt c cc hnh ch nht cho l hnh ch nht c to
gc di phi l (xmin,ymin) v to gc trn tri l (xmax,ymax)
- Phng php tm din tch ph trnh by dng 3

Trang 38

Trang 39

Trang 40

Mt s bi ton v hnh vung

Hn cc bn bit n hnh vung, mt t gic


c bit c 4 cnh bng nhau v 4 gc bng nhau
bng 900), hnh vung l s kt hp hon ho
gia hnh ch nht v hnh thoi. Trong Tin hc,
cng c rt nhiu bi ton th v v hnh vung.
Chng ta hy cng tm hiu mt trong s cc bi
on y.

Trc tin, chng ta cng xt bi


on:

Bi ton 1. Cho 4
xem bn im
vung c cc cnh

im trn mt phng. Hy kim tra


ny c phi l nh ca mt hnh
song song vi trc to hay khng.

Nhn xt 1a. Hnh


bng

vung l hnh ch nht c 4 cnh

nhau.
Nhn xt 1b. Nu xA= xB, xC = xD, yA = yD v yB = yC th ABCD
l hnh ch nht (c cc cnh song song vi trc to )
Bc 1. Bn sp xp to 4 im theo th t tng dn theo honh (trong hnh, th
t cc im sau khi sp xp l A, B, D, C). Sau chng ta kim tra nhn xt 1b. Nu
ng chng ta qua bc 2, nu sai ta ghi kt qu SAI.
Bc 2. Tnh di AB, BD, CD, DA. Nu AB = BC = CD = DA th ta ghi DUNG,
nu sai ta ghi SAI.
Nh vy chng ta gii quyt xong bi ton 1. Tuy nhin, nu cc cnh hnh vung
khng song song vi trc to th bi ton 1 s tr thnh bi ton nh
sau:

Trang 41

Bi ton 2. Cho 4 im trn mt phng. Hy kim tra


xem bn im ny c th l nh ca 1 hnh vung hay
khng?
Input: File KTHV.INP gm 4 dng l to 4 im
ban u. Output: File KTHV.OUT cha s 1 (hoc
-1) nu 4 im c
th (khng th) l 4 nh mt hnh vung.
V d:

Nu gii theo cch thng thng, ta khng nhng kim tra di 4 cnh m phi
kim tra n c ln 4 gc. Ti ngh ra mt gii thut n gin hn nh sau :
Chng ta cng sp xp 4 im ban u theo th t tng dn theo honh , ta c 4
im sp xp theo th t l A, B, D, C. Sau , chng ta s to ra 4 im mi l
A, B, C, D sao cho xA = xB=min (cc honh ca A, B, C, D) ; xC = xD
= max (cc honh ca A, B, C, D); yA = yD = min
(cc tung ca A, B, C, D); yB = yC = max (cc tung ca A, B, C, D).
By gi chng ta kim tra gi tr k = (AB = BC = CD = DA) and (AB = BC =
CD = DA). Nu k = True th ABCD l hnh vung, ngc li th ABCD khng phi l
hnh vung.
Sau y l chng trnh ca bi 2.
Program KiemTraHinhVuong;
Const fi = 'KTHV.INP';
fo = 'KTHV.OUT';
Type Point = Record x,y:real;
End;
Var P : Array[1..8] of Point; {Cc im
A,B,D,C,A,B,C,D}
A : Array[1..8] of Real; {AB,BC,CD,DA,AB,BC,CD,DA}
i,j : byte; f:text;
FunctionD(A,B:Point):real;
Begin
D:=sqrt(sqr(A.x-B.x)+sqr(A.y-B.y));
End;
Trang 42

Procedure DoiCho(Var A,B:Point);


Var k : Point;
Begin
k:=A; A:=B; B:=k;
End;
Procedure ReadInp;
Begin
Assign(f,fi); Reset(f);
For i:=1 to 4 do
Read(f,P[i].x,P[i].y);
Close(f);
End;
Procedure Solve;
Var minx,miny,maxx,maxy:real;
k:boolean;
Begin
For i:=1 to 4 do
For j:=i to 4 do
If P[i].x>P[j].x then DoiCho(P[i],P[j]);
minx:=P[1].x;
miny:=P[1].y;
maxx:=P[1].x;
maxy:=P[1].y;
For i:=2 to 4 do
Begin
If P[i].xthen minx:=P[i].x;
If P[i].ythen miny:=P[i].y;
If P[i].x>maxx then maxx:=P[i].x;
If P[i].y>maxy then maxy:=P[i].y;
End;
P[5].x:=minx; P[6].x:=minx; P[7].x:=maxx; P[8].x:=maxx;
P[5].y:=miny; P[8].y:=miny; P[6].y:=maxy; P[7].y:=maxy;
k:=false;
A[1]:=D(P[1],P[2]);A[2]:=D(P[2],P[4]);
A[3]:=D(P[4],P[3]);A[4]:=D(P[3],P[1]);
A[5]:=D(P[5],P[6]);A[6]:=D(P[6],P[7]);
A[7]:=D(P[7],P[8]);A[8]:=D(P[8],P[5]);
k:=(A[1]=A[2]) and (A[2]=A[3]) and (A[3]=A[4]);
k:= k and (A[5]=A[6]) and (A[6]=A[7]) and(A[7]=A[8]);
Assign(f,fo); Rewrite(f);
If k then Writeln(f,1) elseWriteln(f,-1); Close(f);
End;
Begin
ReadInp;
Solve;
End.
Trang 43

Bn c c th t chng minh thut ton trn cho bi ton 2 l ng. Nu khng


thch, bn cng c th kim tra gc vung bng cch kim tra 4 tam gic ABC,
BCD, CDA, DAB c phi l 4 tam gic vung ln lt ti 4 nh B, C, D, A hay
khng (bng nh l Pythagore o)...
Bi ton 3. Cho hai im A(xA, yA) v B(xB, yB) khng trng nhau. Hy tm to
cc im C, D, E, F sao cho ABCD v ABEF l cc hnh vung.
Sau khi phn tch bi ton ny, ti c kt qu: Nu xA xB th to 4 im C, D,
E, F s l:
xC = xB + (yB - yA) ; yC = yB (xB xA).
xD = xA + (yB - yA) ; yD = yA (xB xA).
xE = xB (yB - yA) ; yE = yB + (xB xA).
xF = xA (yB - yA) ; yF = yA + (xB xA).
Vi xA > xB th vi tnh ton nh trn, ta s c hai hnh vung ABDC, ABFE
(khng ng vi ), do chng ta ch cn o to hai im C v D cho nhau, E
v F cho nhau.
Bi ton 3 l bi ton cho 2 nh k nhau, xc nh 2 nh cn li ca hnh vung. Sau
y l bi ton 4, cho 2 nh i nhau, xc nh 2 nh cn li ca hnh vung.
Bi ton 4. Cho hai im A(xA, yA) v C(xC, yC) l hai nh i nhau ca mt
hnh vung. Hy tm to cc im B v D sao cho ABCD l hnh vung.
Input : File HAIDINH.INP gm 2 dng l to 2 nh A v C ca hnh vung. Output
: File HAIDINH.OUT gm 2 dng l to 2 nh B v D ca hnh vung (mi to
trong file output chnh xc n ch s th 4 sau du phy).

Ta c nhn xt : Hnh vung c hai ng cho bng nhau v vung gc vi nhau ti


trung im mi ng. Ta cn tm trung im ca AC, l im O c to :

Sau , chng ta phn tch tip, c kt qu :


xB = xO + (yA yO) ;
yB = yO + (xO xA)
xD = xO (yA yO) ;
yD = yO (xO xA)

Trang 44

Sau y l chng trnh ca bi 4.


ProgramTim2Dinh;
Const fi = 'HAIDINH.INP';
fo = 'HAIDINH.OUT';
Type Point = Record x,y: real;
End;
Var P:Array[1..5] of Point;
f:text;
Procedure ReadInp;
Begin
Assign(f,fi); Reset(f);
Read(f,P[1].x,P[1].y);
Read(f,P[2].x,P[2].y);
Close(f);
End;
ProcedureSolve;
Begin
P[5].x:=(P[1].x+P[2].x)/2; P[5].y:=(P[1].y+P[2].y)/2;
P[3].x:=P[5].x+(P[1].y-P[5].y); P[3].y:=P[5].y+(P[5].x-P[1].x);
P[4].x:=P[5].x-(P[1].y-P[5].y); P[4].y:=P[5].y-(P[5].x-P[1].x);
End;
Procedure WriteOut;
Begin
Assign(f,fo); Rewrite(f);
Write(f,P[3].x:0:4,' ',P[3].y:0:4);
Writeln(f);
Write(f,P[4].x:0:4,' ',P[4].y:0:4);
Close(f);
End;
Begin
ReadInp;
Solve;
WriteOut;
End.

Chng ta cng xem s pht trin ca nhng bi ton v hnh vung:


Trang 45

Bi ton 5. Hnh vung ( thi Tin hc tr khng chuyn ton


quc 2001
bng B).
Cho mt li N x N im gm N dng v N
ct
l cc im nt ca mt li
vung. Cc dng c nh s t trn xung
di, cc ct c nh s t tri qua phi
bt u t 1. Trn li im cho mt s
on thng, mi on ni mt cp im cnh
nhau trn cng mt dng (on ngang) hoc
trn cng mt ct (on dc). Cn phi m
s cc hnh vung vi kch thc nht nh
c to thnh bi cc on thng cho
ca li nu trn.
Chng hn hnh 4, c 3 hnh vung: hai hnh kch thc 1 v
mt hnh kch thc 2 (kch thc ca hnh vung l s cc on
thng to thnh 1 cnh ca hnh vung).
Yu cu : Hy xc nh s lng cc loi hnh vung v s hnh
vung mi loi trong li im cho
(cc hnh vung c cng kch thc c xp vo cng mt loi).
Input: SQUARE.INP
-Dng u tin cha 2 s nguyn N l s dng (s ct) ca li.
-Dng th hai cha s nguyn M l s cc on thng c cho
trn li.
-M dng tip theo, mi dng biu din mt on thng, c dng :
H i j ch mt on ngang trn dng th i ni 2 im ct j v
ct j+1
hoc V i j ch mt on dc trn ct th i ni hai im
dng j v dng j+1.
Output : SQUARE.OUT
-Dng u tin ghi s nguyn P l s loi hnh vung c trn li.
-P dng tip theo, mi dng ghi thng tin m t v mt loi hnh
vung v s lng hnh vung loi , bao gm hai s nguyn a
v b cho bit c a hnh vung c cnh di b. Cc thng tin v
cc loi hnh vung phi c a ra theo th t tng dn ca
di cnh.
-Trong trng hp khng tm c bt c mt hnh vung no th
ghi duy nht mt dng thng bo : NO SQUARE.
Trang 46

V d : (Theo hnh )

bi ny, chng ta khng ch kim tra hnh vung thng qua cc


nh m cn phi kim tra cc on ni gia chng na.
Vic u tin l t chc d liu nh th no cho hp l. Chng ta s
t to nt hng i, ct j l(i, j) vi
. Nh vy nt gc
trn tri c to (1, 1), nt gc di, phi s l (N, N). Ta s s
dng 1 mng
C : Array[1..9,1..9,1..2] of boolean lu cc cnh, trong :
C[i,j,1] = True / False nu c / khng c on ni ngang gia nt (i, j) v
nt (i, j+1).
C[i,j,2] = True / False nu c / khng c on ni dc gia nt (i, j) v nt
(i+1, j).
Hin nhin, C[i,N,1] = False vi mi i v C[N,i,2] = False vi mi i.
Ta s dng mng HV[1..9] ch s hnh vung m cnh c di l
1 on, 2 on, , N on. Mt tng to bo l vt cn ton b
tm ra tt c cc b 4 im ri kim tra. Tuy nhin, tng ny
khng c kh thi cho lm. Chng ta c th i theo gii php sau :
Ti mi nt (i,j) vi i<="" p="">
For i:=1 to N do

Trang 47

For j:=1 to N do
Begin
max:=i; If max
For d:=1 to N-max do
If KT(i,j,d) then Inc(HV[d]); End;
Km theo , chng ta phi c hm KT(i,j,d) tr v gi tr Boolean
kim tra xem nt (i,j) c th l nh trn tri ca hnh vung m
cnh c di d on hay khng. Nh vy, chng trnh hon chnh
ca bi 5 l :
ProgramSquare;
Const fi = 'SQUARE.INP'; fo = 'SQUARE.OUT'; Vari,j,d,max,N,M,P:byte;
HV : Array[1..9] of integer;
C : Array[1..9,1..9,1..2] of boolean;
f:text; ProcedureReadInp; Var a,b:byte; ch:char; Begin
Fillchar(C,sizeof(C),false); Fillchar(HV,sizeof(HV),0); P:=0;
Assign(f,fi); Reset(f); Readln(f,N); Readln(f,M);
For i:=1 to M do
Begin
Readln(f,ch,a,b);
If ch='H' then C[a,b,1]:=True; If ch='V' then C[b,a,2]:=True; End;
Close(f); End; FunctionKT(i,j,d:byte):boolean; Label Ra;
Var a:byte; k:boolean; Begin
k:=true;
For a:=i to i+d-1 do {Kim tra nhng on dc}
Begin
If C[a,j,2] = false then
Begin
k:=false; Goto Ra; End;
If C[a,j+d,2] = false then
Begin
Page 1 of 58

k:=false; Goto Ra; End;


End;
For a:=j to j+d-1 do {Kim tra nhng on ngang}
Begin
If C[i,a,1] = false then
Begin
k:=false; Goto Ra; End;
k:=false; Goto Ra; End;
End;
Ra : KT:=k; End; ProcedureSolve; Begin
For i:=1 to N do
For j:=1 to N do
Begin max:=i;
If maxthen max:=j; For d:=1 to N-max do
If KT(i,j,d) then Inc(HV[d]); End;
End; ProcedureWriteOut; Begin
For i:=1 to N do
If HV[i]>0 then inc(P); Assign(f,fo); Rewrite(f); Writeln(f,P);
For i:=1 to N do
If HV[i]>0 then Writeln(f,HV[i],' ',i); Close(f);
End;
If C[i+d,a,1] = false
then

Begi
begin
n

ReadInp; Solve; WriteOut; End.


a. Quan h gia cc im - hm CCW.
iu gi nh ta nht khi nhc n quan h gia cc im l khong cch gia
chng. Ta c th tnh khong cch gia hai im nh sau:
function Dist(p1, p2: Point): Real;
begin

Page 2 of 58

Dist:= Sqrt(Sqr(p1.x p2.x) + Sqr(p1.y p2.y));


end;
Nhiu khi ta phi tr li cu hi: vic i t A, B sang C l ta r phi hay r
tri?. iu ny tng chng nh n gin v c cm gic l v ngha, nhng thc t n
li rt quan trng trong mt s thut ton c th.
B

R phi

R tri

Ta c th dng tch c hng trong khng gian gii quyt vn ny. Ta ang
lm vic trong khng gian 2 chiu (mt phng) v vy i vi chiu th 3 th mi gi tr
u l Zero (nu ai hi tch c hng l g th xin tham kho ch khc).
Hm CCW sau tr ra -1 nu l r tri, 1 nu r phi, 0 nu 3 im thng hng. Bn
c c th x l c th hn trong trng hp 3 im thng hng (im no nm gia),
nhng trong nhiu ng dng th iu l khng cn thit.
function CCW(p1, p2, p3: Point): Integer;
var
a1, b1, a2, b2, t: Real;
begin
a1 := p2.x p1.x;
b1 := p2.y p1.y;
a2 := p3.x p2.x;
b2 := p3.y p2.y;
t := a1*b2 a2*b1;
if Abs(t) < Eps then CCW := 0
else
if t > 0 then CCW := 1
else CCW := -1;
end;
3. Bao li.
Bao li ca mt tp hp im c nh ngha l mt a gic li nh nht cha
ton b tp im ny. Mt cch tng ng, bao li l mt ng ngn nht bao quanh
tp im. Bao li c mt s tnh cht d dng nhn ra l: cc nh ca n phi thuc tp

Page 3 of 58

im cho; vi mt ng thng bt k nm ngoi bao khi ri v pha bao th s chm


mt trong cc nh ca bao.
Bi ton: Cho tp im, tm bao li ca n?
Chng ta tm b qua mt s v d cc oan nh: tt c tp im u nm trn mt
ng thng?!!!...
a. Thut ton bc gi.
y l mt gii thut rt con ngi. Bt u bng vic chn mt im chc chn
thuc bao, dng mt tia qut ngc chiu kim ng h cho n khi gp mt im khc,
ta c thm mt nh thuc bao, li tip tc vi im va tm cQu trnh kt
thc khi gp li nh u tin.

Cc bc ca thut ton
C nhiu cch chn im u tin, mt trong cch l ta chn im c honh
nh nht trong cc im c tung nh nht.
Mt iu ng ch y l vic qut mt tia ngc theo chiu kim ng h
tm im u tin chm phi thc cht l ta tm im m tia ni t im gc ti n to
vi trc honh mt gc b nht (iu ny kh d hiu thi). V vy chng ta cng cn
phi bit cch tnh gc khi cho im gc v im cn xt . Nhng ch vi vic sp
xp (tm gc nh nht) thi m phi lm phc tp n vy th tht l ung cng. Ta c
th a ra mt th t hon ton ging vi vic tnh gc c th m chng trnh th n
gin hn nhiu:
function Angle(p1, p: Point): Real;
var
dx, dy, ax, ay, t: Real;
begin {p l im gc}
dx := p1.x p.x;
dy := p1.y p.y;
ax := Abs(dx);
ay := Abs(dy);
if ax + ay < Eps then t := 0
else t := dy/(ax + ay);
if dx < 0 then t := 2 t
else
if dy < 0 then t := 4 + t;
Angle := t;
end;
Sau y l th tc tm bao li theo thut ton bc gi.
procedure Wrap;
Page 4 of 58

var
i, li: Integer;
min, tmp: Real;
t: Point;
begin
t := p[1];
li := 1;
for i := 2 to n do
if (p[i].y < t.y)or(p[i].y = t.y)and(p[i].x < t.x) then
begin
t := p[i];
li := i;
end;
p[n + 1] := t; { pht hin thi im kt thc}
m := 0; {m s l s im trn bao}
repeat
Inc(m);
p[li] := p[m];
p[m] := t;
min := max;
for i := m + 1 to n + 1 do
begin
tmp := Angle(p[i], p[m]);
if (tmp < min) or ((tmp = min) and
(Abs(t.xp[m].x) < Abs(p[i].xp[m].x))) then
begin {nu nhiu im tho mn, chn im xa nht}
min := tmp;
li := i;
t := p[i];
end;
end;
until li = n + 1;
end;
b. Thut ton Graham.
Thut ton bc gi i hi mt chi ph l O(M*N) (trong M l s im trn
bao). V vy n ch lm vic tt trong trng hp s im nm trn bao nh hn nhiu
so vi tng s. Nhng trong trng hp xu nht (tt c mi im u nm trn bao) th
chi ph thut ton s ln ti O(N2) - rt ti t! Chng ta s tip cn mt phng php tt
hn phng php qut Graham. Phng php ny c chi ph thut ton n nh v

Page 5 of 58

khng tn km lm. Hu nh tt c chi ph l dnh cho vic khi mt to ng khp


kn n t tp im cho.
Chn im cht c honh x ln nht trong cc im c tung y nh nht (khi
hiu r thut ton cc bn s bit c nguyn nhn). Chuyn im cht v v tr 1
tin cho tnh ton. Ta sp xp cc im theo kho l gc to bi im v im cht
vi trc honh theo th t tng dn. Khi i theo th t p[1], p[2], p[N], p[1] ta thu
c mt a gic khp kn n.
Ta i vng qua a gic ny, th t mt im vo bao v kim tra xem cc im
trc c cn nm trn bao hay khng. Nu khng ta ch vic loi im ra khi
bao thi.
Vic kim ra mt im c cn nm trn bao hay khng c th lm nh sau: khi cho
mt im mi vo bao, ta s ln ngc li nhng im nm trong bao. Trong qu
trnh, nu gp mt im l khc r phi th im ny s khng thuc bao na, ta loi n
lun. Qu trnh kt thc khi ta gp mt im l khc r tri, v tt c cc im t li
v 1 chc chn s thuc bao.

Cc bc ca thut ton.
Ci t khng phi l mt vn kh nhng phi cnh gic vi sai s v cc im
thng hng.
Vic xy dng ng khp kn n khng thc s phi dng hm Angle v d gy
sai s v chi ph hi ln. V tt c cc tia to bi im cht v mt im bt k u
trong gc phn t I v II nn ta c th dng hm Lower sau lm php so snh cho
vic sp xp.
function Lower(p1, p2: Point): Boolean;
var
a1, b1, a2, b2: Real;
begin
a1 := p1.x p[1].x;
b1 := p1.y p[1].y;
a2 := p2.x p[1].x;
b2 := p2.y p[1].y;
Lower := a1*b2 > a2*b1;
Page 6 of 58

end;
Thc cht ta so snh hai gi tr a1/b1 v a2/b2, tc l cotg ca hai gc. Nhng ta
khng lm nh vy v phi xt b1, b2 liu c bng 0 hay khng.
Sau y l on chng trnh miu t phng php qut Graham. Ta coi mi cng
vic khi to xong xui. Hm CCW ni ti phn trc.
procedure GrahamScan;
var
i: Integer;
begin
m := 2;
for i := 3 to n do
begin
while CCW(p[m - 1], p[m], p[i]) <> 1 do Dec(m);
Inc(m);
p[m] := p[i];
end;
end;
Chi ph cho th tc trn t l thun vi N. ng vy, mc d trong vng lp c mt
vng lp, nhng ta l khng im no b loi qu mt ln nn vng lp ny ch hot
ng khng n N ln.
Nh vy chi ph cho thut ton ny l O(NlogN) nu ta dng phng php sp xp
tt (nh Quick Sort chng hn).
c. Ci tin.
Ta c th lm gim chi ph tnh ton i rt nhiu bng cch loi b nhng im
chc chn khng thuc bao.
V d nh ta loi i nhng im nm hon ton trong t gic c cc nh l cc
im c honh ln nht, honh nh nht, tung ln nht, tung nh nht. i
vi nhng b d liu c to mt cch ngu nhin th vic ny rt c ch. Nhng nu
tt c cc im u thuc bao th vic ny l v ngha. Ni chung mi cch tham lam th
cng u tt trong mt s trng hp nht nh m thi.
4. Cp im gn nht.
Bi ton: Cho tp im, hy tm cp im c khong cch nh nht trong tp im trn.
Mt cch th thin ta c th xt tt c cc cp im v lu li cp im c khong
cch nh nht. Nhng nh vy th chi ph thut ton s l O(N 2). Ta hon ton c th
gii qut bi ton ny vi chi ph l O(NlogN) vi vic p dng t tng chia tr
ca thut ton Merge Sort (xin c phn cu trc d liu v gii thut). tng thut
ton nh sau: ta sp xp cc im theo honh (hoc tung cng c). Ti mi
bc ta chia tp lm hai phn th cp im gn nht s nm mt trong hai phn hoc l
cp im m mi im thuc mt phn.
Page 7 of 58

Chia tr
Vn l phi x l trng hp mi im nm mt phn, cn trng hp c hai
im u thuc mt phn th c gii quyt v li gi qui. Ta c th s dng
ngay th t sp xp v khong cch min tm c c lm cn cho trng hp
xt trn hai phn. Khi xt mi im na ny, nu gp im na kia c hiu honh
n n khng nh hn khong cch min tm c th ta dng lun v tp im
c sp theo x.
Nhng nh vy vn c th gp phi trng hp xu: cc im nm st hai bn
ng phn cch; trong trng hp , nu x l khng tt th chi ph c th s l
O(N2).

trng hp xu
Ta gii quyt vn ny bng cch sp cc im theo tung y v xt tng t
nh trn. Ch , nu ti mi bc ta li gi th tc sp xp mi na th chi ph thut
ton s l O(Nlog2N), khng phi l thc s tt lm. Nhng mt cht, ta thy mi
na u c sp xp, hn na cch lm ca ta cng ang da vo t tng ca Merge
Sort, nh vy ti sao ta khng sp xp theo kiu Merge Sort ngay trong th tc quy
ca mnh. Nh vy hai cng vic u c s l ng thi. Chi ph cho bi ton ny
ging nh chi ph sp xp bng Merge Sort, t l vi NlogN.
Ci t thut ton ny khng kh nhng kh t m. Ta b qua bc sp xp tp
im theo honh x. u tin l th tc trn hai phn, tip theo l th tc quy tm
cp im gn nht.
procedure Merge(l, r: Integer);
var
t: Polygon;
Page 8 of 58

i, j, m, c: Integer;
begin
m := (l + r) div 2;
i := l;
j := m + 1;
for c := 1 to r l + 1 do
if (j > r) or (p[i].y < p[j].y) then
begin
t[c] := p[i];
Inc(i);
end
else
begin
t[c] := p[j];
Inc(j);
end;
for c := 1 to r l + 1 do p[l + c - 1] := t[c];
end;
procedure MinDist(l, r: Integer);
var
i, j, j1, m: Integer;
begin
if l = r then Exit;
m := (l + r) div 2;
MinDist(l, m);
MinDist(m + 1, r); {gi quy tm min trn hai phn}
j1 := m + 1;
for i := l to m do
begin
while (j1 <= r)and(p[j1].y p[i].y >= min) do Inc(j1);
for j := j1 to m do
if p[j].y p[i].y >= min then Break
else
if Dist(p[i], p[j]) < min then
begin {cp nht kt qu}
min := Dist(p[i], p[j]);
Result.x := p[i];
Result.y := p[j];
end;

Page 9 of 58

end;
Merge(l, r); {trn hai phn}
end;
Result l bin lu kt qu, c cu trc ging nh kiu Line. Bin min dng lu
khong cch nh nht tm c cho n thi im hin ti.

Page 10 of 58