So I am having a strange problem in python. I am using the code below to create a plot of places where the object has been. Here is my 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 the amount of points forward
divisible = (int) (duration / self.Scale)
#add them to the direction
self.TowardsY += divisible
tempY = self.Y
for 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 the amount of points forward
divisible = duration / self.Scale
#add them to the direction
self.TowardsX += divisible
self.TowardsY += divisible
Xs = []
Ys = []
for x in xrange(self.X, tempX + divisible):
#find out if it is a 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 the points
for i in xrange(0, len(Xs)):
for 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 I call GoForward(2) it fills five columns with ones, instead of the 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 the parameter given to GoForward(n) it creates that many columns full of 0s... Why is this behavior happening? My code should not produce this effect, but I am inexperienced with python so is that why this is happening? Thanks in advance
EDIT
So I have changed the code for plotting the points to
for i in xrange(0, len(Xs)):
if (self.Plot[Xs[i]][Ys[i]] == 0):
self.Plot[Xs[i]][Ys[i]] = 1
Which will have the correct values, however it is still producing this strange behavior, and the problem lies in this code here.
EDIT 2
When I use the code:
self.Plot[3][3] = 1
It still produces an 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]]
divisible = (int) (duration / self.Scale)? You can't cast things like this in python.XsandYshave the correct values... so the problem lies in the plotting of the points... Ill look at it a bit more