Python - 使用arange绘制导致重复点的点

时间:2018-04-05 16:41:40

标签: python-3.x numpy

我试图通过使用arange函数和numpy.stack来创建一组顶点的2D点,以填充列表,然后在最后将其转换为数组。

然后,我接受这些点并在第二个程序中将它们绘制在matplotlib中。不幸的是,而不是一系列形成盒子的线条,因为我预计似乎许多线条已被重复计算或根本不计算。

编写数组的代码如下:

import numpy as np

objectradius = 4
objectspace = 2     #Must be half the objectradius


boundingvertex = [180,180],[180,-180],[-180,-180],[-180,180]    #The vertices of the corners of the objects They must be given in clockwise or anti-clockwise order  
box1vertex = [-120,-120],[-120,-80],[40,-80],[40,-120]
box2vertex = [-120,0],[-160,0],[-160,160],[-120,160]
box3vertex = [-80,-40],[-40,-40],[-40,120],[-80,120]
box4vertex = [80,-120],[160,-120],[160,80],[80,80]

vertexlist = boundingvertex + box1vertex + box2vertex + box3vertex + box4vertex
vertices = np.asarray(vertexlist) #Converts vertices list to array
segments = []

for i in range (5): #For each object
    objectnum = i + 1
    for k in range(4):  #For each corner vertex
        start = vertices[objectnum + k] 
        end = vertices[objectnum + (k+1)%4]
        if start[0] == end[0]:  #If they have equal x
            if start[1]<end[1]: #If the start point is bigger (more positive) than the end it will fail, so we have to reorder the points
                #print(i,k)
                #print(start,end,"equal x")
                templist = np.arange(start[1],end[1],objectspace)   #Evenly spaces points between the two points
                pointnumber = np.size(templist)
                if pointnumber == 0:
                    print(i,k,"went wrong here")
                    break
                coord = np.full(pointnumber,start[0])   #Makes an equally long array of the x co-ordinate
                newlist = np.stack((templist,coord),axis=-1)    #Takes the templist points and adds the x co-ordinate onto them.
                segments.append(newlist)
                #print (segments)
            else:
                #print(i,k)
                #print(start,end,"equal x")
                templist = np.arange(end[1],start[1],objectspace)
                pointnumber = np.size(templist)
                if pointnumber == 0:
                    print(i,k,"went wrong here")
                    break
                coord = np.full(pointnumber,start[0])
                newlist = np.stack((templist,coord),axis=-1)
                segments.append(newlist)
                #print (segments)
        else:
            if start[0]<end[0]:
                #print(i,k)
                #print(start,end,"equal x")
                templist = np.arange(start[0],end[0],objectspace)
                pointnumber = np.size(templist)
                if pointnumber == 0:
                    print(i,k,"went wrong here")
                    break
                coord = np.full(pointnumber,start[1])
                newlist = np.stack((coord,templist),axis=-1)
                segments.append(newlist)
                #print (segments)
            else:
                #print(i,k)
                #print(start,end,"equal x")
                templist = np.arange(end[0],start[0],objectspace)
                pointnumber = np.size(templist)
                if pointnumber == 0:
                    print(i,k,"went wrong here")
                    break
                coord = np.full(pointnumber,start[1])
                newlist = np.stack((coord,templist),axis=-1)
                segments.append(newlist)
                #print (segments)

segments.append(vertices)
finalpoints = np.asarray(segments)

从这段代码中,我将最终点的每个元素的数组值粘贴到一个文本文件中,然后由第二个程序读取以绘制点。

运行第二个程序并绘制点时,结果如下sample screenshot。很明显,顶点正确绘制,但是arange点没有正确绘制。非常感谢任何帮助。

0 个答案:

没有答案