Calculating point on a circle's circumference from angle in C#?

后端 未结 6 995
故里飘歌
故里飘歌 2020-11-29 03:43

I imagine that this is a simple question, but I\'m getting some strange results with my current code and I don\'t have the math background to fully understand why. My goal

6条回答
  •  日久生厌
    2020-11-29 03:55

    Firstly, since you're in radians it's probably beneficial to define your angle as such:

    double angle = (Math.PI / 3); // 60 degrees...
    

    The functions themselves are working fine. The rounding will only affect your answer if your distance is sufficiently small enough. Other than that, the answers should come out just fine.

    If it's the rounding you're worried about, remember that by default, .NET does banker's rounding, and you may want:

    result.X = (int)Math.Round(centerPoint.X + distance * Math.Cos(angle), MidpointRounding.AwayFromZero);
    result.Y = (int)Math.Round(centerPoint.Y + distance * Math.Sin(angle), MidpointRounding.AwayFromZero);
    

    instead.

    Additionally, in the question you want distance X and angle Y... I assume you're not relating that to the point (X,Y), because that's completely different.

    The distance formula is:

    double distance = Math.Sqrt((centerPoint.X + result.X)^2 + (centerPoint.Y + result.Y)^2);
    

提交回复
热议问题