基质内的螺旋

时间:2018-09-25 00:39:03

标签: python-3.x matrix

我试图获得一个矩阵,其行为类似于在使用matplotlib生成的这张图片中,但是我很难使它按顺序进行。 this picture 这是我正在使用的代码,它在外圈中找到一个点(一个圆的四分之一),并在将它们扩展到完整的圆并尝试对所有内容进行排序后,将x,y的数组保存在X和Y中。我的问题是,每当删除第55和61行上的reversed()时,我都会遇到内存错误。 这是正在使用的代码,如何修复以生成红色绘制的螺旋形?我真正想做的只是x,y的序列和该行显示的环数。

更新: 我更改了代码以计算X,Y通过theta和thr半径的运动,但是我得到了很多重复的x和y值。我不知道如何删除此技术生成的双打,即 我有:

X = 0, 1 ,2,3,4,5, 1 ,5

Y = 2, 5 ,4,6,4,5, 5 ,8

在这种情况下,我想删除列表X和Y上的第二项。如何执行此操作,因为我无法弄清楚。这是我正在使用的代码:

import matplotlib.pyplot as plt
import numpy as np
from random import randint
from math import sin, cos

width, height = 20, 20
a, b = int((width/2)), int((height/2))
r = int((width/2)-1)
#EPSILON = 1
vert=1
total = 0
datapoints=[]
X=[]
Y=[]


map_ = [[0 for x in range(width)] for y in range(height)]
while(r >=0):
    dataValue= a+r
    horiz=0
    if r <2:
        EPSILON = 1+(r)
    if r >=2 & r<=4:
        EPSILON = 1+(2*r)
    if r >=4:
        EPSILON = 20#1+(2*r/10)+0.3
    # draw the circle
    for T in range(360):
        x = int(cos(T)*r)
        y = int(sin(T)*r)
        if len(X) == 0 or((x is not X[-1]) & (y is not Y[-1])):
            # see if we're close to (x-a)**2 + (y-b)**2 == r**2
            if abs((x)**2 + (y)**2 - r**2) < EPSILON**2:
                if abs(x) < width:
                    if abs(y) <= height:
                        if len(X)!=0:
                            if x==X[-1] & y==Y[-1]:
                                del X[-1]
                                del Y[-1]
                                horiz -= 1
                                total -=1
                        index = 0
                        while(index<len(X)):
                            if (X[index] == x) & (Y[index] == y):
                                del X[index]
                                del Y[index]
                                horiz -= 1
                                total -=1
                            index +=1
                            
                        
                        if T <= 90:
                            X.append(x+a)
                            Y.append(y+b)
                        if T >90 & T <= 180:
                            X.append(-x+a)
                            Y.append(y+b)
                        if T >180 & T <= 260:
                            X.append(-x+a)
                            Y.append(-y+b)
                        if T >260 & T <= 379:
                            X.append(x+a)
                            Y.append(-y+b)
                        else:
                            pass
                        
                        
                        #map_[y][x] = r  ## later here is where the data will go...
                        horiz += 1
                        total+=1

    ## datapoints is data per circle
    datapoints.append(horiz)
    r -= 1
    vert+=1

for m in range(len(X)):
    if(m == 1):    
        map_[Y[m]][X[m]] = 10
    else:
        map_[Y[m]][X[m]] = m
print(datapoints, total)
fig=plt.imshow(map_, interpolation='nearest', cmap=plt.cm.bone)
plt.grid(True)
plt.show()

旧代码

import matplotlib.pyplot as plt
import numpy as np
from random import randint

width, height = 20, 20
a, b = int((width/2)), int((height/2))
r = int((width/2)-1)
#EPSILON = 1
vert=1
total = 0
datapoints=[]
X=[[]]
Y=[[]]

##width, height = 11, 11
##a, b = 5, 5
##r = 5
##EPSILON = 2.2

map_ = [[0 for x in range(width)] for y in range(height)]
for i in range(1,r):
    X.append([])
    Y.append([])
while(r >=0):
    dataValue= a+r
    horiz=0
    if r <2:
        EPSILON = 1+(r/10)
    if r >=2 & r<=4:
        EPSILON = 1+(2*r/10)
    if r >=4:
        EPSILON = 1+(2*r/10)+0.3
    # draw the circle
    for y in range(height):
        for x in range(width):
            # see if we're close to (x-a)**2 + (y-b)**2 == r**2
            if abs((x-a)**2 + (y-b)**2 - r**2) < EPSILON**2:
                if(x >= a) & (y <= b):
                    X[r-1].append(x)
                    Y[r-1].append(y)
                
                #map_[y][x] = dataValue  ## later here is where the data will go...
                horiz += 1
                total+=1

    ## datapoints is data per circle
    datapoints.append(horiz)
    r -= 1
    vert+=1
xx = []
yy = []
for i in X:
    print(i)
    try:
        for j in reversed(i):
            i.append(2*a-j)
    except:
        pass
for i in Y:
    try:
        for j in reversed(i):
            i.append(j)
    except:
        pass
for i in X:
    print(i)
    try:
        for j in reversed(i):
            i.append(j)
    except:
        pass
for i in Y:
    try:
        for j in reversed(i):
            i.append(2*a-j)
    except:
        pass

for i in reversed(X):
    try:
        for j in reversed(i):
            xx.append(j)
    except:
         xx.append(i)
for i in reversed(Y):
    try:
        for j in reversed(i):
            yy.append(j)
    except:
        yy.append(i)
for i in range(len(xx)): 
    map_[yy[i]][xx[i]] = i

'''
for i in range(len(datapoints)):
    for j in range(datapoints[i]):
        stepper(datapoints[i])
    ##Control verticle
    ## Make sure its 90 deg
    if i == len(datapoints):
        pass
    else:
        stepper(i*(1024)/vert)
'''
print(vert)
print(total)
fig=plt.imshow(map_, interpolation='nearest', cmap=plt.cm.bone)
#CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.bone, origin=origin)
#cbar = fig1.colorbar(CS)
#cbar.ax.set_ylabel('verbosity coefficient')

plt.grid(True)
plt.show()

0 个答案:

没有答案