Find N random points within a Circle
Last Updated :
22 Jun, 2022
Given four integers N, R, X, and Y such that it represents a circle of radius R with [X, Y] as coordinates of the center. The task is to find N random points inside or on the circle.
Examples:
Input: R = 12, X = 3, Y = 3, N = 5
Output: (7.05, -3.36) (5.21, -7.49) (7.53, 0.19) (-2.37, 12.05) (1.45, 11.80)
Input: R = 5, X = 1, Y = 1, N = 3
Output: (4.75, 1.03) (2.57, 5.21) (-1.98, -0.76)
Approach: To find a random point in or on a circle we need two components, an angle(theta) and distance(D) from the center. After that Now, the point (xi, yi) can be expressed as:
xi = X + D * cos(theta)
yi = Y + D * sin(theta)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define PI 3.141592653589
double uniform()
{
return ( double ) rand () / RAND_MAX;
}
vector<pair< double , double > > randPoint(
int r, int x, int y, int n)
{
vector<pair< double , double > > res;
for ( int i = 0; i < n; i++) {
double theta = 2 * PI * uniform();
double len = sqrt (uniform()) * r;
res.push_back({ x + len * cos (theta),
y + len * sin (theta) });
}
return res;
}
void printVector(
vector<pair< double , double > > A)
{
for (pair< double , double > P : A) {
printf ( "(%.2lf, %.2lf)\n" ,
P.first, P.second);
}
}
int main()
{
int R = 12;
int X = 3;
int Y = 3;
int N = 5;
printVector(randPoint(R, X, Y, N));
return 0;
}
|
Java
import java.util.*;
class GFG{
static final double PI = 3.141592653589 ;
static class pair
{
double first, second;
public pair( double first,
double second)
{
super ();
this .first = first;
this .second = second;
}
}
static double uniform(){ return Math.random();}
static Vector<pair> randPoint( int r, int x,
int y, int n)
{
Vector<pair> res = new Vector<pair>();
for ( int i = 0 ; i < n; i++)
{
double theta = 2 * PI * uniform();
double len = Math.sqrt(uniform()) * r;
res.add( new pair(x + len * Math.cos(theta),
y + len * Math.sin(theta)));
}
return res;
}
static void printVector(Vector<pair> A)
{
for (pair P : A)
{
System.out.printf( "(%.2f, %.2f)\n" ,
P.first, P.second);
}
}
public static void main(String[] args)
{
int R = 12 ;
int X = 3 ;
int Y = 3 ;
int N = 5 ;
printVector(randPoint(R, X, Y, N));
}
}
|
Python3
import math
import random
PI = 3.141592653589 ;
class pair:
def __init__( self , first, second):
self .first = first;
self .second = second;
def uniform():
return random.random();
def randPoint(r, x, y, n):
res = list ();
for i in range (n):
theta = 2 * PI * uniform();
len = math.sqrt(uniform()) * r;
res.append(pair((x + len * math.cos(theta)), (y + len * math.sin(theta))));
return res;
def printVector(A):
for P in A:
print ( "({0:.2f}" . format (P.first), ", " , "{0:.2f})" . format (P.second));
if __name__ = = '__main__' :
R = 12 ;
X = 3 ;
Y = 3 ;
N = 5 ;
printVector(randPoint(R, X, Y, N));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly double PI = 3.141592653589;
class pair
{
public double first, second;
public pair( double first,
double second)
{
this .first = first;
this .second = second;
}
}
static double uniform()
{
return new Random().NextDouble();
}
static List<pair> randPoint( int r, int x,
int y, int n)
{
List<pair> res = new List<pair>();
for ( int i = 0; i < n; i++)
{
double theta = 2 * PI * uniform();
double len = Math.Sqrt(uniform()) * r;
res.Add( new pair(x + len * Math.Cos(theta),
y + len * Math.Sin(theta)));
}
return res;
}
static void printList(List<pair> A)
{
foreach (pair P in A)
{
Console.Write( "({0:F2}, {1:F2})\n" ,
P.first, P.second);
}
}
public static void Main(String[] args)
{
int R = 12;
int X = 3;
int Y = 3;
int N = 5;
printList(randPoint(R, X, Y, N));
}
}
|
Javascript
function uniform()
{
return Math.random();
}
function randPoint(r, x, y, n)
{
let res = new Array();
for (let i = 0; i < n; i++) {
let theta = 2 * Math.PI * uniform();
let len = Math.sqrt(uniform()) * r;
res.push([x + len * Math.cos(theta), y + len * Math.sin(theta)]);
}
return res;
}
function printVector(A)
{
for (let i = 0; i < A.length; i++) {
console.log( "(" + A[i][0].toFixed(2) + ", " + A[i][1].toFixed(2) + ")" );
}
}
let R = 12;
let X = 3;
let Y = 3;
let N = 5;
printVector(randPoint(R, X, Y, N));
|
Output:
(7.05, -3.36)
(5.21, -7.49)
(7.53, 0.19)
(-2.37, 12.05)
(1.45, 11.80)
Time Complexity: O(N)
Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...