在给定中心点、半径和度数的圆上找到点

我已经有10年没有做过这样的数学了... ... 我正在用2D 编写一个游戏,并且移动一个玩家。当我移动玩家的时候,我试图在一个距离玩家位置200像素的圆上计算点,给定一个正或负的角度(度数)在 -360到360之间。屏幕是1280x720,0是屏幕的中心点。玩家在整个笛卡儿坐标系中移动。我试图找到的点可以在屏幕之外。

我尝试了文章 找出带有半径和角度的点的公式,但我不相信我理解什么是“角度”,因为我得到了奇怪的结果,当我通过角度为 -360到360进入一个科斯(角度)或罪(角度)。

举个例子。

  • 1280x720在笛卡尔平面上
  • 中心点(球员的位置) :
    • Let x = 一个介于最小 -640到最大640之间的数字
    • 让 y = 一个介于最小 -360到最大360之间的数字
  • 玩家周围的圆半径: 让 r 总是 = 200
  • 角度: 让 a = 一个给定的数字在 -360到360之间(允许负数指向下方或正数指向上方,所以 -10和350会给出相同的答案)

在圆上返回 X 的公式是什么?

在圆上返回 Y 的公式是什么?

enter image description here enter image description here

98706 次浏览

You should post the code you are using. That would help identify the problem exactly.

However, since you mentioned measuring your angle in terms of -360 to 360, you are probably using the incorrect units for your math library. Most implementations of trigonometry functions use radians for their input. And if you use degrees instead...your answers will be weirdly wrong.

x_oncircle = x_origin + 200 * cos (degrees * pi / 180)
y_oncircle = y_origin + 200 * sin (degrees * pi / 180)

Note that you might also run into circumstance where the quadrant is not what you'd expect. This can fixed by carefully selecting where angle zero is, or by manually checking the quadrant you expect and applying your own signs to the result values.

The simple equations from your link give the X and Y coordinates of the point on the circle relative to the center of the circle.

X = r * cosine(angle)
Y = r * sine(angle)

This tells you how far the point is offset from the center of the circle. Since you have the coordinates of the center (Cx, Cy), simply add the calculated offset.

The coordinates of the point on the circle are:

X = Cx + (r * cosine(angle))
Y = Cy + (r * sine(angle))

I am getting weird results when I pass Angle as -360 to 360 into a Cos(angle) or Sin(angle).

I think the reason your attempt did not work is that you were passing angles in degrees. The sin and cos trigonometric functions expect angles expressed in radians, so the numbers should be from 0 to 2*M_PI. For d degrees you pass M_PI*d/180.0. M_PI is a constant defined in math.h header.

I highly suggest using matrices for this type of manipulations. It is the most generic approach, see example below:

// The center point of rotation
var centerPoint = new Point(0, 0);
// Factory method creating the matrix
var matrix = new RotateTransform(angleInDegrees, centerPoint.X, centerPoint.Y).Value;
// The point to rotate
var point = new Point(100, 0);
// Applying the transform that results in a rotated point
Point rotated = Point.Multiply(point, matrix);
  • Side note, the convention is to measure the angle counter clockwise starting form (positive) X-axis

I also needed this to form the movement of the hands of a clock in code. I tried several formulas but they didn't work, so this is what I came up with:

  • motion - clockwise
  • points - every 6 degrees (because 360 degrees divided by 60 minuites is 6 degrees)
  • hand length - 65 pixels
  • center - x=75,y=75

So the formula would be

x=Cx+(r*cos(d/(180/PI))
y=Cy+(r*sin(d/(180/PI))

where x and y are the points on the circumference of a circle, Cx and Cy are the x,y coordinates of the center, r is the radius, and d is the amount of degrees.

The answer should be exactly opposite.

X = Xc + rSin(angle)

Y = Yc + rCos(angle)

where Xc and Yc are circle's center coordinates and r is the radius.

Recommend:

public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
{
return Quaternion.Euler(angles) * (point - pivot) + pivot;
}

Here is the c# implementation. The method will return the circular points which takes radius, center and angle interval as parameter. Angle is passed as Radian.

public static List<PointF> getCircularPoints(double radius, PointF center, double angleInterval)
{
List<PointF> points = new List<PointF>();


for (double interval = angleInterval; interval < 2 * Math.PI; interval += angleInterval)
{
double X = center.X + (radius * Math.Cos(interval));
double Y = center.Y + (radius * Math.Sin(interval));


points.Add(new PointF((float)X, (float)Y));
}


return points;
}

and the calling example:

List<PointF> LEPoints = getCircularPoints(10.0f, new PointF(100.0f, 100.0f), Math.PI / 6.0f);

You can use this:

Equation of circle where

(x-k)2+(y-v)2=R2

where k and v is constant and R is radius