Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
845 views
in Technique[技术] by (71.8m points)

random - R generate points with condition using runifpoint function

I am trying to generate randomly distributed points in a rectangle.

To create 50 random points in a rectangle, I used

i=50
  pp<-runifpoint(i, win=owin(c(0,19.5),c(0,3.12))

If I were to add conditions on the coordinates before randomly generating points,

e.g. 0.24 <x<19.26 ,0.24<y<2.64 , then generate random points, what code can I imply?

The ultimate goal is to generate points in the rectangle except for the grey shaded area, in the below image

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a question about the R package spatstat.

The argument win specifies the spatial region in which the points will be generated. In your example you have specified this region to be a rectangle. You just need to replace this rectangle by the region in which you want the points to be generated.

You can construct spatial regions (objects of class owin) in many ways. See help(owin), or help(spatstat) for an overview.

In your example, you could build up the shape by forming the union of several rectangles. For example to make a simple cross shape, I could just write

require(spatstat)
A <- owin(c(-1,1), c(-4, 4))
B <- owin(c(-4,4), c(-1,1))
U <- union.owin(A, B)
plot(U)

Another way would be to specify the corners of the polygon shape and use W <- owin(poly=p) where p = list(x, y) contains the coordinates of the corners, listed in anticlockwise order without repetition. See help(owin).

This is also covered in Section 3.5 of the spatstat book. You can download Chapter 3 for free.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...