samedi 19 avril 2014

How to iterate or deal with grid or coordinate in python


Vote count:

0




I have created a forest with random number in a grid of N x N. For creating the forest, I have taken each grid and a random number to plant a tree. And store the planted coordinate in a list li[ ] for x-axis and lj[ ] for y-axis. And also put the coordinate in a .dat file


In python, Here is my code:



from math import *
from random import random

n = input('Choose grid, N = ')
p= 0.5


li = []
lj = []

fid = open('plant.dat','w')
for i in range(1,n+1):
for j in range(1,n+1):
x = random()
if x<p:
li.append(i)
lj.append(j)

print>>fid,i,j
print (i,j),'is',1
else:
print (i,j),'is',0
print (i,j)
print (p,x,n),'and',n*n*p,li,lj


Now I want to fire it. I start with the bottom points and set them on fire. And from there the fire spread in the all four direction to the adjacent points in the forest. Not in diagonal direction.


for that I check where y-axis is 1, it is the bottom point, in the list lj[ ] and put that point as fired.


Now I have to spread the fire to the adjacent tree ( not in the diagonal) direction:


Here I have stuck and how to do this. Basically I want to follow the fired point in the bottom line and increase the value of x-axis and y-axis and check its adjacent point. But it shows only the bottom line.


Here is my code :



print 'Now we are firing'
lif=[]
ljf=[]
fid1 = open('fire.dat','w')
for b in range(len(lj)):
if lj[b] == 1:
print 'We fire here',(li[b],lj[b])
print>>fid1,li[b],lj[b]
lif.append(li[b])
ljf.append(lj[b])
#for e in range(2,n+1):
for r in li:
if li[r] == li:
print 'fire spread at',(li[r],lj[r])
print >> fid1,li[r],lj[r]
lif.append(li[r])
ljf.append(lj[r])


here is my plot for 'plant.dat' file , by gnuplot: http://ift.tt/1jiX75h


In the above picture , clearly fire can go up to a certain extend and stop, leaving many living trees.


I want the coordinate of that living trees only and plot it.


In this particular plot, my final plot with living trees would like the as follows, where partially erased points are fired and it would not be there: http://ift.tt/1eLnJyr



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire