algorithm - Random points inside a parallelogram -
i have 4 side convex polygon defined 4 points in 2d, , want able generate random points inside it.
if simplifies problem, can limit polygon parallelogram, more general answer preferred.
generating random points until 1 inside polygon wouldn't work because it's unpredictable time takes.
a. if can restrict input parallelogram, simple:
- take 2 random numbers between 0 , 1. we'll call
u
,v
. if parallelogram defined points abcd such ab, bc, cd , da sides, take point being:
p = + (u * ab) + (v * ad)
where ab
vector b , ad
vector d.
b. now, if cannot, can still use barycentric coordinates. barycentric coordinates correspond, quad, 4 coordinates (a,b,c,d)
such a+b+c+d=1
. then, point p
within quad can described 4-uple such that:
p = a + b b + c c + d d
in case, can draw 4 random numbers , normalize them add 1. give point. note distribution of points not uniform in case.
c. can also, proposed elsewhere, decompose quad 2 triangles , use half-parallelogram method (i.e., parallelogram add condition u+v=1
) or barycentric coordinates triangles. however, if want uniform distribution, probability of having point in 1 of triangle must equal area of triangle divided area of quad.
Comments
Post a Comment