【发布时间】:2017-02-21 12:24:31
【问题描述】:
我需要在圆形路径中移动 ImageView。
程序的规格:
1) 每个 ImageView 都有一个包含 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 在上面设置。用于设置它的值是速度和当前坐标。 speed 是 1 毫秒内移动的像素数
此外,所有流星.methods 都已经过测试,并且可以正常工作。我认为问题与目标坐标y的计算有关。
【问题讨论】:
-
计算
sqrt的参数值并在sqrt调用之前检查它是否为非负数 -
它是否定的。不幸的是,我不明白为什么。
-
可能的原因:1)你的逻辑错误2)由于浮动计算错误导致的非常小的负值
-
太棒了,谢谢。我只是在输入 logcat 打印出来的值,我想我错过了阅读它 delta x 和 y 为零,这可能是问题所在。
标签: java android floating-point imageview geometry