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
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)
}
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.
I don't know c#, anyway if you are trying to draw the points somewhere you have to consider the fact that the Y axis crease from the top to the bottom of the screen, so your sin element should have be -sin(...) and not +sin(...)
so
result.Y = centerPoint.Y + (int)Math.Round( distance * Math.Sin( angle ) );
should become:
result.Y = centerPoint.Y - (int)Math.Round( distance * Math.Sin( angle ) );
If you are not trying to draw them I could not imagine what the problem is, can you give some example?
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);
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?)
-(NSMutableArray *)GetPointsForCircle
{
NSMutableArray *Points = [[NSMutableArray alloc] init];
CGPoint CenterPoint = CGPointMake(160, 230);
CGPoint Point;
for (float Angel = 0; Angel <= 360; Angel+= 60)
{
Point.x = CenterPoint.x + 100 * cos(Angel);
Point.y = CenterPoint.y + 100 * sin(Angel);
[Points addObject:[NSValue valueWithCGPoint:Point]];
}
return Points;
}
- (CGPoint)pointOnCircle:(int)thisPoint withTotalPointCount:(int)totalPoints
{
CGPoint centerPoint = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
float radius = 100.0;
float angle = ( 2 * M_PI / (float)totalPoints ) * (float)thisPoint;
CGPoint newPoint;
newPoint.x = (centerPoint.x) + (radius * cosf(angle));
newPoint.y = (centerPoint.y) + (radius * sinf(angle));
return newPoint;
}