美文网首页
根据屏幕上一条线求出线两侧形成的路的Path

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

作者: NemoHo | 来源:发表于2017-01-05 19:11 被阅读0次

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

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);
    }
}

相关文章

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

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

  • 水彩蓝色妖姬过程图

    根据照片打出线稿

  • express学习笔记

    路由 路由结构,app.method(path,[calback...],callback)path,服务器上的路...

  • 关于赌球2【原创】

    1.确定小组出线队伍: 根据球队实力,确定出线的队伍。 2.确定出线一二排名: 看净胜球数,积分,强弱搭配(1/8...

  • Path使用

    Path的常用方法 使用方法 lineTo,moveTo,setLastPointlineTo画一条线,到指定的点...

  • 汗水

    天很热,趴桌子上写作,汗水不听话的向下直淌,胳膊上能看到有液体渗出,像水滴形成的过程,慢慢的形成一条线,开始还有些...

  • 屏幕边缘的一条线

    你给我贴的钢化膜遮住了屏幕边缘的一条线侧过来 刚好看不见进度条让我忘了时间不知不觉就和你在一起好久了 却还是想让钢...

  • 安卓电子画板实现笔锋效果

    前言 在安卓绘图中,path是一个很常用的类,使用它可以实现基本的画线功能,但是自己用path画出来的同一条线段大...

  • 交托信任笃定真我

    这个世界只是一块屏幕,真我无限的力量投射出了屏幕上出演的一切人事物,屏幕上的幻象没有任何力量,他们只是根据真我给出...

  • 响应式布局——控制不同设备的显示样式

    响应式布局:就是根据我们做好的页面根据不同的尺寸在不同的屏幕上有不同的回馈,在小屏幕上可能我们会隐藏一些东西,这种...

网友评论

      本文标题:根据屏幕上一条线求出线两侧形成的路的Path

      本文链接:https://www.haomeiwen.com/subject/ufgzvttx.html