提供触摸一个自定义绘制圆周运动:机器人 [英] provide circular motion for a custom drawing on touch: Android

查看:328
本文介绍了提供触摸一个自定义绘制圆周运动:机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义绘制的矩形,我想基于触摸事件的圆形路径移动。
它遵循在触摸为顺时针或逆时针运动的方向,但基本上移动在圆周运动,好象上移动的圆的边缘。

I have a custom drawn rectangle which i want to move in a circular path based on touch events. It follows the direction of the touch for clockwise or anticlockwise movement but basically move in circular motion, as if moving on the edge of the circle.

我现在的思维过程如下:
根据该用户的当前和previous的x,y我将找到在度的角度,然后通过相同的角度通过重新绘制在新的位置,只需确保其上移动的圆的边缘移动此矩形。
但是,这会导致以下一些困惑:
1.如何决定角运动是顺时针或逆时针。
2.我不能够正确地找出数学这一点。

My current thought process is as follows: Based on the users current and previous x,y i shall find the angle in degrees and then move this rectangle by the same angle by re-drawing in the new position, just making sure that it moves on the edge of a circle. But this leads to some confusion on the following: 1. how do i decide whether angle movement is clockwise or anti-clockwise. 2. I am not being able to figure out the math for this properly.

这会是最好的办法还是有这样做的更好的主意?
此外,如果这是最好的办法,可能有人请告诉我的计算公式由同时采取的时钟和逆时针照顾我应该移动的角度?

Would this be the best approach or is there a better idea for doing this? Also, if this is the best approach, could someone please tell me the formula for calculating the angle by which i should move it while taking care of the clocking and anticlockwise ?

可能有人请帮忙吗?
请让我知道是否需要任何更多的细节。
谢谢

could someone please help? please let me know if any more details are required. Thanks

推荐答案

下面是为了几个步骤来移动你的矩形沿圆的边缘的当用户点击并持有以圆的一面:结果
  1 获取所需的方向。结果
  2 获得当前x和y坐标角度。结果
  3。添加方向(+1如果逆时针方向,-1,如果按顺时针方向),以角。结果
  4。计算新的x和y坐标。结果
  5。更新/显示矩形。

Steps

Here are a few steps in order to move your rectangle along a circle's rim when the user taps and holds to the side of the circle:
1. Obtain direction desired.
2. Obtain angle from current x and y coordinates.
3. Add direction (+1 if counterclockwise, -1 if clockwise) to angle.
4. Calculate new x and y coordinates.
5. Update/display rectangle.

1 在伪code, =方向符号(Rectangle1.x - UsersFingerPosition.x)。这里的标志是返回一个函数-1如果数字为负,0如果是0,1,如果它是积极的。需要注意的是符号(0)仅当用户在您的矩形位置的精确x和y结果。在这种情况下,矩形不动(这应该是很好的)。在Java中,签到功能是 Math.signum()

1. In pseudocode, direction = sign(Rectangle1.x - UsersFingerPosition.x). Here sign is a function returning -1 if the number was negative, 0 if it is 0, and 1 if it is positive. Note that sign(0) will only result when the user is on the exact x and y of your rectangle's location. In that case, the rectangle would not move (which should be good). In Java, the sign function is Math.signum().

2 以获得当前的角度使用以下Java code:

2. To obtain the current angle use the following java code:

double angle = Math.toDegrees(Math.atan2(Circle.y-Rectangle1.y, Rectangle1.x-Circle.x));

请注意顺序 Circle.y-Rectangle.y Rectangle.x ... Circle.x 。这是对坐标(0,0)在左上角,而不是在屏幕的中心是一个结果。

Note the order of Circle.y-Rectangle.y and Rectangle.x...Circle.x. This is a result of the coordinate (0, 0) being in the top left corner instead of the center of the screen.

3。够简单,只需添加的方向角。如果需要的话,做这样的事情。

3. Simple enough, just add direction to angle. If desired, do something like

angle += direction*2; //So it will move more quickly

4 以获取新的x和你的矩形的y坐标,用三角函数正弦和余弦:

4. To get the new x and y coordinates of your rectangle, use the trigonometric functions sine and cosine:

Rectangle1.x = Math.cos(Math.toRadians(angle))*Circle.radius + Circle.x - Rectangle1.width;
Rectangle1.y = Math.sin(Math.toRadians(angle))*Circle.radius + Circle.y - Rectangle1.height;

(其中Circle.x和Circle.y是您的圆的中心的坐标和Circle.radius自然是它的半径)。

(where Circle.x and Circle.y are the coordinates of the center of your circle and Circle.radius is naturally it's radius).

5 这一次你必须照顾(或已经)。!)

5. This one you'll have to take care of (or have already) :)!

希望这可以帮助您!

这篇关于提供触摸一个自定义绘制圆周运动:机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆