Py学习  »  Python

试着用python制作一个线条投射器

Wemmons • 3 年前 • 1286 次点击  

您好,我正在尝试为那些不知道这是什么的人制作一个直线投射器,它会给你2点之间的所有点,在这种情况下,我会将这些数据写入一个图像,但由于某种原因,当我运行它时,它只会向右转(如果你运行代码,你会看到我的意思),有人知道这是为什么吗 这是我的代码,我非常清楚它很凌乱,效率也不高,所以我们欢迎您提出建议,让它更有效或更不凌乱

import cv2 as cv
import numpy as np
dim_x = 50
dim_y = 50
x1 = 8
x2 = 40
y1 = 8
y2 = 30
count = 0
def calc(cord, target):
    x = abs(cord[0] - target[0])
    y = abs(cord[1] - target[1])
    return(abs(x+y))

def starter(x1,x2,y1,y2):
    x = abs(x1-x2)
    y = abs(y1-y2)
    z = x1-x2
    if z == abs(z):
        right = 1
    else:
        right = 0
    return(x+y, right)
distance,direction = starter(x1,x2,y1,y2)
# for direction 1 = left and 0 = right
print(distance,direction)
pic = np.zeros((dim_y,dim_x))
pic[x1,y1] = 255
pic[x2,y2] = 255
while True:
   
    top_r = (x1+1,y1+1)
    mid_r = (x1+1,y1)
    but_r = (x1+1,y1-1)
    top = (x1,y1+1)
    mid = (x1,y1)
    but= (x1,y1-1)
    top_l = (x1-1, y1+1)
    mid_l = (x1-1, y1)
    but_l = (x1-1,y1-1)
    top_r2 = calc(top_r,np.array((x2,y2)))
    mid_r2 = calc(mid_r,np.array((x2,y2)))
    but_r2 = calc(but_r,np.array((x2,y2)))
    top2 = calc(top,np.array((x2,y2)))
    mid2 = calc(mid,np.array((x2,y2)))
    but2 = calc(but,np.array((x2,y2)))
    top_l2 = calc(top_l,np.array((x2,y2)))
    mid_l2 = calc(mid_l,np.array((x2,y2)))
    but_l2 = calc(but_l,np.array((x2,y2)))
    if top_r2 < mid_r2 and top_r2 < but_r2 and top_r2 < top2 and  top_r2 < mid2 and top_r2 < but2 and top_r2 < top_l2  and top_r2 < mid_l2 and top_r2 < but_l2:
        print("top")
        pic[top_r] = 255
        x1 = top_r[0]
        y1 = top_r[1]
        
    elif mid_r2 < mid_r2 and mid_r2 < but_r2 and mid_r2 < top2 and mid_r2 < top_r2 and mid_r2 < but2 and mid_r2 < top_l2  and mid_r2 < mid_l2 and mid_r2 < but_l2:
        print("mid")
        pic[mid_r] = 255
        x1 = mid_r[0]
        y1 = mid_r[1]
    elif but_r2 < mid_r2 and mid_r2 < top_r2 and mid_r2 < top2 and mid_r2 < mid2 and mid_r2 < but2 and  mid_r2 < top_l2  and mid_r2 < mid_l2 and mid_r2 < but_l2:
        print("but")
        pic[but_r] = 255
        x1 = but_r[0]
        y1 = but_r[1]
    elif top2 < mid_r2 and top2 < but_r2 and top2 < top_r2 and top2 < mid2 and top2 < but2 and top2 < top_l2 and top2 < mid_l2 and top2 < but_l2:
        print("top2")
        pic[top] = 255
        x1 = top[0]
        y1 = top[1]
    elif mid2 < mid_r2 and mid2 < but_r2 and mid2 < top2 and mid2 < mid_r2 and mid2 < but2 and mid2 < top_l2  and mid2 < mid_l2 and mid2 < but_l2:
        print("mid2")
        pic[mid] = 255
        x1 = mid[0]
        y1 = mid[1]
        break
    elif but2 < mid_r2 and but2 <  top_r2 and but2 <  top2 and but2 <  mid2 and but2 <  but_r2 and but2 <  top_l2  and but2 <  mid_l2 and but2 <  but_l2:
        print("but2")
        pic[but] = 255
        x1 = but[0]
        y1 = but[1]
    elif top_l2 < mid_r2 and top_l2 < but_r2 and top_l2 < top2 and top_l2 < mid2 and top_l2 < but2 and top_l2 < top_r2  and top_l2 < mid_l2 and top_l2 < but_l2:
        print("top3")
        pic[top_l] = 255
        x1 = top_l[0]
        y1 = top_l[1]
    elif mid_l2 < mid_r2 and mid_l2 < but_r2 and mid_l2 < top2 and mid_l2 < top_r2 and mid_l2 < but2 and mid_l2 < top_l2  and mid_l2 < mid_r2 and mid_l2 < but_l2:
        print("mid4")
        pic[mid_l] = 255
        x1 = mid_l[0]
        y1 = mid_l[1]
    elif  but_l2 < mid_r2 and but_l2 < top_r2 and but_l2 < top2 and but_l2 < mid2 and but_l2 < but2 and but_l2 < top_l2  and but_l2 < mid_l2 and but_l2 < but_r2:
        print("but5")
        pic[but_l] = 255
        x1 = but_l[0]
        y1 = but_l[1]
    print(x1,y1)
    if count == 0:
        break
    count +=1
cv.imwrite("frame.png", pic)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131943
 
1286 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Tim Roberts
Reply   •   1 楼
Tim Roberts    4 年前

这是你在两点之间划一条线的方法。这是基于Bresenham的算法。你知道哪个方向变化最大,这就是你的“主要”方向。然后,在每一步,你都会朝着主方向移动一个。你保留一个“误差项”,它跟踪你需要在“小”方向上移动多远,当它穿过整个单元时,你也会在小方向上移动。还要注意的是,在numpy术语中,Y坐标(行)应始终位于第一位。

import cv2 as cv
import numpy as np
dim_x = 50
dim_y = 50

x1,y1 = (8,8)
y2,y2 = (40,30)

# Are we increasing in X?

deltax = x2 - x1
deltay = y2 - y1

dirx = 1 if deltax > 0 else -1
diry = 1 if deltay > 0 else -1

# Which direction is major?

if deltax > deltay:
    steps = deltax
    xmajor = 1
else:
    steps = deltay
    xmajor = 0

error = 0

pic = np.zeros((dim_y,dim_x))
pic[y1,x1] = 255
pic[y2,x2] = 255
for count in range(steps):
    if xmajor:
        x1 += dirx
        error += deltay
        if error >= deltax:
            y1 += diry
            error -= deltax
    else:
        y1 += diry
        error += deltax
        if error >= deltay:
            x1 += dirx
            error -= deltay
    pic[y1,x1] = 255
    print(x1,y1)
cv.imwrite("frame.png", pic)