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

Solution 1:

You forgot to add the center point:

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

The rest should be ok... (what strange results were you getting? Can you give an exact input?)

Solution 2:

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

Solution 3:

A Swift3 version

func pointOnCircle(radius: Double, angleInDegrees: Double, origin: CGPoint) -> CGPoint {
    let x = abs(Double(origin.x) + radius * cos(angleInDegrees * (.pi / 180)))
    let y = abs(Double(origin.y) - radius * sin(angleInDegrees * (.pi / 180)))

    return CGPoint(x: x, y: y)
}

Solution 4:

Without more information on the exact errors it's hard to tell what's wrong. The equations look right and should work. Are you sure the angles you are passing in are correct for angles > 90 degrees? The only other thing I could think of would be that you're multiplying distance (an int) by the result of Math.sin (double) but that shouldn't really be an issue.