jeudi 20 octobre 2016

R - Create random rectangle with given dimensions

I'm simulating an environment in R where points are generated randomly to be located within a circle with the following code:

rho <- sqrt(runif(1, 0.0, 1.0))
theta <- runif(1, 0, 2*pi) 

I would like to generate a random rectangle within the circle where dimensions can be configurable by the user. This rectangle can move north, west, south or east with a probability 0.25 to each direction.

obs_direction<-sample(c(0:3), size=1, replace=TRUE, prob=c(0.25,0.25,0.25,0.25)) # 0-N 1-W 2-S 3-E

I would like to detect when the point is located within the rectangle or blocked by it respect the coordinate 0,0. By means, if we draw a line between 0,0 and the point, it crosses the rectangle or not.

How can I do it efficiently?




Aucun commentaire:

Enregistrer un commentaire