This is my code.
from visual import *
s=[]
n=20
num_lines = sum(1 for line in open('G:\Dropbox\Programming\Fortran\Source\Assign2\pos_x.dat'))
loop=num_lines/n
f=open('G:\Dropbox\Programming\Fortran\Source\Assign2\pos_x.dat','r')
box(pos=[10,10,10],length=20,width=20,height=20,opacity=0.3,color=color.white)
for i in range(0,n-1):
line=f.readline()
row = line.split()
x = float(row[0])
y = float(row[1])
z = float(row[2])
s.append(sphere(pos=[x,y,z],radius=1,color=color.cyan))
for i in range(1,loop-1):
rate(100)
for j in range(0,n-1):
line=f.readline()
row = line.split()
x = float(row[0])
y = float(row[1])
z = float(row[2])
s[j].pos = [x,y,z]
A few spheres(not all of them) have flickering when they move on the display. How do I reduce the flickering? Here is the pos_x.dat
I tested your code with your supplied data and your problem lies within a misunderstanding of the range
function.
Your problem is that in Python the upper bound you supply as the second argument for range
is not included in the resulting range (i.e. range(1,4)
returns [1,2,3]
).
Your data is in the format that in each block of 20 lines are the positions of 20 spheres in a frame. If you do for j in range(0,n-1):
it's the same as for j in range(0, 19):
so you only iterate over 19 data points (indices 0 to 18) instead of the 20 you need per frame, which leads to flicker because at the next frame the data you'd like to use for your first sphere is in fact the data of the last sphere you forgot in the last frame.
The solution is to remove the -1
from the second argument of all your calls to the range
function. For example change the line
for i in range(0,n-1):
to
for i in range(0, n):
and so on.