根据屏幕上一条线求出线两侧形成的路的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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值