如何计算带有一定角度的圆弧位置? [英] How to calculate position on a circle with a certain angle?

查看:317
本文介绍了如何计算带有一定角度的圆弧位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出我怎么可能能够在一个圆圈计算坐标。为简单起见我做了一些图片。

这与我有信息的开始。现在我需要计算新的坐标时,例如圆形会变成90度的权利。就像下一张图片:

我需要计算新的红点的坐标。 (我还需要这有不同程度的如20度)。

要做到这一点我的计划是要做到以下几点:


  • 计算两点之间的距离

  • 计算北(上)和给定的点之间的度

  • 计算的程度(从退一步)新位置+它需要打开(在图像90度)度。

我的第一个步骤是:

 距离=的Math.sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(点1 .Y-point2.y))

来计算新度的部分是:

 双THETA = Math.atan2(targetPt.y  -  centerPt.y,targetPt.x  -  centerPt.x);
THETA + = Math.PI / 2.0;

和最后部分来计算新的位置将是:

 双X = mMiddleView.getX()+距离* Math.cos(Math.toRadians(THETA));
双Y = mMiddleView.getY()+距离* Math.sin(Math.toRadians(THETA));

然而,当我做这些计算例如与0度,它仍然会返回比原来的坐标另一个值。

任何帮助将是AP preciated!

编辑为菲利普Jahoda:

我的价值观是:

 距离+  -  70,currentDegree = 0。
的PointF点=新的PointF((浮点)mMiddleView.getX(),(浮点)mMiddleView.getY());
的PointF点2 =为getPosition(点(浮点)的距离,currentDegree);

和我的结果是:

 中心:的PointF(490.0,728.0)半径:78.0角度:0.0
新点:的PointF(568.0,728.0)

正如你所看到的,度数为0,因此该点不应该转。它应保持490,728坐标但它不会让那些。


解决方案

这就是如何:

 私人的PointF为getPosition(的PointF中心,半径浮动,浮动角){    的PointF P =新的PointF((浮点)(center.x +半径* Math.cos(Math.toRadians(角度)))
    (浮点)(center.y +半径* Math.sin(Math.toRadians(角度))));    回磷;
}

该方法计算各地视半径和角度圆(视图中心)的中心位置即可。角的度数。

返回的的PointF 将包含x和y坐标所计算的位置的

请注意,0度是圆的极东的位置,270度是圆的最北端的位置:

因此​​,如果您的视图的中心在x:100,Y:100和你计算为0度的角度和50的半径的位置,其结果将是X:150,Y:100

如果使用角度90度和半径50,其结果将是X:100,Y:150

它是用来<一个href=\"https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartLib/src/com/github/mikephil/charting/utils/Utils.java#L485-L499\"相对=nofollow> 这里 在我的图表libary,和它的作品。

I'm trying to figure out how I could be able to calculate coordinates on a circle. For simplicity I made some images.

That's the start with information I have. Now I need to calculate the new coordinates when for example the circle would turn 90 degrees to the right. Just like the next image:

I need to calculate the coordinates of the new red dot. (I also need this with different degrees such as 20 degrees).

To do this my plan was to do the following:

  • Calculate the distance between the two points
  • Calculate the degree between the north (up) and the given point
  • Calculate the new location with the degree (from a step back) + the degrees it needs to turn (in the images 90 degrees).

My first step is:

distance = Math.sqrt((point1.x-point2.x)*(point1.x-point2.x) + (point1.y-point2.y)*(point1.y-point2.y))

The part to calculate the new degrees is:

double theta = Math.atan2(targetPt.y - centerPt.y, targetPt.x - centerPt.x);
theta += Math.PI/2.0;

And the last part to calculate the new location would be:

double x = mMiddleView.getX() + distance * Math.cos(Math.toRadians(theta));
double y = mMiddleView.getY() + distance * Math.sin(Math.toRadians(theta));

However when I do these calculations with for example 0 degrees it still returns another value than the original coordinates.

Any help would be appreciated!

Edit for Philipp Jahoda:

My values are:

distance +- 70, currentDegree = 0.
PointF point = new PointF((float)mMiddleView.getX(), (float)mMiddleView.getY());
PointF point2 = getPosition(point, (float) distance, currentDegree);

and my results are:

center: PointF(490.0, 728.0) radius: 78.0 angle: 0.0
new point: PointF(568.0, 728.0)

As you can see, the degree is 0 so the point is not supposed to turn. It should keep the 490, 728 coordinates but it does not keep those.

解决方案

Thats how:

private PointF getPosition(PointF center, float radius, float angle) {

    PointF p = new PointF((float) (center.x + radius * Math.cos(Math.toRadians(angle))),
    (float) (center.y + radius* Math.sin(Math.toRadians(angle))));

    return p;
}

This method calculates the position around the center of a circle (center of your view) depending on radius and angle. Angle in degrees.

The returned PointF will contain the x- and y-coordinate of the calculated position.

Be aware that 0 degrees is at the very east position of the circle, 270 degrees is in the very north position of the circle:

So if the center of your view is at x: 100, y: 100 and you calculate the position with an angle of 0 degrees and a radius of 50, the result will be x: 150, y: 100

If you use angle 90 degrees and radius 50, the result will be x: 100, y: 150

It is used here in my charting libary, and it works.

这篇关于如何计算带有一定角度的圆弧位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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