c

Cyberpunk Keanu

03/06/2020, 10:42 AM
Can someone with good mathematical logic help me out for this? I'm drawing two concentric circles on the screen. The smaller one has a radius startRadius and the outer larger one's radius is endRadius. So, Now I want to get a randomly generated point, which lies in the region between the two concentric circles. Also, is there a way to get significant distance between the previously generated random point & the newly generated random point? I'll be adding imageviews on these points, so don't want overlapping images because the two randomly picked points are almost next to each other. Thanks.
t

Tim Malseed

03/06/2020, 10:46 AM
I would write a function that determines if a point lies inside a circle. Then I would test that the point falls withing the outer circle, and doesn’t fall within the inner circle
A point is considered inside a circle, if the distance from the center of the circle to the point is less than the radius of the circle
Something like:
Copy code
``````fun isPointInCircle(point: Point, circleCenter: Point, radius: Int) : Boolean {

return sqrt((point.x - circleCenter.x).pow(2) + (point.y - circleCenter.y).pow(2)) < radius

}``````
p

pedro

03/06/2020, 10:51 AM
Assuming the centre of the circles is
``(0,0)``
, and radius is r1 and r2 (r1 is smaller), generate a random value for an angle (0-360 degrees or use radians straight away as usually that’s what the drawing primitives will ask you)and generate a random value for the radius, which has to be between r1 and r2. Now use simple trigonometry to get the
``(x, y)``
position (
``x = radius * cos angle;  y = radius * sin angle``
)
5