Wednesday 16 May 2012

EXAMPLES BY EXAMPLES

I was bit slow at understanding all of the random codes, so I tried almost the examples from the "random" lecture PDF files.
Example 1 :
Comment : In linear form, the simplest one.
CODE :
-----------------------------------------------------------------------------------------------------------------
XRES = 600
YRES = 600
CX = XRES/2
CY = YRES/2
npts = 5000

import math

def setup():
    size(XRES,YRES)
    background(255)
    stroke(0,10)
    smooth()
    noLoop()
   
def draw():
    for i in range(npts):
        r = CX + random(-200,200)
        line(r,CY-100,r,CY+100)
-----------------------------------------------------------------------------------------------------------------

Example 2 :
Comment : Just applied list in and get a slightly different result from the first one.
CODE :
-----------------------------------------------------------------------------------------------------------------
 XRES = 600
YRES = 600
CX = XRES/2
CY = YRES/2
npts = 5000

buckets = [0.]
buckets = buckets*XRES

def setup():
    size(XRES, YRES)
    background(255)
    stroke(0,10)
    smooth()
    noLoop()
  
def draw():
    for i in range(npts):
        r = CX + random(-200,200)
        line(r,CY-100,r,CY+100)
        ir = int(r)
        buckets[ir] = buckets[ir] + 1
      
    stroke(0)
    for i in range (XRES):
        line(i,CY-200,i,CY-200-buckets[i])
-----------------------------------------------------------------------------------------------------------------

Example 3 :
Comment : Still the same as the second one but with a gradient effect.
CODE :
-----------------------------------------------------------------------------------------------------------------
 XRES = 600
YRES = 600
CX = XRES/2
CY = YRES/2
npts = 5000

buckets = [0.]
buckets = buckets*XRES

def setup():
    size(XRES, YRES)
    background(255)
    stroke(0,10)
    smooth()
    noLoop()
  
def draw():
    for i in range(npts):
        r = CX
        for j in range(12): r = r + random(-50,50)
        line(r,CY-100,r,CY+100)
      
    stroke(0)
    for i in range(XRES):
        line(i, CY-200, i, CY-200-buckets[i])
----------------------------------------------------------------------------------------------------------------- 


No comments:

Post a Comment