在android上的2个距离点之间画线

发布于 2025-01-06 04:43:15 字数 219 浏览 1 评论 0原文

假设我们有一个 400x800 的画布,我想画一条连接点的线 P1 (10,10) 和 P2(500000,800000)。 正如您所看到的,第二个点远远超出了画布边界。如果我使用 canvas.darwLine(p1.x, p1.y,p2.x,p2.y,paint) 应用程序将冻结并且应用程序变得无法使用。 使用剪裁并不能解决问题,绘图引擎仍在尝试将像素绘制到第二点的整个路径

有任何建议或解决方法吗?

Lets say we have a 400x800 canvas and I want to draw a line connecting the points
P1 (10,10) and P2(500000,800000).
As you can see the second point is far outside the canvas boundaries. If I use canvas.darwLine(p1.x, p1.y,p2.x,p2.y,paint) the app freezes and the app becomes unusable.
using a clipping doesn't solve the problem, the drawing engine is still trying to draw the pixels to the whole way to the second point

Any suggestions or a workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫罗兰の梦幻 2025-01-13 04:43:15

如果 P2 位于可见区域之外(在本例中为 480x800),则计算该线与边框的交点,然后使用交点而不是 P2。

If P2 is outside the visible area (480x800 in this particular case) then calculate the intersection of this line with border, an then use the intersection point instead of P2.

赠佳期 2025-01-13 04:43:15

你可以缩小你的生产线,像这样:

int maxX = 400;
int maxY = 800;

//Calculate how much we have to scale down to fit in the bounds:
float scaleX = (maxX - p1.x)/p2.x;
float scaleY = (maxY - p1.y)/p2.y;

//Get the smallest scale, so that we fit in both axises.
float scale = Math.min(scaleX, scaleY);

//Only scale if we are scaling down. There is no need to make lines smaller than the screen scale up to the screen bounds:
if(scale < 1.0f){
    p2.x *= scale;
    p2.y *= scale;
}

我还没有尝试过这个,所以我不能保证它会起作用。

You can scale down your line, with something like this:

int maxX = 400;
int maxY = 800;

//Calculate how much we have to scale down to fit in the bounds:
float scaleX = (maxX - p1.x)/p2.x;
float scaleY = (maxY - p1.y)/p2.y;

//Get the smallest scale, so that we fit in both axises.
float scale = Math.min(scaleX, scaleY);

//Only scale if we are scaling down. There is no need to make lines smaller than the screen scale up to the screen bounds:
if(scale < 1.0f){
    p2.x *= scale;
    p2.y *= scale;
}

I haven't tried this, so I can't guarantee that it will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文