简体   繁体   中英

How to propagate two vectors u and v to fill a rectangle box in an efficient way

For illustration purpose, see image below.

图片

Given a point p and two vectors u and v , we can get a linear combination of u and v , t = m*u + n*v , m and n are integer, and t should be within the box. Solve this problem is not too difficult. As m and n can both be positive and negative. It can be discussed:

  1. m > 0 and n > 0
  2. m > 0 and n < 0
  3. m < 0 and n > 0
  4. m < 0 and n < 0

Here is the python code for case 1:

m = 0 
n = 0 
t = m*u+n*v
x = t[0]
y = t[1]
l = []
while (x>0 and x < 1024 and y > 0 and y < 1024):
    l.append(t)
    m  = m + 1
    t = m*u+n*v
    x = t[0]
    y = t[1]
    while (x>0 and x < 1024 and y > 0 and y < 1024):
        l.append(t)
        n = n +1
        t = m*u+n*v
        x = t[0]
        y = t[1]

Using two loops for 4 sets may solve the problem.

Another way is generate too many points and then remove the points outside the box

I think maybe there is other simple and elegant way to do it?

Transform box into coordinate system defined with p as origin, and u as x direction of axis and v as direction of y axis. Result will be parallelogram. It is enough to find integer coordinates that are inside that parallelogram. That can be done by finding minimal and maximal m that is inside parallelogram, and searching for each m between minimal and maximal what range on n's are inside parallelogram.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM