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

3d Surface fitting to N random points. It draws the surface by converting z values to RGB colors.

Python, 63 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
51
52
53
54
55
56
57
58
59
60
61
62
63
# 3D surface fitting to N random points
# using inverse distance weighted averages.
# FB - 201003162
from PIL import Image
import random
import math

# image size
imgx = 512
imgy = 512
image = Image.new("RGB", (imgx, imgy))

# random color palette coefficients
kr = random.randint(1, 7)
kg = random.randint(1, 7)
kb = random.randint(1, 7)
ir = 2**kr
ig = 2**kg
ib = 2**kb
jr = 2**(8-kr)
jg = 2**(8-kg)
jb = 2**(8-kb)

# select n random points
n=random.randint(5, 50)
arx=[]
ary=[]
arz=[]

for i in range(n):
    arx.append(random.randint(0, imgx-1))
    ary.append(random.randint(0, imgy-1))
    arz.append(random.randint(0, 255))

for y in range(imgy):
    for x in range(imgx):
        flag=False
        sumv=0.0
        sumw=0.0
        for i in range(n):
            dx=x-arx[i]
            dy=y-ary[i]
            if(dx==0 and dy==0):
                flag=True
                z=arz[i]
                break
            else:
                # wgh=1.0/math.pow(math.sqrt(dx*dx+dy*dy),1.0) # linear
                wgh=1.0/math.pow(math.sqrt(dx*dx+dy*dy),2.0) # quadratic
                # wgh=1.0/math.pow(math.sqrt(dx*dx+dy*dy),3.0) # cubic
                sumw+=wgh
                sumv+=(wgh*arz[i])
            
        if flag==False:
            z=int(sumv/sumw)

        # z to RGB
        r = z % ir * jr
        g = z % ig * jg
        b = z % ib * jb
        image.putpixel((x, y), b * 65536 + g * 256 + r)

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

3 comments

FB36 (author) 14 years, 1 month ago  # | flag

This method can easily be reduced to 2D for generating a curve fitting to N random points. I have an applet (w/ source code) for that in my homepage: http://myweb.unomaha.edu/~fbahadir/

Dainius 10 years, 4 months ago  # | flag

love this piece of code. Thank you. Used it as a starting point for some of my creations, where I want things to be random. thank you, Dainius. http://www.dainiusmacikenas.com/coded_art.html

FB36 (author) 10 years, 4 months ago  # | flag

Great to hear! Awesome work!