在 Canvas 上按某个单元扩展路径的方法
Way to extend a Path by some unit on Canvas
我有一个简单的 Path
,我将其绘制为
animPath.moveTo(360, 360);
animPath.lineTo(500, 200);
现在我想将这条路径从上次绘制的坐标延长 5 个单位(因为我只希望它的长度增加 5 个单位并且我没有更多的 x 和 y 坐标).有什么 Android API 或简单的方法吗?我觉得必须有一种方法,如 add(5)
应该根据最后的坐标在同一方向上将这条路径延长 5 个单位。但是找不到。
不幸的是,没有 API 方法。您可以通过保存两个最新点并(经过一些数学计算)通过再添加一个 animPath.lineTo(x, y)
调用来绘制此偏移量来实现它。
给出了在给定距离处找到直线上第三个点(中心和另一个点已知)的一种简单方法here,它对我有用。
Len = Sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
Normalized (unit-length) direction vector is
dx = (x2-x1) / Len
dy = (y2-y1) / Len
P3 coordinates for case when P1P3 and P1P2 vectors have the same direction:
x3 = x1 + Distance * dx
y3 = y1 + Distance * dy
for opposite direction:
x3 = x1 - Distance * dx
y3 = y1 - Distance * dy
我有一个简单的 Path
,我将其绘制为
animPath.moveTo(360, 360);
animPath.lineTo(500, 200);
现在我想将这条路径从上次绘制的坐标延长 5 个单位(因为我只希望它的长度增加 5 个单位并且我没有更多的 x 和 y 坐标).有什么 Android API 或简单的方法吗?我觉得必须有一种方法,如 add(5)
应该根据最后的坐标在同一方向上将这条路径延长 5 个单位。但是找不到。
不幸的是,没有 API 方法。您可以通过保存两个最新点并(经过一些数学计算)通过再添加一个 animPath.lineTo(x, y)
调用来绘制此偏移量来实现它。
给出了在给定距离处找到直线上第三个点(中心和另一个点已知)的一种简单方法here,它对我有用。
Len = Sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
Normalized (unit-length) direction vector is
dx = (x2-x1) / Len
dy = (y2-y1) / Len
P3 coordinates for case when P1P3 and P1P2 vectors have the same direction:
x3 = x1 + Distance * dx
y3 = y1 + Distance * dy
for opposite direction:
x3 = x1 - Distance * dx
y3 = y1 - Distance * dy