Reputation: 826
The final values are different from than during individual key fills, when key "0" is getting filled with
{0: (0, 0), 1: (1, 0), 2: (2, 0), 3: (3, 0), 4: (4, 0),
5: (5, 0), 6: (6, 0), 7: (7, 0), 8: (8, 0), 9: (9, 0),
10: (10, 0), 11: (11, 0), 12: (12, 0)
etc; these values are changed once the outer while loop is fully completed. They turn into:
{0: (0, 0), 1: (0, 0), 2: (-1, 0), 3: (-2, 0), 4: (-3, 0),
5: (-4, 0), 6: (-5, 0), 7: (-6, 0), 8: (-7, 0), 9: (-8, 0),
10: (-9, 0), 11: (-10, 0), 12: (-11, 0)
I'm not able to figure out why. Any help is much appreciated. You can uncomment the print statement for reference.
The code:
import numpy as np
from math import cos,sin,pi
ANGLE_ACCURACY = 0.5
noOfAnglesLimit = (360.0/ANGLE_ACCURACY)*0.5
angle = 0
polarLut = dict()
inner = dict()
while angle<noOfAnglesLimit:
theta = (pi/180.0)*(angle*ANGLE_ACCURACY)
radius = 0
while radius<=200:
x = int(float(radius)*cos(theta)+0.5)
y = int(float(radius)*sin(theta)+0.5)
inner[radius] = (x,y)
radius+=1
polarLut[angle] = inner
#print(polarLut)
angle+=1
import json
with open('file.txt', 'w') as file:
file.write(json.dumps(polarLut))
Upvotes: 0
Views: 50
Reputation: 169407
You only have one inner
dictionary, which you assign to all values of the polarLut
dictionary, then go on modifying it again for the next angle
.
You'll want something like
polarLut = dict()
while angle < noOfAnglesLimit:
theta = (pi / 180.0) * (
angle * ANGLE_ACCURACY
)
inner = dict()
radius = 0
while radius <= 200:
x = int(float(radius) * cos(theta) + 0.5)
y = int(float(radius) * sin(theta) + 0.5)
inner[radius] = (x, y)
radius += 1
polarLut[angle] = inner
angle += 1
instead.
Also, I would suggest using for angle in range(noOfAnglesLimit)
instead, it's more Pythonic.
Upvotes: 3
Reputation: 22043
You are always storing the same dictionary inside polarLut
, so all the entries point to the same original inner
.
Move the declaration of inner
inside your while loop.
Upvotes: 1