美文网首页
2020-08-16

2020-08-16

作者: 惬意摩奇 | 来源:发表于2020-08-16 16:47 被阅读0次

    import numpyas np

    import matplotlib.pyplotas plt

    def BresenhamLine(img,x1, y1, x2,  y2):

    pointList = []

    y_neg =False

        dx = x2 - x1

    if (dx <0):#向左走,就将起点终点调换

            temp = x2

    x2 = x1

    x1 = temp

    temp = y2

    y2 = y1

    y1 = temp

    dx = x2 - x1# 重新计算dx

        dy = y2 - y1

    if (dy <0):# 向下走,就将计算出来的y加个负号

            y_neg=True

            dy=-dy

    x = x1

    y = y1#从起点开始

        if(dx==0):#打竖画线,向左走换了(x1,y1),(x2,y2),向下走没有换,注意

            y=min(y1,y2)

    y_end=max(y1,y2)

    while(y<=y_end):

    img[x, y] =1

                y+=1

                pointList.append([x, y])

    return img,pointList

    elif(dy==0):#打横画线

            while (x <= x2):

    img[x, y] =1

                x+=1

                pointList.append([x, y])

    return img,pointList

    else:

    k = dy /(1.0*dx)#斜率

        if (abs(k) <=1):#画横斜线,|斜率|<=1

            sign_k =True  # 斜率标记

            xend = x2

    else:#| 斜率|>1

            sign_k =False# 等会关于y=x对称,计算|斜率|<=1的值,画|斜率|>1的画

            #先打横了来算,画的时候才对称

            xend = x1+dy

    temp=dy

    dy=dx

    dx=temp

    k = dy / (1.0*dx)# 斜率

        p =2 * dy - dx#P0 = 2*dy - dx

        v =2 * dy

    u = p - dx#Pk+1 = Pk + 2*dy - 2*dx,u 表示2*dy - 2*dx

    ############开始画线###################3

        b=y1-x1

    while ( x <= xend):

    if (sign_k ==True):#画横斜线,|斜率|<=1

                if(y_neg==True):#向下走,画横线,只需要关于y=y0上下翻转就好了

                    #SetPixel(hDC, x, y, color);

                    img[x, 2*y1-y] =1#(x,-y)

                    pointList.append([x, y])

    else:#向上走,画横线,最正常了

                    img[x, y] =1#(x,y)

                    pointList.append([x, y])

    else :#画竖斜线,|斜率|>1

    #SetPixel(hDC, y, x, color);

                if (y_neg ==True):#画竖斜线,又向下走,要先关于y=kx+b对称,之后再关于y=y0上下翻转

                    # SetPixel(hDC, x, y, color);

                    img[y-b,  2*y1-(x+b)] =1#(y,-x)

                    pointList.append([x, y])

    else:#向上走,画竖斜线,关于y=kx+b对称

                    img[y-b, x+b] =1#(y,x)

                    pointList.append([x, y])

    if (p>0):

    y+=1

                p = p + u

    #cout << "1 ";

            else:

    p = p + v

    #//cout << "0 ";

            x+=1

        return img,pointList

    if __name__ =='__main__':

    img = np.full([100,100],np.nan)

    # plt.imshow(img)

    # plt.show()

        line = []

    count =0

        for iin range(img.shape[0]):

    for jin range(img.shape[1]):

    if np.sqrt((i-50)**2+(j-50)**2)<50 and np.sqrt((i-50)**2+(j-50)**2)>48  and img[i,j]!=1:

    img,pointList = BresenhamLine(img, i, j, 50, 50)

    line.append(pointList)

    for iin range(img.shape[0]):

    for jin range(img.shape[1]):

    if np.sqrt((i-50)**2+(j-50)**2)<50 and img[i,j]!=1:

    img,pointList = BresenhamLine(img, i, j, 50, 50)

    line.append(pointList)

    print(len(line))

    plt.imshow(img)

    plt.show()

    相关文章

      网友评论

          本文标题:2020-08-16

          本文链接:https://www.haomeiwen.com/subject/igatjktx.html