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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14096138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:38:52  来源:igfitidea点击:

Find the point on a circle with given center point, radius, and degree

c#geometry

提问by Kyle Anderson

It's been 10 years since I did any math like this... I am programming a game in 2D and moving a player around. As I move the player around I am trying to calculate the point on a circle 200 pixels away from the player position given a positive OR negative angle(degree) between -360 to 360. The screen is 1280x720 with 0,0 being the center point of the screen. The player moves around this entire Cartesian coordinate system. The point I am trying trying to find can be off screen.

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

I tried the formulas on article Find the point with radius and anglebut I don't believe I am understanding what "Angle" is because I am getting weird results when I pass Angle as -360 to 360 into a Cos(angle) or Sin(angle).

我尝试了文章用半径和角度找到点的公式,但我不相信我理解“角度”是什么,因为当我将角度作为 -360 到 360 传递给 Cos(angle) 或 Sin 时,我得到了奇怪的结果(角度)。

So for example I have...

所以例如我有...

  • 1280x720 on a Cartesian plane
  • Center Point (the position of player):
    • let x = a number between minimum -640 to maximum 640
    • let y = a number between minimum -360 to maximum 360
  • Radius of Circle around the player: let r always = 200
  • Angle: let a = a number given between -360 to 360 (allow negative to point downward or positive to point upward so -10 and 350 would give same answer)
  • 笛卡尔平面上的 1280x720
  • 中心点(玩家的位置):
    • 让 x = 最小 -640 到最大 640 之间的数字
    • 让 y = 最小 -360 到最大 360 之间的数字
  • 玩家周围的圆半径:让 r 始终 = 200
  • 角度:让 a = 一个介于 -360 到 360 之间的数字(允许负数指向下方或正数指向上方,因此 -10 和 350 会给出相同的答案)

What is the formula to return X on the circle?

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

What is the formula to return Y on the circle?

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

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

采纳答案by yoozer8

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 和 Y 坐标。

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.

这会告诉您该点与圆心的偏移量。由于您有中心坐标 (Cx, Cy),只需添加计算出的偏移量即可。

The coordinates of the point on the circle are:

圆上点的坐标为:

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

回答by Seth Battin

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.

但是,由于您提到以 -360 到 360 度来测量角度,因此您的数学库可能使用了不正确的单位。大多数三角函数的实现使用弧度作为输入。如果你改用学位……你的答案会错得离谱。

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.

请注意,您可能还会遇到象限不是您所期望的情况。这可以通过仔细选择角度零的位置来解决,或者通过手动检查您期望的象限并将您自己的符号应用于结果值。

回答by dasblinkenlight

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

当我将 Angle 作为 -360 到 360 传递给 Cos(angle) 或 Sin(angle) 时,我得到了奇怪的结果。

I think the reason your attempt did not work is that you were passing angles in degrees. The sinand costrigonometric functions expect angles expressed in radians, so the numbers should be from 0to 2*M_PI. For ddegrees you pass M_PI*d/180.0. M_PIis a constant defined in math.hheader.

我认为您的尝试无效的原因是您以度为单位传递角度。在sincos三角函数预计弧度表示的角度,所以这些数字应该是从02*M_PI。对于d你通过的学位M_PI*d/180.0M_PI是在math.h标头中定义的常量。

回答by Johan Larsson

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
  • 旁注,惯例是测量角度逆时针开始形式(正)X 轴

回答by Wraithious

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
  • 运动 - 顺时针
  • 点 - 每 6 度(因为 360 度除以 60 分是 6 度)
  • 手长 - 65 像素
  • 中心 - 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.

其中 x 和 y 是圆周上的点,Cx 和 Cy 是中心的 x,y 坐标,r 是半径,d 是度数。

回答by Aditya Aggarwal

The answer should be exactly opposite.

答案应该正好相反。

X = Xc + rSin(angle)

X = Xc + rSin(角度)

Y = Yc + rCos(angle)

Y = Yc + rCos(角度)

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

其中 Xc 和 Yc 是圆的中心坐标,r 是半径。

回答by kid

Recommend:

推荐:

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

回答by MD. Nazmul Kibria

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

这是 c# 实现。该方法将返回以radiuscenterangle interval作为参数的圆点。角度作为弧度传递。

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);

回答by TomOfMilton

I wanted to share how your contributions above helped me produce an Arduino LCD compass. I hope this is the right etiquette...I just joined stackoverflow so I could thank you fine folks.

我想分享您的上述贡献如何帮助我制作 Arduino LCD 指南针。我希望这是正确的礼节……我刚刚加入了 stackoverflow,所以我可以感谢你们这些好人。

While standing on the shoulders of the geometry giants above I was able to produce this sample compass: Arduino TFT compass with multiple bearings

当站在上面几何巨人的肩膀上时,我能够制作出这个示例指南针: 带有多个轴承的 Arduino TFT 指南针

The code for the function I called repeatedly (for different bearings you see in tiny yellow text) is written in Arduino (kinda like "C")...and is pretty translatable:

我反复调用的函数的代码(对于你在黄色小文本中看到的不同轴承)是用 Arduino 编写的(有点像“C”)......并且非常易于翻译:

void PaintCompassNeedle( int pBearingInDegrees, int pRadius, TSPoint pCentrePt ) {
    // ******************************************************************************
    // * Formula for finding pointX on the circle based on degrees around the circle:
    // * x_oncircle = x_origin + radius * cos (degrees * pi / 180)  
    // * y_oncircle = y_origin - radius * sin (degrees * pi / 180) //minus explained
    // * Thanks to folks at stackoverflow...standing on the shoulders of giants. :) 

    float bearingInRads = (pBearingInDegrees) * PI / 180; 
    // Degrees vs Rads...The math folks use Rads in their formulas

    // *******************************************************************
    // * bearingPt is the point on the circle that we are trying to find
    TSPoint bearingPt;
    // Find the X on the circle starting with orgin (centre)
    bearingPt.x = pCentrePt.x + pRadius * sin(bearingInRads); 
    // Notice the "minus" R * cos()...because TFT the y is upside down bearingPt.y = 
    pCentrePt.y - pRadius * cos(bearingInRads); 
    // * Extra Explanation: The TFT is the graphical display I'm using and it
    // * calculates x & y from the top left of screen (portrait mode) as (0,0)
    // * ...so by Subtracting from the Y orgin...I flip it vertically
    // * Other folks using x,y as increasing to the right and up respectively
    // * would keep the plus sign after the pCentrePt.y
    // *************************************************************************

    // ***************************************************************
    // * This part will change for the final product...but leaving
    // * it because when call numerous times it shows it working for
    // * a number of different quadrants (displaying yellow degrees text)
    tft.fillCircle( bearingPt.x, bearingPt.y, 5, RED); 
    tft.setCursor( bearingPt.x, bearingPt.y );
    tft.setTextSize( 1 );
    tft.setTextColor( YELLOW );
    tft.print( pBearingInDegrees );

    TSPoint innerPt;
    innerPt.x = pCentrePt.x + pRadius/2 * sin(bearingInRads);
    innerPt.y = pCentrePt.y - pRadius/2 * cos(bearingInRads);
    tft.drawLine(innerPt.x, innerPt.y, bearingPt.x, bearingPt.y, RED);

}

回答by Jaehoon

You can use this:

你可以使用这个:

Equation of circle where

圆的方程,其中

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

(xk) 2+(yv) 2=R 2

where k and v is constant and R is radius

其中 k 和 v 是常数,R 是半径