Android中计算屏幕上两点之间的路径

发布于 2024-10-20 10:56:24 字数 1309 浏览 1 评论 0原文

起初问题对我来说似乎很简单,但现在我陷入了困境。

场景 我想在屏幕上移动图像,沿着我创建的特定路径。移动该图像是在线程上进行的,类似于:

@Override
    public void run() {
        Canvas c;
        while (run) {
            c = null;
            try {
                c = panel.getHolder().lockCanvas(null);
                  synchronized (panel.getHolder()) {

                    panel.updateImageCoordinates();
                    panel.onDraw(c);


                }
            } finally {
                if (c != null) {
                    panel.getHolder().unlockCanvasAndPost(c);
                }
            }
        }

对于我想要移动的图像,我有一个列表,其中包含它应该移动的要点。每个坐标有:

 public class Coordinates {
        private int x = 0;
        private int y = 0;
        private int speedX=0;
        private int speedY=0;
}

例如,我的第一个点是-5;-30,我需要到达第二个点50.50。绘制图像的下一个坐标的计算是在 updateImageCooperatives() 上进行的。我的问题是我不知道如何计算 speedX 和 speedY 以便我沿直线从 A 点到达 B 点。基本上,对于 updateImageCoorindates() 的每次执行,我需要做的事情:

image.currentX= image.currentX+speedX;
image.currentY= image.currentY+speedY
//Check if I reached the B point. if so, move to next point.

我不知道基于坐标,如何计算 x 和 Y 方向上的速度。

我附上一张图片作为示例。任何帮助表示赞赏。 在此处输入图像描述

Problem seemed very simple to me at first but now I am stuck.

Scenario
I want to move a image on the screen, on a certain path I create. Moving this image is being made on a thread, something like:

@Override
    public void run() {
        Canvas c;
        while (run) {
            c = null;
            try {
                c = panel.getHolder().lockCanvas(null);
                  synchronized (panel.getHolder()) {

                    panel.updateImageCoordinates();
                    panel.onDraw(c);


                }
            } finally {
                if (c != null) {
                    panel.getHolder().unlockCanvasAndPost(c);
                }
            }
        }

for the Image I want to move I have a List with main points where it should go. Each coordinate has:

 public class Coordinates {
        private int x = 0;
        private int y = 0;
        private int speedX=0;
        private int speedY=0;
}

For example, my first point is -5;-30 and I need to get to second point 50.50. The calculation of next coordinates to draw the image is made on updateImageCoordinates(). My problem is that I don't know how to calculate speedX and speedY so that I get from point A to point B on a straight line. Basically for each execution of updateImageCoorindates() I need to do:

image.currentX= image.currentX+speedX;
image.currentY= image.currentY+speedY
//Check if I reached the B point. if so, move to next point.

I don't know based on knowing the coordinates, how I can calculate the speed on x and Y directions.

I attach a image for exemplification. Any help is appreciated.
enter image description here

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

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

发布评论

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

评论(2

半夏半凉 2024-10-27 10:56:24

我不确定我是否清楚地理解了你的问题......

如果你正在寻找将 PointA 转换为 AB 线上的点的函数。

包含两个点的线将具有方程:

-30 = -5*a + b

50 = 50*a + b
所以
b = -250/11
a = 16/11

因此要找到下一个点,您必须:

检查下一个点的 x 是否位于目标点的左侧 (-1) 或右侧 (+1),

并通过以下方式计算下一个点:

image.currentX= image.currentX+((-1 or +1)*movement_speed);
image.currentY= image.currentY+16/11*(-1 or +1)*movement_speed + (-250/11)

I'm not shure if I've understood your question clearly...

If you are looking for function which will translate PointA into point on line A-B.

Line containing both points will have equation:

-30 = -5*a + b
and
50 = 50*a + b
so
b = -250/11
a = 16/11

so to find next point you have to:

check if x of next point is at left (-1) or right (+1) of the destination point

and calculate next point by:

image.currentX= image.currentX+((-1 or +1)*movement_speed);
image.currentY= image.currentY+16/11*(-1 or +1)*movement_speed + (-250/11)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文