python - Multidimensional Array sets multiple rows/columns -
so having strange problem in python. using code below create plot of places object has been. here code:
def goforward(self, duration): if (self.towardsx - self.x == 0): myro.robot.motors(self.speed, self.speed) myro.wait(abs(duration)) myro.robot.stop() #get amount of points forward divisible = (int) (duration / self.scale) #add them direction self.towardsy += divisible tempy = self.y y in xrange(self.y, divisible + tempy): if (y % self.scale == 0): self.plot[(int) (self.x)][y] = 1 return #calc slope slope = (self.towardsy - self.y) / (self.towardsx - self.x) tempx = self.x tempy = self.y #go forward #get amount of points forward divisible = duration / self.scale #add them direction self.towardsx += divisible self.towardsy += divisible xs = [] ys = [] x in xrange(self.x, tempx + divisible): #find out if plottable point if (((slope * (x - self.x)) + self.y) % self.scale == 0.0): xs.append(x) ys.append((int)((slope * (x - self.x)) + self.y)) #plot points in xrange(0, len(xs)): j in xrange(0, len(ys)): if (self.plot[xs[i]][ys[j]] == 0): self.plot[xs[i]][ys[j]] = 1 self.x += divisible self.y += divisible
but, when call goforward(2)
fills 5 columns ones, instead of few points. example:
[[0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0] [0,0,0,0,1,1,0,0,0,0]]
based off parameter given goforward(n)
creates many columns full of 0s... why behavior happening? code should not produce effect, inexperienced python why happening? in advance
edit
so have changed code plotting points
for in xrange(0, len(xs)): if (self.plot[xs[i]][ys[i]] == 0): self.plot[xs[i]][ys[i]] = 1
which have correct values, still producing strange behavior, , problem lies in code here.
edit 2
when use code:
self.plot[3][3] = 1
it still produces array of:
[[0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]]
so produce grid showing printing self.plot
? , saying grid gets initialized 0? self.plot
? list of lists , that's it? when print self.plot
before running loop print expect (which assume should zeros)?
so if xs
, ys
points should 1 simplify code using 1 list of plottable points:
plottable_points = [] # loop plottable_points.append( (x, int((slope * (x - self.x)) + self.y)) ) x, y in plottable_points: self.plot[x][y] = 1
i'm not sure how self.plot
being initialized or used, if print things before , after each step should able figure out logic wrong.
edit 1: little python tip:
for x, y in zip(xs, ys): self.plot[x][y] = 1
does same thing first code example does, variables.
edit 2:
the problem how initializing self.plot
. when repeat list that, outer list becomes list of pointers...all pointing same object (in case list). when self.plot[3][3] = 1
, setting column in each row. try initializing self.plot
(there may better way this, i'm tired):
self.plot = [] col in range(height * multiplyby): self.plot.append([0] * width * multiplyby) # or: self.plot = [ [0] * width * multiply col in range(height * multiplyby) ]
Comments
Post a Comment