[R] random point in a circle centred in a geographical position
Albert Picado
albert_picado at yahoo.fr
Sat Oct 7 09:32:52 CEST 2006
Dear List members
I am trying to find a way to generate a random point in a circle centred in a geographical location.
So far I have used the following formula (see code below):
random_x = original_x + radius*cos(angle)
random_y = original_y + radius*sin(angle)
where radius is a random number between 0 and the radius of the circle and
angle is between 0 and 360 degrees
The code bellow works fine however some people argue that this method will produce uneven locations… I really do not understand why.
Another option will be to use the “rejection method”: generate points inside a box and reject points that fall outside the circle. However I think this is much complicate… or at least I don’t know how to programme it.
So,
1. Is there any package that generates random points in a circle? (I couldn’t find any)
2. Do you think the code bellow produces uneven locations?
3. Any suggestions to programme the “rejection method”? Maybe someone has already used it...
Cheers
albert
#Code random location in a circle
# Generate UTM coordinates (original location)
> x_utm<-c(289800)
> y_utm<-c(4603900)
> coord<-as.data.frame(cbind(x_utm,y_utm))
# Generate a random radius with a maximum distance of 1000 meters from the original location.
>radius<-runif(1,0,1000)
# Generate a random angle
>angle<-runif(1,0,360)
#Generate a random x coordinate
>x_rdm<-coord$x_utm[1]+radius*cos(angle)
#Generate a random y coordinate
>y_rdm<-coord$y_utm[1]+radius*sin(angle)
>result<-as.data.frame(cbind(x_rdm,y_rdm, x_utm, y_utm))
# We can calculate the distance between the original and the random location
> result$dist_m<-sqrt(((coord$x_utm-result$x_rdm)^2+(coord$y_utm-result$y_rdm)^2))
>result
More information about the R-help
mailing list