Closest Pair of Points

November 17, 2022 | Author: Anonymous | Category: N/A
Share Embed Donate


Short Description

Download Closest Pair of Points...

Description

 

Fcasmst Rn`r ai Ra`jts us`jl H`v`hm njh Fajqumr nclar`tke \m nrm l`vmj nj nrrny ai j pa`jts `j tkm t km pcnjm, njh tkm pradcme `s ta i`jh aut tkm fcasmst pn`r ai  pa`jts `j tkm nrrny. Xk`s pradcme nr`sms `j n juedmr ai nppc`fnt`ajs. nppc`fnt`ajs. Iar mxnepcm, `j n`r-trnii`f fajtrac, yau eny wnjt ta eaj`tar pcnjms tknt faem taa fcasm talmtkmr, s`jfm tk`s eny `jh`fntm n pass`dcm facc`s`aj. Tmfncc tkm iaccaw`jl iareucn iar h`stnjfm dmtwmmj twa pa`jts p njh q. Xkm Drutm iarfm sacut`aj `s A(jZ4), faeputm tkm h`stnjfm dmtwmmj mnfk pn`r njh rmturj tkm senccmst. \m fnj fncfucntm tkm senccmst h`stnjfm `j A(jCalj) t`em us`jl H`v`hm njh Fajqumr strntmly. @j tk`s past, n A(j x (Calj)Z4) nppranfk `s h`sfussmh. \m w`cc dm h`sfuss`jl n A(jCalj) nppranfk `j n smpnrntm past. Nclar`tke 

Iaccaw`jl nrm tkm hmtn`cmh stmps ai n A(j (Calj)Z4) nclar`tke.  @jput7 Nj nrrny ai j pa`jts R_U  Autput7 Xkm senccmst h`stnjfm dmtwmmj twa pa`jts `j tkm t km l`vmj nrrny.

Ns n prm-prafmss`jl stmp, tkm `jput nrrny `s sartmh nffarh`jl ta x faarh`jntms. >) I`jh tkm e`hhcm pa`jt `j tkm sartmh nrrny, wm fnj tnom R_j/4U  ns e`hhcm pa`jt. 4) H`v`hm tkm l`vmj nrrny `j twa kncvms. Xkm i`rst sudnrrny s udnrrny fajtn`js pa`jts irae R_2U ta R_j/4U. Xkm smfajh

sudnrrny fajtn`js pa`jts irae R_j/4+>U ta R_j->U. 6) Tmfurs`vmcy i`jh tkm senccmst h`stnjfms h`st njfms `j datk sudnrrnys. Cmt tkm h`stnjfms dm hc njh hr. I`jh tkm

e`j`eue ai hc njh hr. Cmt tkm e`j`eue dm h.

8) Irae tkm ndavm 6 stmps, wm knvm nj uppmr daujh h ai e`j`eue h`stnjfm. Jaw wm jmmh ta fajs`hmr tkm

 pn`rs sufk tknt ajm pa`jt `j pn`r `s irae tkm cmit knci njh tkm atkmr `s irae tkm r`lkt r`lkt knci. Fajs`hmr tkm vmrt`fnc c`jm pnss`jl tkraulk R_j/4U njh i`jh ncc pa`jts wkasm x faarh`jntm `s fcasmr tknj h ta tkm e`hhcm vmrt`fnc c`jm.

 

Du`ch nj nrrny str`p_U ai ncc sufk pa`jts.

=) Vart tkm nrrny str`p_U nffarh`jl ta y faarh`jntms. Xk`s stmp `s A(jCalj). @t fnj dm apt`e`zmh ta A(j) dy

rmfurs`vmcy sart`jl njh emrl`jl. 9) I`jh tkm senccmst h`stnjfm `j str`p_U. Xk`s `s tr`foy. Irae tkm i`rst caao, `t smmes ta dm n A(jZ4) stmp, dut `t

`s nftunccy A(j). @t fnj dm pravmh lmaemtr`fnccy lmaemtr`fncc y tknt iar mvmry pa`jt `j tkm str`p, wm ajcy jmmh ta fkmfo nt east < pa`jts nitmr `t (jatm tknt str`p `s sartmh nffarh`jl ta P faarh`jntm). Vmm  Vmm  tk`s i ar ar earm njncys`s. , p4)7

 

  rmturj entk.sqrt((p>.x - p4.x) * (p>.x - p4.x) + (p>.y - p4.y) * (p>.y - p4.y)) # N Drutm Iarfm emtkah ta rmturj tkm senccmst h`stnjfm dmtwmmj twa pa`jts `j R_U ai s`zm j hmi drutmIarfm(R, j)7 e`jQvnc 5 icant('`ji') iar ` `j rnjlm(j)7 iar g `j rnjlm(` + >, j)7 `i h`st(R_`U, R_gU) 3 e`jQvnc7 e`jQvnc 5 h`st(R_`U, R_gU) rmturj e`jQvnc # N ut`c`ty iujft`aj ta i`jh tkm h`stnjfm dmtwmmj tkm fcasmst pa`jts ai str`p ai l`vmj s`zm. Ncc  pa`jts `j str`p_U nrm sartmh nffarh`jt ta y faarh`jntm. Xkmy ncc knvm nj uppmr uppmr daujh aj e`j`eue h`stnjfm ns h.Jatm tknt tk`s emtkah smmes ta dm n A(jZ4) emtkah, dut `t's n A(j) emtkah ns tkm `jjmr caap rujs nt east 9 t`ems hmi str`pFcasmst(str`p, s`zm, h)7 # @j`t`nc`zm tkm e`j`eue h`stnjfm ns h e`jQvnc 5 h # R`fo ncc pa`jts ajm dy ajm njh try tkm jmxt pa`jts t`cc t`c c tkm h`iimrmjfm dmtwmmj y faarh`jntms `s senccmr tknj h. Xk`s `s n pravmj inft tknt tk`s caap rujs nt east 9 t`ems iar ` `j rnjlm(s`zm)7  g 5 ` + > wk`cm g 3 s`zm njh (str`p_gU.y str`p_`U.y) 3 e`jQvnc7

 

 

e`jQvnc 5 h`st(str`p_`U, str`p_gU)  g +5 > rmturj e`jQvnc

# N rmfurs`vm iujft`aj ta i`jh tkm senccmst h`stnjfm. Xkm nrrny R fajtn`js ncc pa`jts sartmh nffarh`jl ta x faarh`jntm hmi fcasmst]t`c(R, ^, j)7 # @i tkmrm nrm 4 ar 6 pa`jts, tkmj usm drutm iarfm `i j 35 67 rmturj drutmIarfm(R, j) # I`jh tkm e`hhcm pa`jt e`h 5 j // 4 e`hRa`jt 5 R_e`hU #ommp n fapy ai cmit njh r`lkt drnjfk Rc 5 R_7e`hU Rr 5 R_e`h7U # Fajs`hmr tkm vmrt`fnc c`jm pnss`jl tkraulk tkm e`hhcm pa`jt fncfucntm tkm senccmst h`stnjfm hc aj cmit ai e`hhcm pa`jt njh hr aj r`lkt s`hm hc 5 fcasmst]t`c(Rc, ^, e`h) hr 5 fcasmst]t`c(Rr, ^, j - e`h) # I`jh tkm senccmr ai twa h`stnjfms h 5 e`j(hc, hr) # Du`ch nj nrrny str`p_U tknt fajtn`js pa`jts fcasm (fcasmr tknj h) ta tkm c`jm pnss`jl tkraulk tkm e`hhcm pa`jt str`pR 5 _U

 

  str`p^ 5 _U cr 5 Rc + Rr iar ` `j rnjlm(j)7 `i nds(cr_`U.x - e`hRa`jt.x) 3 h7 str`pR.nppmjh(cr_`U) `i nds(^_`U.x - e`hRa`jt.x) 3 h7 str`p^.nppmjh(^_`U) str`pR.sart(omy 5 cnedhn pa`jt7 pa`jt.y) #3-- TM^]@TMH e`jQn 5 e`j(h, str`pFcasmst(str`pR, cmj(str`pR), h)) e`jQd 5 e`j(h, str`pFcasmst(str`p^, cmj(str`p^), h)) # I`jh tkm smci.fcasmst pa`jts `j str`p. str `p. Tmturj tkm e`j`eue ai h njh smci.fcasmst h`stnjfm `s str`p_U rmturj e`j(e`jQn,e`jQd) # Xkm en`j iujft`aj tknt i`jhs tkm senccmst h`stnjfm. Xk`s emtkah en`jcy usms fcasmst]t`c() hmi fcasmst(R, j)7 R.sart(omy 5 cnedhn pa`jt7 pa`jt.x)

^ 5 fapy.hmmpfapy(R) ^.sart(omy 5 cnedhn pa`jt7 pa`jt.y) # ]sm rmfurs`vm iujft`aj fcasmst]t`c() ta i`jh tkm senccmst h`stnjfm rmturj fcasmst]t`c(R, ^, j) # Hr`vmr fahm R 5 _Ra`jt(4, 6), Ra`jt(>4, 62), Ra`jt(82, =2), Ra`jt(=, >),

 

 

Ra`jt(>4, >2), Ra`jt(6, 8)U

j 5 cmj(R)  pr`jt("Xkm senccmst h`stnjfm `s", fcasmst(R, j))

Autput7 

Xkm senccmst h`stnjfm `s >.8>84>8 X`em Faepcmx`ty Cmt X`em faepcmx`ty ai ndavm nclar`tke dm X(j). Cmt us nssuem tknt wm usm n A(jCalj)

sart`jl nclar`tke. Xkm ndavm nclar`tke h`v`hms ncc pa`jts `j twa smts njh rmfurs`vmcy rmf urs`vmcy fnccs iar twa smts. Nitmr h`v`h`jl, `t i`jhs tkm str`p `j A(j) t`em, sarts tkm str`p `j A(jCalj) t`em njh i`jnccy i`jhs tkm fcasmst pa`jts `j str`p `j A(j) t`em. Va X(j) fnj mxprmssmh ns iaccaws X(j) 5 4X(j/4) + A(j) + A(jCalj) + A(j) X(j) 5 4X(j/4) + A(jCalj) X(j) 5 X(j x Calj x Calj) Jatms  >) X`em faepcmx`ty fnj dm `epravmh ta A(jCalj) dy apt`e`z`jl stmp = ai tkm ndavm nclar`tke. \m w`cc

saaj dm h`sfuss`jl tkm apt`e`zmh sacut`aj `j n smpnrntm past. 4) Xkm fahm i`jhs senccmst h`stnjfm. @t fnj dm mns`cy eah`i`mh ta i`jh tkm pa`jts w`tk tkm t km senccmst h`stnjfm. 6) Xkm fahm usms qu`fo sart wk`fk fnj dm A(jZ4) `j tkm warst fnsm. Xa knvm tkm uppmr daujh ns A(j

(Calj)Z4), n A(jCalj) sart`jl nclar`tke c`om emrlm sart ar kmnp sart fnj dm usmh Fcasmst Rn`r ai Ra`jts | A(jcalj) @epcmemjtnt`aj  





 

H`ii`fucty Cmvmc 7  7 Knrh   Cnst ]phntmh 7 >= Npr, 4242 \m nrm l`vmj nj nrrny ai j pa`jts `j tkm t km pcnjm, njh tkm pradcme `s ta i`jh aut tkm fcasmst pn`r ai pa`jts `j tkm nrrny. Xk`s pradcme nr`sms `j n juedmr ai nppc`fnt`ajs. nppc`fnt` ajs. Iar mxnepcm, `j n`r-trnii`f fajtrac, yau eny wnjt ta eaj`tar pcnjms tknt faem taa fcasm talmtkmr, s`jfm tk`s eny `jh`fntm n pass`dcm facc`s`aj. Tmfncc tkm iaccaw`jl iareucn iar h`stnjfm dmtwmmj twa pa`jts p njh q. . Xkm t`em faepcmx`ty ai tkm `epcmemjtnt`aj prav`hmh `j tkm prmv`aus past `s A(j (Calj)Z4). @j tk`s past, wm h`sfuss `epcmemjtnt`aj w`tk t`em faepcmx`ty ns A(jCalj). Iaccaw`jl `s n rmfnp ai tkm nclar`tke h`sfussmh `j tkm t km prmv`aus past. >) \m sart ncc pa`jts nffarh`jl ta x faarh`jntms. 4) H`v`hm ncc pa`jts `j twa kncvms. 6) Tmfurs`vmcy i`jh tkm senccmst h`stnjfms `j datk sudnrrnys.

 

 

8) Xnom tkm e`j`eue ai twa senccmst h`stnjfms. Cmt tkm e`j`eue dm h. =) Frmntm nj nrrny str`p_U tknt starms ncc pa`jts wk`fk nrm nt east h h`stnjfm nwny irae tkm e`hhcm c`jm

h`v`h`jl tkm twa smts. 9) I`jh tkm senccmst h`stnjfm `j str`p_U. 5 (Ra`jt *)n, *)n, *p4 5 (Ra`jt *)d? rmturj (p>-:x - p4-:x)? } // Jmmhmh ta sart nrrny ai pa`jts nffarh`jl ta P faarh`jntm `jt faepnrmP(fajst va`h* n, fajst va`h* d) { Ra`jt *p> 5 (Ra`jt *)n, *p4 5 (Ra`jt (Ra`jt *)d? rmturj (p>-:y - p4-:y)? } // N ut`c`ty iujft`aj ta i`jh tkm h`stnjfm dmtwmmj twa pa`jts icant h`st(Ra`jt p>, Ra`jt p4) { rmturj sqrt( (p>.x - p4.x)*(p>.x - p4.x) + (p>.y - p4.y)*(p>.y - p4.y) )? }

// N Drutm Iarfm emtkah ta rmturj tkm senccmst h`stnjfm dmtwmmj twa pa`jts `j R_U ai s`zm j icant drutmIarfm(Ra`jt R_U, `jt j)

 

{ icant e`j 5 ICXQEN[? iar (`jt ` 5 2? ` 3 j? ++`) iar (`jt g 5 `+>? g 3 j? ++g) `i (h`st(R_`U, R_gU) 3 e`j) e`j 5 h`st(R_`U, R_gU)? rmturj e`j? } // N ut`c`ty iujft`aj ta i`jh n e`j`eue ai twa icant vncums icant e`j(icant x, icant y) { rmturj (x 3 y)1 x 7 y? } // N ut`c`ty iujft`aj ta i`jh tkm h`stnjfm dmtwmmj tkm fcasmst pa`jts ai str`p ai n l`vmj s`zm. Ncc  pa`jts `j str`p_U nrm sartmh nffarh`jl ta y faarh`jntm. Xkmy ncc knvm nj uppmr daujh aj e`j`eue h`stnjfm ns h. Jatm tknt tk`s emtkah smmes ta dm n A(jZ4) emtkah, dut dut `t's n A(j) emtkah ns tkm `jjmr caap rujs nt east 9 t`ems

icant str`pFcasmst(Ra`jt str`p_U, `jt s`zm, icant h) { icant e`j 5 h? // @j`t`nc`zm tkm e`j`eue h`stnjfm ns h // R`fo ncc pa`jts ajm dy ajm njh try tkm jmxt pa`jts t`cc t `cc tkm h`iimrmjfm dmtwmmj y faarh`jntms `s senccmr tknj h. Xk`s `s n pravmj inft tknt tk`s caap rujs nt east 9 t`ems iar (`jt ` 5 2? ` 3 s`zm? ++`) iar (`jt g 5 `+>? g 3 s`zm && (str`p_gU.y - str`p_`U.y) 3 e`j? ++g) `i (h`st(str`p_`U,str`p_gU) 3 e`j)

 

 

e`j 5 h`st(str`p_`U, str`p_gU)? rmturj e`j?

} // N rmfurs`vm iujft`aj ta i`jh tkm senccmst h`stnjfm. Xkm nrrny Rx fajtn`js ncc pa`jts sartmh nffarh`jl ta x faarh`jntms njh Ry fajtn`js ncc pa`jts sartmh nffarh`jl ta y faarh`jntms icant fcasmst]t`c(Ra`jt Rx_U, Ra`jt Ry_U, `jt j) { // @i tkmrm nrm 4 ar 6 pa`jts, pa`jts , tkmj usm drutm iarfm `i (j 35 6) rmturj drutmIarfm(Rx, j)? // I`jh tkm e`hhcm pa`jt `jt e`h 5 j/4? Ra`jt e`hRa`jt 5 Rx_e`hU? // H`v`hm pa`jts `j y sartmh nrrny nraujh tkm vmrt`fnc c`jm. Nssuept`aj7 Ncc x faarh`jntms nrm h`st`jft. Ra`jt Ryc_e`hU? Ryc_e`hU? // y sartmh pa`jts aj cmit ai vmrt`fnc c`jm Ra`jt Ryr_j-e`hU? // y sartmh pa`jts pa`jts aj r`lkt ai vmrt`fnc c`jm `jt c` 5 2, r` 5 2? // `jhmxms ai cmit njh r`lkt sudnrrnys iar (`jt ` 5 2? ` 3 j? `++) { `i (Ry_`U.x 35 e`hRa`jt.x && c`3e`h) Ryc_c`++U 5 Ry_`U? mcsm Ryr_r`++U 5 Ry_`U?

 

  } // Fajs`hmr tkm vmrt`fnc c`jm pnss`jl tkraulk tkm e`hhcm pa`jt fncfucntm tkm senccmst h`stnjfm hc aj cmit ai e`hhcm pa`jt njh hr aj r`lkt s`hm icant hc 5 fcasmst]t`c(Rx, Ryc, e`h)? icant hr 5 fcasmst]t`c(Rx + e`h, Ryr, j-e`h)? // I`jh tkm senccmr ai twa h`stnjfms icant h 5 e`j(hc, hr)? // Du`ch nj nrrny str`p_U tknt fajtn`js pa`jts fcasm (fcasmr tknj h) ta tkm c`jm pnss`jl tkraulk tkm e`hhcm pa`jt Ra`jt str`p_jU? `jt g 5 2? iar (`jt ` 5 2? ` 3 j? `++) `i (nds(Ry_`U.x - e`hRa`jt.x) 3 h) str`p_gU 5 Ry_`U, g++? // I`jh tkm fcasmst pa`jts pa`jts `j str`p. Tmturj tkm e`j`eue ai h njh fcasmst h`stnjfm `s str`p_U rmturj str`pFcasmst(str`p, g, h)? } // Xkm en`j iujft`aj tknt i`jhs tkm senccmst h`stnjfm Xk`s emtkah en`jcy usms fcasmst]t`c() icant fcasmst(Ra`jt R_U, `jt j) { Ra`jt Rx_jU? Ra`jt Ry_jU? iar (`jt ` 5 2? ` 3 j? `++) {

 

 

Rx_`U 5 R_`U? Ry_`U 5 R_`U? } qsart(Rx, j, s`zmai(Ra`jt), faepnrm[)? qsart(Ry, j, s`zmai(Ra`jt), faepnrmP)? // ]sm rmfurs`vm iujft`aj fcasmst]t`c() ta i`jh tkm senccmst h`stnjfm rmturj fcasmst]t`c(Rx, Ry, j)?

} // Hr`vmr pralrne ta tmst ndavm iujft`ajs `jt en`j() { Ra`jt R_U 5 {{4, 6}, {>4, 62}, {82, =2}, {=, >}, {>4, >2}, {6, 8}}? `jt j 5 s`zmai(R) / s`zmai(R_2U)? faut 33 "Xkm senccmst h`stnjfm `s " 33 fcasmst(R, j)? rmturj 2? }

Autput7 Xkm senccmst h`stnjfm `s >.8>84> X`em Faepcmx`ty7Cmt X`em faepcmx`ty ai ndavm nclar`tke dm X(j). Cmt us nssuem tknt wm usm n A(jCalj)

sart`jl nclar`tke. Xkm ndavm nclar`tke h`v`hms ncc pa`jts `j twa smts njh rmfurs`vmcy rmf urs`vmcy fnccs iar twa smts. Nitmr h`v`h`jl, `t i`jhs tkm str`p `j A(j) t`em. Ncsa, `t tnoms A(j) t`em ta h`v`hm tkm Ry nrrny nrrn y nraujh tkm e`h vmrt`fnc c`jm. I`jnccy i`jhs tkm fcasmst pa`jts `j str`p `j A(j) t`em. Va X(j) fnj dm mxprmssmh ns iaccaws X(j) 5 4X(j/4) + A(j) + A(j) + A(j) X(j) 5 4X(j/4) + A(j) X(j) 5 X(jCalj)

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF