根据屏幕上一条线求出线两侧形成的路的Path

根据屏幕上的一条线,求出该线左右两侧的点,可以使用这些点组成一条路。

private void points2path() {
    /**
    *A(a,b) B(m,n) BC = L
    *
    *x1= m - (b-n) /√[(a-m)^2+(b-n)^2]
    *x2= m + (b-n) /√[(a-m)^2+(b-n)^2]
    *y1 = n + L(a-m) /√[(a-m)^2+(b-n)^2]
    *y2 = n - L(a-m) /√[(a-m)^2+(b-n)^2]
     */
    //获取所有计算后的点的集合
    
    
    int path_width = 30;
    
    //获取一侧点的集合
    mLeftPoints.clear();
    for(int i=0;i<mPoints.size();i++){
        Point currentPoint = mPoints.get(i);
        Point secondPoint;
        int m = currentPoint.x;
        int n = currentPoint.y;
        if(i==mPoints.size()-1){
            secondPoint= mPoints.get(i-1);
        }else{
            secondPoint= mPoints.get(i+1);
        }
        int a;
        int b;
        a = secondPoint.x;
        b = secondPoint.y;
        
        int x;
        int y;
        
        /**
        *C1(x,y) c2(x3,y3) A(x2,y2) B(x1,y1) BC=a
        *
        *x=x1-a*sin{arctan[(y2-y1)/(x2-x1)]}
        *y=y1+a*cos{arctan[(y2-y1)/(x2-x1)]}
        *x3=x1+a*sin{arctan[(y2-y1)/(x2-x1)]} 
        *y3=y1- a*cos{arctan[(y2-y1)/(x2-x1)]}
         */
        
        //m,n为B,a,b为A
        int x1=m,y1=n;
        int x2=a,y2=b;
        if(y2==y1){
            x = (int) (m - (b-n) / Math.sqrt(Math.pow((a-m),2)+Math.pow((b-n),2)));
            y = (int) (n + (path_width/2)*(a-m) /Math.sqrt(Math.pow((a-m),2)+Math.pow((b-n),2)));
        }else if(x2==x1){
            x = x1+(path_width/2);
            y = y1;
        }else if(x2<x1 && y2>y1){
            x=(int) (x1+(path_width/2)*Math.sin(Math.atan((y2-y1)/(x2-x1))));
            y=(int) (y1-(path_width/2)*Math.cos(Math.atan((y2-y1)/(x2-x1))));               
        }else{
            x=(int) (x1-(path_width/2)*Math.sin(Math.atan((y2-y1)/(x2-x1))));
            y=(int) (y1+(path_width/2)*Math.cos(Math.atan((y2-y1)/(x2-x1))));
        }
        
        mLeftPoints.add(new Point(x, y));
    }
    
    //获取另一侧点的集合
    mRightPoints.clear();
    for(int i=0;i<mPoints.size();i++){
        Point currentPoint = mPoints.get(i);
        Point secondPoint;
        int m = currentPoint.x;
        int n = currentPoint.y;
        if(i==mPoints.size()-1){
            secondPoint= mPoints.get(i-1);
        }else{
            secondPoint= mPoints.get(i+1);
        }
        int a;
        int b;
        a = secondPoint.x;
        b = secondPoint.y;
        
        int x;
        int y;
        
        
        int x1=m,y1=n;
        int x2=a,y2=b;
        if(y2==y1){
            x = (int) (m + (b-n) / Math.sqrt(Math.pow((a-m),2)+Math.pow((b-n),2)));
            y = (int) (n - (path_width/2)*(a-m) /Math.sqrt(Math.pow((a-m),2)+Math.pow((b-n),2)));
        }else if(x2==x1){
            x = x1-(path_width/2);
            y = y1;
        }else if(x2<x1 && y2>y1){
            x=(int) (x1-(path_width/2)*Math.sin(Math.atan((y2-y1)/(x2-x1))));
            y=(int) (y1+(path_width/2)*Math.cos(Math.atan((y2-y1)/(x2-x1))));               
        }else{
            x=(int) (x1+(path_width/2)*Math.sin(Math.atan((y2-y1)/(x2-x1))));
            y=(int) (y1-(path_width/2)*Math.cos(Math.atan((y2-y1)/(x2-x1))));
        }
        
        mRightPoints.add(new Point(x, y));
    }
    
    //TODO 由于最后一个点的坐标是反向计算出来的,因此它的left和right是反的,在此做交换处理
    Point temp = mLeftPoints.remove(mLeftPoints.size()-1);
    mLeftPoints.add(mRightPoints.remove(mRightPoints.size()-1));
    mRightPoints.add(temp);
    
    mPointsPath.clear();
    mPointsPath.addAll(mLeftPoints);
    mPointsPath.addAll(mRightPoints);
    
    //将点集合转成成矩形Path
    mRectPath.reset();
    Point point = mPointsPath.get(0);
    mRectPath.moveTo(point.x,point.y);
    for(int i=1;i<mPointsPath.size();i++){
        if(i<mLeftPoints.size()){
            point = mLeftPoints.get(i);
        }else{
            point = mRightPoints.get(mRightPoints.size()-(i-mLeftPoints.size()+1));
        }
        mRectPath.lineTo(point.x, point.y);
    }
    
    
    //将点集合转换成Path集合,Path集合个数为原始点的个数减一(此处可表示为left或者right集合长度减一)
    mPaths.clear();
    for(int i=0;i<mLeftPoints.size()-1;i++){
        Path path = new Path();
        Point leftCurrentPoint = mLeftPoints.get(i);
        Point leftNextPoint = mLeftPoints.get(i+1);
        Point rightCurrentPoint = mRightPoints.get(i);
        Point rightNextPoint = mRightPoints.get(i+1);
        path.moveTo(leftCurrentPoint.x,leftCurrentPoint.y);
        path.lineTo(leftNextPoint.x,leftNextPoint.y);
        path.lineTo(rightNextPoint.x,rightNextPoint.y);
        path.lineTo(rightCurrentPoint.x,rightCurrentPoint.y);
        path.close();
        mPaths.add(path);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 慢慢的写了一些文章,每次都投到首页,可是……你懂的,可能是时机还不到吧,每一篇都被拒了。 于是,干脆就不投首页而只...
    火火烽阅读 4,017评论 3 2
  • 戴金戴银戴富贵,戴珍珠戴气质。优质珍珠的温和细腻珠光总是让人越陷越深,但矜贵的珍珠就像美丽的女人一样需要长期由内及...
    陈小懒cy阅读 4,032评论 0 0
  • ……我可能已经忘记当初对爱情的最美好的畅想了
    Amarantine丶阅读 1,405评论 0 0

友情链接更多精彩内容