Welcome, guest | Sign In | My Account | Store | Cart

Red curve: Helix ; Blue curve: super-helix (helix around another helix) ; Green curve: hyper-helix?

Python, 50 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# SuperHelix
# FB - 201007213
import math
from PIL import Image
imgx = 800
imgy = 800
image = Image.new("RGB", (imgx, imgy))

r0 = float(300) # radius of the 1. helix
r1 = float(30)  # radius of the 2. helix
r2 = float(10)  # radius of the 3. helix

ctr = 0
while True:
    ctr += 1
    
    # 1. helix point
    a0 = float(ctr) / r0 / 10
    x0 = r0 * math.cos(a0)
    y0 = r0 * math.sin(a0)
    z0 = float(ctr) * 0.01

    # plot the 1. helix point
    if z0 > imgy - 1: break
    image.putpixel((x0 + imgx / 2, z0), (255, 0, 0))
    
    # 2. helix point (superhelix)
    a1 = float(ctr) / r1 / 2
    x1 = x0 + r1 * math.cos(a1) * math.cos(a0)
    y1 = y0 + r1 * math.cos(a1) * math.sin(a0)
    z1 = z0 + r1 * math.sin(a1)

    # plot the 2. helix point
    if z1 < 0: z1 = 0
    if z1 > imgy - 1: break
    image.putpixel((x1 + imgx / 2, z1), (0, 0, 255))

    # 3. helix point (hyperhelix?)
    a2 = float(ctr) / r2
    x2 = x1 + r2 * math.cos(a2) * math.cos(a1)
    y2 = y1 + r2 * math.cos(a2) * math.sin(a1)
    z2 = z1 + r2 * math.sin(a2)

    # plot the 3. helix point
    if z2 < 0: z2 = 0
    if z2 > imgy - 1: break

    image.putpixel((x2 + imgx / 2, z2), (0, 255, 0))

image.save("SuperHelix.png", "PNG")

1 comment

Martin Miller 13 years, 7 months ago  # | flag

I had to convert each component of the coordinate passed in the first tuple argument to all the image.putpixel() method calls into integer to avoid a TypeError exception. For example the first one:

image.putpixel((x0 + imgx / 2, z0), (255, 0, 0))

changed to:

image.putpixel((int(x0 + imgx / 2), int(z0)), (255, 0, 0))

Even with this extra overhead, it still creates the image quickly.

Created by FB36 on Wed, 21 Jul 2010 (MIT)
Python recipes (4591)
FB36's recipes (148)

Required Modules

Other Information and Tasks