-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsurf3d.py
More file actions
71 lines (66 loc) · 1.9 KB
/
Copy pathsurf3d.py
File metadata and controls
71 lines (66 loc) · 1.9 KB
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
64
65
66
67
68
69
70
71
from kandinsky import *
from math import *
# functions to plot
def sinc(x,y):
r=sqrt(x**2+y**2)
z=sin(r)/r
return z
def sincos(x,y):
z=sin(x)*cos(y)
return z
# function draws a line using kandinsky library
# start point:(x1,y1), end point:(x2,y2), optional color
def connect(x1,y1,x2,y2,co="black"):
xspan=x2-x1;yspan=y2-y1
steps=max(abs(xspan),abs(yspan))
if steps==0:
set_pixel(x1,y1,co)
return
dx=xspan/steps;dy=yspan/steps
x=x1;y=y1
for k in range(steps):
set_pixel(int(x+.5),int(y+.5),co)
x+=dx;y+=dy
#plotting function
#fun: function to plot must take 2 arguments, return 1
#xrange,yrange,zrange: tuples with (min,max) value
#txt; text which is added to the plot, optional
def surf(fun,xrange,yrange,zrange,txt=""):
nh=120;nv=60;scale=120
alpha=pi/4;k=0.4
fmax=[0]*320;fmin=[0]*320
xmin,xmax=xrange;ymin,ymax=yrange;zmin,zmax=zrange;
hm=160;vm=110
ca=k*cos(alpha);sa=k*sin(alpha)
dy=(ymax-ymin)/nv;dx=(xmax-xmin)/nh
fill_rect(0,0,320,240,"black")
y=ymin
for v in range(nv):
x=xmin;first1=True;first2=True
for h in range(nh):
z=fun(x,y)
xx=-scale+2*(x-xmin)/(xmax-xmin)*scale
yy=-scale+2*(y-ymin)/(ymax-ymin)*scale
zz=-scale+2*(z-zmin)/(zmax-zmin)*scale
xscr=int(hm+xx+yy*ca);yscr=int(vm-zz/2-sa*yy)
xscr=max(min(319,xscr),0)
if 240-yscr>fmax[xscr]:
if not first1:
connect(xscr2,yscr2,xscr,yscr,"white")
fmax[xscr]=240-yscr
xscr2,yscr2=xscr,yscr;first1=False
else:
first1=True
if yscr>fmin[xscr]:
if not first2:
connect(xscr2,yscr2,xscr,yscr,"white")
fmin[xscr]=yscr
xscr2,yscr2=xscr,yscr;first2=False
else:
first2=True
x+=dx
y+=dy
draw_string(txt,30,10,"white","black")
# function call to plot surface
surf(sinc,(-12, 12),(-12, 12),(-0.1, 1.0),"z=sinc(x,y)")
#surf(sincos,(-4,4),(-4,4),(-1,1),"sinxcos")