1

I need to plot n number of points inside a circle of say radius R, randomly. When I try to do it with a random theta, and random distance from the center of the circle, points are concentrated towards the center of the circle and rightfully so. Is there a way to have them spread randomly over the circle as shown below in the image?

desired output:

enter image description here

Current output:

enter image description here

below is the code in D3.js for the current output:

while(count){ (count is the number of points to be plotted inside the circle)

        var randAngle = Math.random()*Math.PI*2;
        var randRadius = Math.random()*radius; (radius is the radius of the circle in which I want to plot)
        var randX = cx + randRadius * Math.cos(randAngle);
        var randY = cy + randRadius * Math.sin(randAngle);

    
        parent_group_dns.append('rect')
                        .attr('x', randX)
                        .attr('y', randY)
                        .attr('width', 1.5)
                        .attr('height', 1.5)
                        .attr('fill', 'white')

        count--;
    }

is there a way to distribute points not by theta but just by distances?

Abhilash
  • 145
  • 3
  • 12
  • 2
    There could be something useful for you [here](https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly) – ThatCoolCoder Oct 28 '20 at 08:07
  • 2
    It's because you're generating the points in polar coordinates. Instead, generate them in Cartesian coordinates and then cull them based on whether or not they're within the circle. – Ouroborus Oct 28 '20 at 08:23
  • 3
    instead of `var randRadius = Math.random()*radius;` try `var randRadius = Math.sqrt( ~~(Math.random()*radius*radius));` Also read about [Disk Point Picking](https://mathworld.wolfram.com/DiskPointPicking.html) – enxaneta Oct 28 '20 at 08:32
  • @Ouroborus True that. Was struggling to plot in cartesian coordinates. – Abhilash Oct 28 '20 at 08:54
  • 1
    @enxaneta WORKED! I understand the reason now. Thanks Much. – Abhilash Oct 28 '20 at 08:54

0 Answers0