Question

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 is simple, as stated in the title: I just want to find the point at some distance and angle from a center point.

My current code:

Point centerPoint = new Point ( 0, 0 );
Point result      = new Point ( 0, 0 );
double angle      = 0.5; //between 0 and 2 * PI, angle is in radians
int distance      = 1000;

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

In general, this seems to work fairly reasonably, but I get problems at various spots, most notably when the angle corresponds to points in the negative x and y axis. Clearly I'm doing something wrong -- thoughts on what that is?

UPDATE: This was my mistake, this code works fine -- the few outliers that were not working were actually due to a bug in how the angle for 1.5PI was being calculated. I thought I had checked that well enough, but evidently had not. Thanks to everyone for their time, hopefully the working code above will prove helpful to someone else.

Was it helpful?

Solution

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

OTHER TIPS

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

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.

-(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;   
    }

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top