Home > Software engineering >  Get random point on circle on a segment that has a specific heading
Get random point on circle on a segment that has a specific heading

Time:10-25

Example

I have a circle, with a specific radius. Inside the circle I have a point with the X, Y coords. I also have a heading, for example 210. How can I get a random point INSIDE THE CIRCLE on the segment that I draw from the point to the margin of the circle? Thanks!

CodePudding user response:

        double radiusCircle = 10;
        double y = -3;
        double x = -Math.Sqrt(3 * 9);
        double angleRad = Math.Atan2(y, x);
        double angleDeg = angleRad * 180 / Math.PI;   // 210 (-150) degrees

        double distancePointCenter = Math.Sqrt(x * x   y * y);
        double distancePointMargin = radiusCircle - distancePointCenter;

        Random rand = new Random(123);

        for (int i = 0 i < 10; i  )
        {   // Generate 10 random points
            double randomPointDistToCenter = distancePointCenter   distancePointMargin * rand.NextDouble();
            double xRandomPoint = randomPointDistToCenter * Math.Cos(angleRad);
            double yRandomPoint = randomPointDistToCenter * Math.Sin(angleRad);
        }
  • Related