从每个路径与初始点分开的点绘制路径

问题描述 投票:1回答:1

我在尝试弄清楚如何从画布上的某个点绘制路径时遇到了一些麻烦,每个路径的起点与初始点的距离相等。为了说明我的意思,我到目前为止的代码能够生成:enter image description here

并且期望的结果是这样的:

enter image description here

我的代码:

int n = 3;
int r;
double x;
double y;
point1 = new Point(mWidth/2, mHeight/2);
double angle;
double angleFactor;

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    for (int i = 0; i < 3; i++){
        angleFactor = 2 * Math.PI / n;
        angle = i * angleFactor;
        x = (point1.x) + r * Math.cos(angle);
        y = (point1.y) + r * Math.sin(angle);

        //Draw paths
        path.reset();
        path.moveTo(point1.x, point1.y);
        path.lineTo((float) x, (float) y);
        canvas.drawPath(path, paint);
    }

}

有一个简单的解决方案吗?

java android canvas path point
1个回答
1
投票

由于您希望线的偏移与中心点之间的距离很小,因此您可以像这样定义起始坐标:

double xStart, xEnd;
double yStart, yEnd;
double offsetFraction = 0.1;

foronDraw()循环内:

double lengthX = r * Math.cos(angle);
double lengthY = r * Math.sin(angle);

xStart = (point1.x) + offsetFraction * lengthX;
yStart = (point1.y) + offsetFraction * lengthY;
xEnd = (point1.x) + lengthX;
yEnd = (point1.y) + lengthY;

//Draw paths
path.reset();
path.moveTo((float) xStart, (float) yStart);
path.lineTo((float) xEnd, (float) yEnd);
canvas.drawPath(path, paint);
© www.soinside.com 2019 - 2024. All rights reserved.