Application of split Search Algorithm in fbreader

Source: Internet
Author: User

In fbreader, a piece of code implements the click screen and Text Selection Function.

Looking for the footprint, I found that there was a splitting search algorithm. I didn't look at it carefully, so I thought I would try to simulate it first.

The following code is available:

Import Java. util. arraylist; import Java. util. collections; import Java. util. comparator; import Java. util. iterator; public class binearysearch {class rect {intstartx; intstarty; intendx; intendy;} class point {intx; inty ;}@ suppresswarnings ("rawtypes ") class comparatorrects implements comparator {public int compare (Object arg0, object arg1) {rect rect1 = (rect) arg0; rect rect2 = (rect) arg1; If (rect2.endy> rect1.endy) {return-1;} elseif (rect2.endy = rect1.endy) {If (rect2.endx> rect1.endx) {return-1;} elseif (rect2.endx <rect1.endx) {return 1 ;} else {return 0 ;}} else {return 1 ;}} Private Static arraylist <rect> listrects = new arraylist <binearysearch. rect> (); Private Final Static intx_max = 10; // 10 rows private final static inty_max = 20; // 10 columns private final static intrect_width = 10; // width of the simulated square private final static intrect_height = 15; // height of the simulated square private final static intwordpadding = 2; // padding private final static intlinepadding = 3; // line spacing public static void main (string ARGs []) {binearysearch bsearch = new binearysearch (); bsearch. init (); system. out. println ("=========== size:" + listrects. size (); bsearch. printrectlist (); point = bsearch. new Point (); point. X = 50; point. y = 90; int matchindex = binarysearchrect (listrects, 0, listrects. size (), point); system. out. println ("============= index:" + matchindex);} @ suppresswarnings ("unchecked") Private void printrectlist () {collections. sort (listrects, new comparatorrects (); iterator <rect> iterator = listrects. iterator (); int COUNT = 0; while (iterator. hasnext () {rect = iterator. next (); system. out. println ("=" + Count + "= X1:" + rect. startx + "X2:" + rect. endx + "Y1:" + rect. starty + "Y2:" + rect. endy); count ++ ;}} private void Init () {for (INT I = 0; I <x_max; I ++) {for (Int J = 0; j <y_max; j ++) {If (j = 0) // if it is the first line {rect = new rect (); rect. startx = I * (rect_width + wordpadding); rect. endx = (I + 1) * (rect_width + wordpadding)-wordpadding; rect. starty = linepadding; rect. endy = rect_height + linepadding; listrects. add (rect);} else {rect = new rect (); rect. startx = I * (rect_width + wordpadding); rect. endx = (I + 1) * (rect_width + wordpadding)-wordpadding; rect. starty = J * (rect_height + linepadding); rect. endy = (J + 1) * rect_height + J * linepadding; listrects. add (rect );}}}} /*** improved the text location of the square where the coordinates are located ** @ Param listrects * @ Param low * @ Param high * @ Param point * @ return */static int binarysearchrect (arraylist <rect> listrects, int low, int high, point) {int mid; If (high <= low) Return-1; Mid = (low + high) >>> 1; // or Mid = low + (high-low)/2) if (listrects. get (MID ). starty> point. y) {return binarysearchrect (listrects, low, mid, point);} elseif (listrects. get (MID ). endy <point. y) {return binarysearchrect (listrects, Mid + 1, high, point);} else {// If (listrects. get (MID ). startx> point. x) {return binarysearchrect (listrects, low, mid, point);} elseif (listrects. get (MID ). endx <point. x) {return binarysearchrect (listrects, Mid + 1, high, point);} else {return mid ;}}}}

Print result:

==========size:200====0=====x1:0 x2:10 y1:3 y2:18====1=====x1:12 x2:22 y1:3 y2:18====2=====x1:24 x2:34 y1:3 y2:18====3=====x1:36 x2:46 y1:3 y2:18====4=====x1:48 x2:58 y1:3 y2:18====5=====x1:60 x2:70 y1:3 y2:18====6=====x1:72 x2:82 y1:3 y2:18====7=====x1:84 x2:94 y1:3 y2:18====8=====x1:96 x2:106 y1:3 y2:18====9=====x1:108 x2:118 y1:3 y2:18====10=====x1:0 x2:10 y1:18 y2:33====11=====x1:12 x2:22 y1:18 y2:33====12=====x1:24 x2:34 y1:18 y2:33====13=====x1:36 x2:46 y1:18 y2:33====14=====x1:48 x2:58 y1:18 y2:33====15=====x1:60 x2:70 y1:18 y2:33====16=====x1:72 x2:82 y1:18 y2:33====17=====x1:84 x2:94 y1:18 y2:33====18=====x1:96 x2:106 y1:18 y2:33====19=====x1:108 x2:118 y1:18 y2:33====20=====x1:0 x2:10 y1:36 y2:51====21=====x1:12 x2:22 y1:36 y2:51====22=====x1:24 x2:34 y1:36 y2:51====23=====x1:36 x2:46 y1:36 y2:51====24=====x1:48 x2:58 y1:36 y2:51====25=====x1:60 x2:70 y1:36 y2:51====26=====x1:72 x2:82 y1:36 y2:51====27=====x1:84 x2:94 y1:36 y2:51====28=====x1:96 x2:106 y1:36 y2:51====29=====x1:108 x2:118 y1:36 y2:51====30=====x1:0 x2:10 y1:54 y2:69====31=====x1:12 x2:22 y1:54 y2:69====32=====x1:24 x2:34 y1:54 y2:69====33=====x1:36 x2:46 y1:54 y2:69====34=====x1:48 x2:58 y1:54 y2:69====35=====x1:60 x2:70 y1:54 y2:69====36=====x1:72 x2:82 y1:54 y2:69====37=====x1:84 x2:94 y1:54 y2:69====38=====x1:96 x2:106 y1:54 y2:69====39=====x1:108 x2:118 y1:54 y2:69====40=====x1:0 x2:10 y1:72 y2:87====41=====x1:12 x2:22 y1:72 y2:87====42=====x1:24 x2:34 y1:72 y2:87====43=====x1:36 x2:46 y1:72 y2:87====44=====x1:48 x2:58 y1:72 y2:87====45=====x1:60 x2:70 y1:72 y2:87====46=====x1:72 x2:82 y1:72 y2:87====47=====x1:84 x2:94 y1:72 y2:87====48=====x1:96 x2:106 y1:72 y2:87====49=====x1:108 x2:118 y1:72 y2:87====50=====x1:0 x2:10 y1:90 y2:105====51=====x1:12 x2:22 y1:90 y2:105====52=====x1:24 x2:34 y1:90 y2:105====53=====x1:36 x2:46 y1:90 y2:105====54=====x1:48 x2:58 y1:90 y2:105====55=====x1:60 x2:70 y1:90 y2:105====56=====x1:72 x2:82 y1:90 y2:105====57=====x1:84 x2:94 y1:90 y2:105====58=====x1:96 x2:106 y1:90 y2:105====59=====x1:108 x2:118 y1:90 y2:105====60=====x1:0 x2:10 y1:108 y2:123====61=====x1:12 x2:22 y1:108 y2:123====62=====x1:24 x2:34 y1:108 y2:123====63=====x1:36 x2:46 y1:108 y2:123====64=====x1:48 x2:58 y1:108 y2:123====65=====x1:60 x2:70 y1:108 y2:123====66=====x1:72 x2:82 y1:108 y2:123====67=====x1:84 x2:94 y1:108 y2:123====68=====x1:96 x2:106 y1:108 y2:123====69=====x1:108 x2:118 y1:108 y2:123====70=====x1:0 x2:10 y1:126 y2:141====71=====x1:12 x2:22 y1:126 y2:141====72=====x1:24 x2:34 y1:126 y2:141====73=====x1:36 x2:46 y1:126 y2:141====74=====x1:48 x2:58 y1:126 y2:141====75=====x1:60 x2:70 y1:126 y2:141====76=====x1:72 x2:82 y1:126 y2:141====77=====x1:84 x2:94 y1:126 y2:141====78=====x1:96 x2:106 y1:126 y2:141====79=====x1:108 x2:118 y1:126 y2:141====80=====x1:0 x2:10 y1:144 y2:159====81=====x1:12 x2:22 y1:144 y2:159====82=====x1:24 x2:34 y1:144 y2:159====83=====x1:36 x2:46 y1:144 y2:159====84=====x1:48 x2:58 y1:144 y2:159====85=====x1:60 x2:70 y1:144 y2:159====86=====x1:72 x2:82 y1:144 y2:159====87=====x1:84 x2:94 y1:144 y2:159====88=====x1:96 x2:106 y1:144 y2:159====89=====x1:108 x2:118 y1:144 y2:159====90=====x1:0 x2:10 y1:162 y2:177====91=====x1:12 x2:22 y1:162 y2:177====92=====x1:24 x2:34 y1:162 y2:177====93=====x1:36 x2:46 y1:162 y2:177====94=====x1:48 x2:58 y1:162 y2:177====95=====x1:60 x2:70 y1:162 y2:177====96=====x1:72 x2:82 y1:162 y2:177====97=====x1:84 x2:94 y1:162 y2:177====98=====x1:96 x2:106 y1:162 y2:177====99=====x1:108 x2:118 y1:162 y2:177====100=====x1:0 x2:10 y1:180 y2:195====101=====x1:12 x2:22 y1:180 y2:195====102=====x1:24 x2:34 y1:180 y2:195====103=====x1:36 x2:46 y1:180 y2:195====104=====x1:48 x2:58 y1:180 y2:195====105=====x1:60 x2:70 y1:180 y2:195====106=====x1:72 x2:82 y1:180 y2:195====107=====x1:84 x2:94 y1:180 y2:195====108=====x1:96 x2:106 y1:180 y2:195====109=====x1:108 x2:118 y1:180 y2:195====110=====x1:0 x2:10 y1:198 y2:213====111=====x1:12 x2:22 y1:198 y2:213====112=====x1:24 x2:34 y1:198 y2:213====113=====x1:36 x2:46 y1:198 y2:213====114=====x1:48 x2:58 y1:198 y2:213====115=====x1:60 x2:70 y1:198 y2:213====116=====x1:72 x2:82 y1:198 y2:213====117=====x1:84 x2:94 y1:198 y2:213====118=====x1:96 x2:106 y1:198 y2:213====119=====x1:108 x2:118 y1:198 y2:213====120=====x1:0 x2:10 y1:216 y2:231====121=====x1:12 x2:22 y1:216 y2:231====122=====x1:24 x2:34 y1:216 y2:231====123=====x1:36 x2:46 y1:216 y2:231====124=====x1:48 x2:58 y1:216 y2:231====125=====x1:60 x2:70 y1:216 y2:231====126=====x1:72 x2:82 y1:216 y2:231====127=====x1:84 x2:94 y1:216 y2:231====128=====x1:96 x2:106 y1:216 y2:231====129=====x1:108 x2:118 y1:216 y2:231====130=====x1:0 x2:10 y1:234 y2:249====131=====x1:12 x2:22 y1:234 y2:249====132=====x1:24 x2:34 y1:234 y2:249====133=====x1:36 x2:46 y1:234 y2:249====134=====x1:48 x2:58 y1:234 y2:249====135=====x1:60 x2:70 y1:234 y2:249====136=====x1:72 x2:82 y1:234 y2:249====137=====x1:84 x2:94 y1:234 y2:249====138=====x1:96 x2:106 y1:234 y2:249====139=====x1:108 x2:118 y1:234 y2:249====140=====x1:0 x2:10 y1:252 y2:267====141=====x1:12 x2:22 y1:252 y2:267====142=====x1:24 x2:34 y1:252 y2:267====143=====x1:36 x2:46 y1:252 y2:267====144=====x1:48 x2:58 y1:252 y2:267====145=====x1:60 x2:70 y1:252 y2:267====146=====x1:72 x2:82 y1:252 y2:267====147=====x1:84 x2:94 y1:252 y2:267====148=====x1:96 x2:106 y1:252 y2:267====149=====x1:108 x2:118 y1:252 y2:267====150=====x1:0 x2:10 y1:270 y2:285====151=====x1:12 x2:22 y1:270 y2:285====152=====x1:24 x2:34 y1:270 y2:285====153=====x1:36 x2:46 y1:270 y2:285====154=====x1:48 x2:58 y1:270 y2:285====155=====x1:60 x2:70 y1:270 y2:285====156=====x1:72 x2:82 y1:270 y2:285====157=====x1:84 x2:94 y1:270 y2:285====158=====x1:96 x2:106 y1:270 y2:285====159=====x1:108 x2:118 y1:270 y2:285====160=====x1:0 x2:10 y1:288 y2:303====161=====x1:12 x2:22 y1:288 y2:303====162=====x1:24 x2:34 y1:288 y2:303====163=====x1:36 x2:46 y1:288 y2:303====164=====x1:48 x2:58 y1:288 y2:303====165=====x1:60 x2:70 y1:288 y2:303====166=====x1:72 x2:82 y1:288 y2:303====167=====x1:84 x2:94 y1:288 y2:303====168=====x1:96 x2:106 y1:288 y2:303====169=====x1:108 x2:118 y1:288 y2:303====170=====x1:0 x2:10 y1:306 y2:321====171=====x1:12 x2:22 y1:306 y2:321====172=====x1:24 x2:34 y1:306 y2:321====173=====x1:36 x2:46 y1:306 y2:321====174=====x1:48 x2:58 y1:306 y2:321====175=====x1:60 x2:70 y1:306 y2:321====176=====x1:72 x2:82 y1:306 y2:321====177=====x1:84 x2:94 y1:306 y2:321====178=====x1:96 x2:106 y1:306 y2:321====179=====x1:108 x2:118 y1:306 y2:321====180=====x1:0 x2:10 y1:324 y2:339====181=====x1:12 x2:22 y1:324 y2:339====182=====x1:24 x2:34 y1:324 y2:339====183=====x1:36 x2:46 y1:324 y2:339====184=====x1:48 x2:58 y1:324 y2:339====185=====x1:60 x2:70 y1:324 y2:339====186=====x1:72 x2:82 y1:324 y2:339====187=====x1:84 x2:94 y1:324 y2:339====188=====x1:96 x2:106 y1:324 y2:339====189=====x1:108 x2:118 y1:324 y2:339====190=====x1:0 x2:10 y1:342 y2:357====191=====x1:12 x2:22 y1:342 y2:357====192=====x1:24 x2:34 y1:342 y2:357====193=====x1:36 x2:46 y1:342 y2:357====194=====x1:48 x2:58 y1:342 y2:357====195=====x1:60 x2:70 y1:342 y2:357====196=====x1:72 x2:82 y1:342 y2:357====197=====x1:84 x2:94 y1:342 y2:357====198=====x1:96 x2:106 y1:342 y2:357====199=====x1:108 x2:118 y1:342 y2:357============index:54

Conclusion:
Coordinates: (50, 90)

====54=====x1:48 x2:58 y1:90 y2:105

It is much better than the original traversal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.