拆分查找算法在FBreader中的应用

来源:互联网 发布:自学java三个月找工作 编辑:程序博客网 时间:2024/06/13 15:05

在FBreader中,有一段代码是实现了关于点击屏幕,然后选取文字的功能.

寻着足迹,发现有一个拆分查找算法.没细看,于是想想自己先模拟实现试试.

于是有如下代码:

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行private final static intY_Max= 20;// 10列private final static intrect_width= 10;// 模拟方块的宽度private final static intrect_height= 15;// 模拟方块的高度private final static intwordPadding= 2;// 字间距private final static intlinePadding= 3;// 行间距public static void main(String args[]){BinearySearch bSearch = new BinearySearch();bSearch.init();System.out.println("==========size:" + listRects.size());bSearch.printRectList();Point 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 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) // 如果是第一行{Rect 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 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);}}}}/** * 改进用来搜索坐标所在的方块的文字位置 *  * @param listRects * @param low * @param high * @param point * @return */static int BinarySearchRect(ArrayList<Rect> listRects, int low, int high,Point 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;}}}}


打印结果:

==========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

结论:
坐标:(50,90)刚好在

====54=====x1:48 x2:58 y1:90 y2:105
里,比从头遍历要好很多.