Example #1
0
def main():
    hurstExponent = float(sys.argv[1])
    stddraw.setPenRadius(0.0)
    stddraw.clear(stddraw.LIGHT_GRAY)
    scaleFactor = 2 ** (2.0 * hurstExponent)
    curve(0, .5, 1.0, .5, .01, scaleFactor)
    stddraw.show()
Example #2
0
def plotLines(a):
    """
    Plot the values of array 'a' as line end-points.
    """
    N = len(a)
    stddraw.setXscale(0, N-1)
    stddraw.setPenRadius()
    for i in range(1, N):
        stddraw.line(i-1, a[i-1], i, a[i])
Example #3
0
def plotPoints(a):
    """
    Plot the values of array 'a' as points.
    """
    N = len(a)
    stddraw.setXscale(0, N-1)
    stddraw.setPenRadius(1.0 / (3.0 * N))
    for i in range(N):
        stddraw.point(i, a[i])
Example #4
0
def plotPoints(a):
    """
    Plot the elements of array a as points.
    """
    n = len(a)
    stddraw.setXscale(-1, n)
    stddraw.setPenRadius(1.0 / (3.0 * n))
    for i in range(n):
        stddraw.point(i, a[i])
Example #5
0
def plotLines(a):
    """
    Plot the elements of array a as line end-points.
    """
    n = len(a)
    stddraw.setXscale(-1, n)
    stddraw.setPenRadius(0.0)
    for i in range(1, n):
        stddraw.line(i-1, a[i-1], i, a[i])
Example #6
0
def tree(n, x, y, a, branchRadius):
    cx = x + math.cos(a) * branchRadius
    cy = y + math.sin(a) * branchRadius
    stddraw.setPenRadius(.001 * (n ** 1.2))
    stddraw.line(x, y, cx, cy)
    stddraw.show()
    if (n == 0):
        return

    tree(n-1, cx, cy, a + BEND_ANGLE - BRANCH_ANGLE, \
        branchRadius * BRANCH_RATIO)
    tree(n-1, cx, cy, a + BEND_ANGLE + BRANCH_ANGLE, \
        branchRadius * BRANCH_RATIO)
    tree(n-1, cx, cy, a + BEND_ANGLE, \
        branchRadius * (1 - BRANCH_RATIO))
Example #7
0
def main():
    n = int(sys.argv[1])

    cx = [0.000, 1.000, 0.500]
    cy = [0.000, 0.000, 0.866]

    x = 0.0
    y = 0.0

    stddraw.setPenRadius(0.0)
    for i in range(n):
        r = stdrandom.uniformInt(0, 3)
        x = (x + cx[r]) / 2.0
        y = (y + cy[r]) / 2.0
        stddraw.point(x, y)
    stddraw.show()
Example #8
0
def main():
    n = int(sys.argv[1])
    dist = stdarray.readFloat1D()
    cx = stdarray.readFloat2D()
    cy = stdarray.readFloat2D()
    x = 0.0
    y = 0.0
    stddraw.setPenRadius(0.0)
    for i in range(n):
        r = stdrandom.discrete(dist)
        x0 = cx[r][0]*x + cx[r][1]*y + cx[r][2]
        y0 = cy[r][0]*x + cy[r][1]*y + cy[r][2]
        x = x0
        y = y0
        stddraw.point(x, y)
    stddraw.show()
Example #9
0
def main(argv):
    n = float(argv[1])
    decay = float(argv[2])
    stddraw.createWindow()
    stddraw.setPenRadius(0)
    angle = 360.0 / n
    step = math.sin(math.radians(angle/2.0))
    t = turtle.Turtle(0.5, 0, angle/2.0)

    i = 0
    while i < 10.0 * 360.0 / angle:
        step /= decay
        t.goForward(step)
        t.turnLeft(angle)
        i += 1
    stddraw.show()
    stddraw.wait()
Example #10
0
def draw(pole):

    n = len(pole) - 1

    # Draw 3 poles.
    stddraw.clear()
    stddraw.setPenColor(POLE_COLOR)
    stddraw.setPenRadius(POLE_WIDTH)
    for i in range(3):
        stddraw.line(i, 0, i, n)

    # Draw n discs.
    discs = stdarray.create1D(3, 0)   # discs[p] = # discs on pole p
    for i in range(n, 0, -1):
        stddraw.setPenColor(DISC_COLOR)
        stddraw.setPenRadius(0.035)   # magic constant
        size = 0.5 * i / n
        p = pole[i]
        stddraw.line(p-size/2, discs[p], p + size/2, discs[p])
        discs[p] += 1

    stddraw.sleep(500)
    stddraw.show()
def main():

    stddraw.createWindow(1024, 256)
    stddraw.setPenRadius(0)
    stddraw.setXscale(0, _SAMPLES_PER_REDRAW)
    stddraw.setYscale(-0.75, +0.75)
    stddraw.show()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i - 24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    t = 0

    # The main input loop.
    while True:

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)

        # Plot
        stddraw.point(t % _SAMPLES_PER_REDRAW, sample)

        if t == (_SAMPLES_PER_REDRAW - 1):
            stddraw.show()
            stddraw.clear()
            t = 0

        t += 1
Example #12
0
#-----------------------------------------------------------------------
# plotfilter.py
#-----------------------------------------------------------------------

import stdio
import stddraw

# Plot the points read from standard input.

x0 = stdio.readFloat()
y0 = stdio.readFloat()
x1 = stdio.readFloat()
y1 = stdio.readFloat()

stddraw.createWindow()
stddraw.setXscale(x0, x1)
stddraw.setYscale(y0, y1)
stddraw.setPenRadius(0.001)

# Read and plot the points.
while not stdio.isEmpty():
    x = stdio.readFloat()
    y = stdio.readFloat()
    stddraw.point(x, y)

stddraw.show()
stddraw.wait()
Example #13
0
import stddraw
import math

# Draw an animation of the second, minute, and hour hands of an
# analog clock.
stddraw.createWindow()
t = 0
while True:

    # Remainder operator with floats so all hands move every second.
    seconds = t % 60
    minutes = (t / 60.0) % 60
    hours   = (t / 3600.0) % 12

    stddraw.clear()
    stddraw.setPenRadius()

    # Draw clock face.
    stddraw.setPenColor(stddraw.BLACK)
    stddraw.filledCircle(0.5, 0.5, 0.45)

    # Draw hour markers.
    stddraw.setPenColor(stddraw.BLUE)
    for i in range(12):
        theta = math.radians(i * 30)
        stddraw.filledCircle(0.5 + 0.4 * math.cos(theta), \
                             0.5 + 0.4 * math.sin(theta), .025)

    # Draw second hand.
    stddraw.setPenRadius(.01)
    stddraw.setPenColor(stddraw.YELLOW)
Example #14
0
 def draw(self):
     stddraw.setPenRadius(0.0125)
     stddraw.point(self._r[0], self._r[1])
Example #15
0
def main():
    n = int(sys.argv[1])
    stddraw.setPenRadius(0.0)
    draw(n, .5, .5, .5)
    stddraw.show()
# p[i][j] = prob. that surfer moves from page i to page j
p = stdarray.create2D(n, n, 0.0)
for i in range(n):
    for j in range(n):
        p[i][j] = stdio.readFloat()

# freq[i] = # times surfer hits page i
freq = stdarray.create1D(n, 0)

# Start at page 0.
page = 0
stddraw.createWindow()
stddraw.setXscale(-1, n)
stddraw.setYscale(0, t)
#stddraw.setPenRadius(.5/float(n))
stddraw.setPenRadius()
for i in range(t):

    # Make one random move.
    r = random.random()
    sum = 0.0;
    for j in range(n):
        # Find interval containing r.
        sum += p[page][j]
        if r < sum:
            page = j
            break
    freq[page] += 1

    if i % 1000 == 0:
        # Plot histogram of frequencies
Example #17
0
        return  
    koch(n-1, stepSize, myTurtle)
    myTurtle.turnLeft(60.0)
    koch(n-1, stepSize, myTurtle)
    myTurtle.turnLeft(-120.0)
    koch(n-1, stepSize, myTurtle)
    myTurtle.turnLeft(60.0)
    koch(n-1, stepSize, myTurtle)
 
# Accept integer n as a command-line argument. Plot a Koch curve of 
# order n to standard draw.

n = int(sys.argv[1])
stddraw.setCanvasSize(512, 256)
stddraw.setYscale(-.1, 0.4)
stddraw.setPenRadius(0.0)
stddraw.clear(stddraw.LIGHT_GRAY)
stepSize = 1.0 / (3.0 ** n)
myTurtle = Turtle(0.0, 0.0, 0.0)
koch(n, stepSize, myTurtle)
stddraw.show()

#-----------------------------------------------------------------------

# python koch.py 0

# python koch.py 1

# python koch.py 2

# python koch.py 3
Example #18
0
 def draw(self):
     stddraw.setPenRadius(0.025)
     stddraw.point(self._r.cartesian(0), self._r.cartesian(1))