gpt4 book ai didi

java - 绘制箭头时的偏移路径

转载 作者:行者123 更新时间:2023-12-02 10:00:59 25 4
gpt4 key购买 nike

我正在通过执行以下操作来绘制箭头:

deltaX = this.mPoints[1].x - this.mPoints[3].x;
deltaY = this.mPoints[1].y - this.mPoints[3].y;

frac = (float) 0.1;

point_x_1 = this.mPoints[3].x + (1 - frac) * deltaX + frac * deltaY;
point_y_1 = this.mPoints[3].y + (1 - frac) * deltaY - frac * deltaX;

point_x_2 = this.mPoints[1].x;
point_y_2 = this.mPoints[1].y;

point_x_3 = this.mPoints[3].x + (1 - frac) * deltaX - frac * deltaY;
point_y_3 = this.mPoints[3].y + (1 - frac) * deltaY + frac * deltaX;

Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.moveTo(point_x_1, point_y_1);
path.lineTo(point_x_2, point_y_2);
path.lineTo(point_x_3, point_y_3);
path.lineTo(point_x_1, point_y_1);
path.lineTo(point_x_1, point_y_1);

path.close();

canvas.drawLine(this.mPoints[3].x, this.mPoints[3].y, this.mPoints[1].x, this.mPoints[1].y, this.mPaint);
canvas.drawPath(path, mPaint);

这是我得到的结果:

arrow image

正如您所看到的,我画的线在箭头后面伸出。

我的问题:

我如何修改当前的内容以避免在箭头后面看到线条。

我尝试过 path.offset(-30,30); 使用各种值,但它不起作用,因为线的角度每次都会改变。

最佳答案

使线结束于当前绘制线的 90%(这是 1 - frac,因此线应结束于箭头三角形的底部):

deltaX = this.mPoints[1].x - this.mPoints[3].x;
deltaY = this.mPoints[1].y - this.mPoints[3].y;

frac = (float) 0.1;

point_x_1 = this.mPoints[3].x + (1 - frac) * deltaX + frac * deltaY;
point_y_1 = this.mPoints[3].y + (1 - frac) * deltaY - frac * deltaX;

point_x_2 = this.mPoints[1].x;
point_y_2 = this.mPoints[1].y;

point_x_3 = this.mPoints[3].x + (1 - frac) * deltaX - frac * deltaY;
point_y_3 = this.mPoints[3].y + (1 - frac) * deltaY + frac * deltaX;

line_end_x = this.mPoints[1].x - frac * deltaX; // This
line_end_y = this.mPoints[1].y - frac * deltaY;

Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.moveTo(point_x_1, point_y_1);
path.lineTo(point_x_2, point_y_2);
path.lineTo(point_x_3, point_y_3);
path.lineTo(point_x_1, point_y_1);
path.lineTo(point_x_1, point_y_1);

path.close();

// line_end_* instead of this.mPoints[1].*
canvas.drawLine(this.mPoints[3].x, this.mPoints[3].y, line_end_x, line_end_y, this.mPaint);
canvas.drawPath(path, mPaint);

我无法测试数学,但我认为这是正确的。尝试一下。

关于java - 绘制箭头时的偏移路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55633433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com