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

后端 未结 10 1822
后悔当初
后悔当初 2020-12-02 06:57

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 poin

相关标签:
10条回答
  • 2020-12-02 07:25

    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
    0 讨论(0)
  • 2020-12-02 07:27

    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

    0 讨论(0)
  • 2020-12-02 07:33

    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.

    0 讨论(0)
  • 2020-12-02 07:34

    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))
    
    0 讨论(0)
  • 2020-12-02 07:35

    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);
    
    0 讨论(0)
  • 2020-12-02 07:36

    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.

    0 讨论(0)
提交回复
热议问题