您的位置:首页 > 移动开发 > Android开发

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

2016-04-26 17:16 465 查看

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

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息