Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在圆形路径中移动图像视图_Java_Android_Floating Point_Imageview_Geometry - Fatal编程技术网

Java 在圆形路径中移动图像视图

Java 在圆形路径中移动图像视图,java,android,floating-point,imageview,geometry,Java,Android,Floating Point,Imageview,Geometry,我需要以圆形路径移动ImageView 程序的规范: 1) 每个ImageView都有一个meteor类,其中包含ImageView的 当前坐标 目标坐标 以及其他一些变量和函数 2) 设置目标坐标后,ImageView将以正确的速度向其移动 3) 我使用的圆的路径方程是k+sqrt(-h^2+2*h*x+r^2-x^2)(上半部分),k-sqrt(-h^2+2*h*x+r^2-x^2)(下半部分) 下面是我使用的代码,用于计算圆上半部分的目标坐标 if(mete

我需要以圆形路径移动ImageView

程序的规范:

1) 每个ImageView都有一个meteor类,其中包含ImageView的

  • 当前坐标
  • 目标坐标
  • 以及其他一些变量和函数
2) 设置目标坐标后,ImageView将以正确的速度向其移动

3) 我使用的圆的路径方程是k+sqrt(-h^2+2*h*x+r^2-x^2)(上半部分),k-sqrt(-h^2+2*h*x+r^2-x^2)(下半部分)

下面是我使用的代码,用于计算圆上半部分的目标坐标

              if(meteor.getXCoord() == meteor.getTargetCoordsX() && meteor.getXCoord() != meteor.getH() + meteor.getR()) {
                     if (meteor.getYCoord() == meteor.getTargetCoordsY()) {

                         /*
                             b+sqrt(-a^2+2*a*x+r^2-x^2), b-sqrt(-a^2+2*a*x+r^2-x^2)
                          */

                         meteor.setDeltaX(meteor.getSpeedX() + meteor.getXCoord());
                         meteor.setDeltaY(meteor.getSpeedY() + meteor.getYCoord());

                         meteor.setTargetCoordsX(meteor.getDeltaX());

                         //where target coordinate y is set *****
                         meteor.setTargetCoordY((meteor.getK() + (float) Math.sqrt(-1 * meteor.getH() * meteor.getH() + 2 * meteor.getH() * meteor.getDeltaX() + meteor.getR() * meteor.getR() - meteor.getDeltaX() * meteor.getDeltaX())));


                         //bottom half
                     }

               }
我的问题是,在第一次运行之后,目标坐标y根据logcat变为NaN。 此外,根据Log.d,用于设置目标坐标y的每个值都已正确设置。 其他信息:

  • TargetCoord y&x为浮动
  • h、 k和r是浮点数
  • delta x和y是浮点数
  • 当前坐标x和y为浮动
Delta x和y设置在上面。用于设置它的值是速度和当前坐标。速度是在1毫秒内移动的像素数


此外,所有的meteor.methods都经过了测试,并且工作正常。我认为问题与目标坐标y的计算有关。

使用一点三角法可能更容易:如果你以恒定速度旋转(
x0
y0
),坐标更新可以写成

xn = x0 + (x-x0)*cos(u) + (y-y0)*sin(u)
yn = y0 - (x-x0)*sin(u) + (x-x0)*cos(u)

其中
u
是角速度。这避免了使用
sqrt

可能更容易使用一点三角法:如果以恒定速度旋转(
x0
y0
),坐标更新可以写成

xn = x0 + (x-x0)*cos(u) + (y-y0)*sin(u)
yn = y0 - (x-x0)*sin(u) + (x-x0)*cos(u)

其中
u
是角速度。这避免了在
sqrt
callIt为负值之前,需要为
sqrt
计算参数值并检查它是否为非负值。不幸的是,我不明白为什么。可能的原因:1)您的逻辑错误2)由于浮点计算错误导致的非常小的负值太棒了,谢谢。我刚刚输入了logcat打印出来的值,我想我没有读到,可能是因为delta x和y为零。计算
sqrt
的参数值,在
sqrt
callIt为负值之前检查它是否为非负值。不幸的是,我不明白为什么。可能的原因:1)您的逻辑错误2)由于浮点计算错误导致的非常小的负值太棒了,谢谢。我刚刚输入了logcat打印出来的值,我想我没有读到它,delta x和y为零,这可能就是问题所在。太棒了,谢谢。我将在周末玩它。事实上,我在获得恒定速度时遇到了一个问题,因为x速度和y速度不同,因为屏幕大小不同。我查看了你的方法,它看起来不错,但我将保留我当前的方法,以便与我所有其他模式的移动方式保持一致。太棒了,谢谢。我将在周末玩它。实际上,我在获得恒定速度时遇到了一个问题,因为x速度和y速度是不同的,因为屏幕大小不同。我查看了你的方法,它看起来不错,但我将保留我当前的方法,以便与我所有其他模式的移动方式保持一致。