问题描述
数学再次击败我。这是这样一个简单的任务,但我不能设法把它完成。
Math has defeated me once again. This is such a simple task, but I can't manage to get it done.
情景:我画上一个SurfaceView一个圆形的图像。用户倒是像边境的一个点,并开始将它拖到adround。我需要根据用户的移动旋转一圈图像。我有两个信息重要的一块,图像中心X,Y坐标和触摸点坐标。
Scenario: I draw on a SurfaceView a round image. The user touches a point on image border and starts to drag it adround. I need to rotate the circle image according to user movement.I have two important piece of information, the image center X,Y coordinates and the touched points coordinates.
正如你可以在图片中看到,用户触摸的点,根据我的抽签触摸点的角度应在40。我不能设法正确地计算它。
As you can see in the image, the user touched a point, according to my draw the touched point angle should be around 40. I can't manage to calculate it properly.
我试图用这个公式:
angle = Math.atan2(touchedY - centerY, touchedX - centerX) * 180 / Math.PI
我不能设法了解我应该怎么计算角度,因为它是现在,它不能正常工作和值都不好。例如,在图像的情况下,该角度计算为-50。
I can't manage to understand how I should calculate the angle, as it is now, it doesn't work properly and values are not good. For instance, in the case of the image, the angle calculate is -50.
感谢您的时间,任何的信息是很乐意考虑。
Thank you for your time, any informations is gladly taken.
LE:其实我做了一个错误,我想,如下所述。如果圆圈是这样的:
LE: Actually I did a mistake I think, as mentioned below. Should the circle be like:
推荐答案
让我们改写这个问题:你想找到两个向量的夹角。第一载体是upvector会straigt从您的中心点( U ),第二个载体是从中心点到触摸点的向量( v )
Let's reformulate the problem: You want to find the angle between two vectors. The first vector is the upvector going straigt up from your center-point (u), and the second vector is the vector from the center point to the touch point (v).
现在我们可以回想一下(或谷歌)的
Now we can recall (or google) that
COS的在 = UV /(| U | * | V |)
cos a = uv / (|u|*|v|)
其中的在的是向量之间的夹角| U |是一个矢量的长度。该upvector, U ,是(0,1)和长度为1。
Where a is the angle between the vectors and |u| is the length of a vector. The upvector, u, is (0, 1) and has length 1.
用手乘以向量取消的X项,为我们提供了这样的事情。
Multiplying the vectors by hand cancels the x-term and gives us something like this.
double tx = touch_x - center_x, ty = touch_y - center_y;
double t_length = Math.sqrt(tx*tx + ty*ty);
double a = Math.acos(ty / t_length);
请注意如何购买V矢量减去所述触摸点的中心点获得。请记住,如果需要转换为度。
Note how the v vector is obtained by subtracting the center point from the touch point. Remember to convert to degrees if needed.
这篇关于计算触摸点的角度和旋转它的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!