day2/koch.py
author Puneeth Chaganti <punchagan@fossee.in>
Fri, 02 Oct 2009 18:02:38 +0530
changeset 3 18a010e4caa0
parent 0 9243d75024cc
permissions -rwxr-xr-x
Added day2 session1.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
import pylab as P
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     3
def split_line(pt1, pt2):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     4
    """Given two complex numbers p1 and p2 return a list of the
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
    necessary points pairwise.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
    """
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
    # The points we generate are p1, p2, p3, p4 and p5
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
    p1, p5 =  pt1, pt2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
    diff = pt2 - pt1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    10
    segment = diff/3.0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
    p2 = p1 + segment
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
    p4 = p5 - segment
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    13
    # Now rotate a line given by 60degrees.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    14
    # Recall that complex multiplication does this easily.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
    # Complex numbers are defined by complex(a, b) also.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
    p3 = p2 + segment*complex(P.cos(P.pi/3), P.sin(P.pi/3))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
    return [(p1, p2), (p2, p3), (p3, p4), (p4, p5)]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    18
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
def koch(n):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
    start = complex(0, 0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
    end = complex(1, 0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
    points = [(start, end)]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
    for level in range(n):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
        new_points = []
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
        for (p1, p2) in points:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
            new_points.extend(split_line(p1, p2))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
        points = new_points
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
    points.insert(0, (start, points[0][0]) )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
    points.append((points[-1][1], end))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
    return points
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
def plot_koch(n):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
    points = koch(n)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
    x = [t[0].real for t in points]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
    y = [t[0].imag for t in points]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
    P.plot(x, y, 'r-')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
    P.ylim(0, 1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
    P.xlim(0, 1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40