Nugroho's blog.: Iterasi di Python

Wednesday, April 27, 2016

Iterasi di Python


Jika kita punya sebuah list bernama bola yang didefinisikan sebagai:

bola = []

kemudian kita membuat obyek bernama ball

ball = sphere (pos=(uniform(1,7),0,uniform(-7,7)), radius=.3, color (uniform(0,1),uniform(0,1),uniform(0,1)))
ball.vx  = uniform(-7,7)
ball.vy  = uniform(-7,7)
ball.vz  = uniform(-7,7)
 
Obyek ini kita masukkan ke dalam bola dengan perintah

bola.append(ball)

Kita dapat melakukannya berkali-kali sehingga pada list bola terdapat beberapa obyek bernama ball

Jika kita ingin mengakses obyek tersebut, kita dapat menggunakan perintah semacam

bola[0].vx = 1

jika kita ingin mengakses vx di semua obyek bola, kita dapat menggunakan iterasi

for i in arange (n):
  bola[i].vx = 1


Namun di python ada cara lain yang juga mudah

for ball in (bola):
  ball.vx = 1

Berikut contoh kode yang menggunakan iterasi seperti itu

from visual import *
from random import uniform,random
from visual.controls import *

def change():
global jalan,vx,vy,vz
if b.value:
jalan = True
for ball in(bola):
ball.vx = uniform(-7,7)
ball.vy = uniform(-7,7)
ball.vz = uniform(-7,7)
else:
jalan = False

c = controls(title='Tempat Tombol',x=800, y=0, width=300, height=300, range=50)
b = toggle( pos=(0,0), width=20, height=20, text='Click me', action=lambda: change() )
display(center=(0,0,0),background=(1,1,1), #autoscale=False,
width=600, height=600, forward=(-0.4,-0.3,-1)) #arah kamera
g = -1.
dt = .1
e = 1.
b.value = True
jalan = True
l = 17.
dl = .01
n = 11

distant_light(direction=(1,1,1), color=color.red)
lantai = box(color=color.white, pos=(0,0,0),length=l,height=dl, width=l, opacity=.3)
dindingKiri = box(color=color.white, pos=(-l/2,l/2,0),length=dl,height=l, width=l, opacity=.3)
dindingKanan= box(color=color.white, pos=(l/2,l/2,0),length=dl,height=l, width=l, opacity=.3)
dBelakang = box(color=color.white, pos=(0,l/2,-l/2),length=l,height=l, width=dl, opacity=.3)
atap = box(color=color.white, pos=(0,l,0),length=l,height=dl, width=l, opacity=.3)
bola = []
for i in arange(n):
ball = sphere (pos=(uniform(1,7),0,uniform(-7,7)), radius=.3, color=(uniform(0,1),uniform(0,1),uniform(0,1)))
ball.vx = uniform(-7,7)
ball.vy = uniform(-7,7)
ball.vz = uniform(-7,7)
bola.append(ball)

def proses():
global vx,vy,vz
for ball in (bola):
a = g
ball.vy += a*dt
ball.pos+= vector(ball.vx*dt,ball.vy*dt,ball.vz*dt)
tumbukan()

def tumbukan():
global vx,vy,vz
for ball in(bola):
if ball.y<0:
ball.y = 0.01
ball.vy *=-1.*e
elif ball.y>l:
ball.y = l-.01
ball.vy *= -1
if ball.x<-l/2:
ball.x=-l/2+.01
ball.vx *= -1*e
if ball.x>l/2:
ball.x=l/2-.01
ball.vx *= -1*e
if ball.z<-l/2:
ball.z=-l/2+.01
ball.vz *= -1*e
if ball.z>l/2:
ball.z=l/2-.01
ball.vz *= -1*e

while 1:
rate (100)
if jalan:
proses()


.




No comments:

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)