确定一个点是否在一条线上

时间:2013-08-05 12:35:23

标签: java android

我一直试图确定一个点是否在一条线上。该行使用Pdftron在Android应用程序的pdf文档上绘制。

我一直试图解决这个问题,因为2周后,请帮帮我。

以下是代码

mPDFView.setDoc(doc);// doc is a PDF document.

mPDFView.setOnTouchListener(new OnTouchListener() {//mPDFView is a third party layout component created by Pdftron

    @Override
    public boolean onTouch(View v, MotionEvent arg1) {
        if(arg1.getAction() == MotionEvent.ACTION_DOWN){
          //record the start time
            startTime = arg1.getEventTime();
            arg1.getX();
            arg1.getY();
        }
        else if(arg1.getAction() == MotionEvent.ACTION_UP){
          //record the end time
            endTime = arg1.getEventTime();
        }
        if(endTime - startTime > 1000){
            xValue=arg1.getX();
            yValue=arg1.getY();
            int page_num = 1;
            try{
            for (PageIterator itr = doc.getPageIterator(); itr.hasNext();) {
                   System.out.println("Page " + (page_num++) + ": ");
               Page page = (Page) (itr.next());
                   int num_annots;
               num_annots = page.getNumAnnots();
               for (int i = 0; i < num_annots; ++i) {
                 Annot annot = page.getAnnot(i); 
                 if (annot.isValid() == false)
                    continue;
                 double[] bbox = annot.getRect().get();
                     double x1 = bbox[0];  
                 double y1 = bbox[1];
                 double x2 = bbox[2];
                 double y2 = bbox[3];                              
                 double slope_line = (y1-y2)/(x1-x2);
        boolean point_status = methodLineEquation(slope_line, xValue, yValue, x1, y1);
                }
              }
           }catch(Exception e){
            e.printStackTrace();
           }
        }
    return false;
                    }
   });

public boolean methodLineEquation(double slope,double newX,double newY,double x1,double y1){
            double y=newY;
        double x=newX;
            double result = (y - y1)-(slope*(x-x1));// Line Equation
            if(result==0){
               Toast.makeText(getApplicationContext(), "Point on line", 500).show();
                return true;
            }
            else{
               Toast.makeText(getApplicationContext(), "Point not on line",500).show();
                return false;
            }
 }

注意:大括号可能不合适,因为我删除了一些不必要的代码行。请忽略。

0 个答案:

没有答案