example code:
Rem Project: Dark Basic Pro Project
Rem Created: Wednesday, June 09, 2010
Rem ***** Main Source File *****
Rem Created: Wednesday, June 09, 2010
Rem ***** Main Source File *****
set display mode 1280,1024,32
d3d_init
dim ret(-1) as dword
global retx as float
global rety as float
global pixels_per_unit as float
pixels_per_unit=100.0
global imag1 as dword
global imag2 as dword
imag1=zCreate(2,0)
imag2=zCreate(2,0)
sync on
do
cls
if mouseclick()
toUnits(mousex(),mousey())
if keystate(30) then rety=0
if keystate(31) then retx=0
if spacekey()
zSet(imag2,retx,rety)
else
zSet(imag1,retx,rety)
endif
endif
clearRet()
zDraw(imag1,rgb(0,255,0))
zDraw(imag2,rgb(0,0,255))
zPower(imag1,imag2,10,10)
drawGraphPaper(pixels_per_unit, screen width()/2,screen height()/2,1)
for n=0 to array count(ret())
`just ta get a nice, consistent color:
value as dword
value=(2^24)*n*1.0/(array count(ret()))
r as dword
g as dword
b as dword
r=(value and %111111110000000000000000)>>16
g=(value and %000000001111111100000000)>>8
b=(value and %000000000000000011111111)>>0
r=minimum(r+40,255)
g=minimum(g+40,255)
b=minimum(b+40,255)
zDraw(ret(n),rgb(r,g,b))
next
text 0,16,zString(imag1)+"^("+zString(imag2)+")"
tempx as integer
tempy as integer
for n=0 to array count(ret())
//text tempx,tempy,zString(ret(n))
//tempy=tempy+16
//if tempy>screen height()
// tempy=0
// tempx=tempx+55
//endif
next
sync
loop
function minimum(a as dword, b as dword)
if a>b then exitfunction b
endfunction a
function zString(a as dword)
s$=str$(peek float(a))+" + i*"+str$(peek float(a+4))
endfunction s$
function zCreate(a as float, b as float)
mem as dword
mem=alloc(12)
poke dword mem+8,1
poke float mem+0,a
poke float mem+4,b
endfunction mem
function zDestroy(a as dword)
free a
endfunction
function zDraw(a as dword,color as dword)
toScreen(peek float(a),peek float(a+4))
d3d_circle retx,rety,4,1,color
endfunction
function zDivide(result as dword, a as dword, b as dword)
div#=peek float(b)*peek float(b)+peek float(b+4)*peek float(b+4)
poke float result, (peek float(a)*peek float(b)+peek float(a+4)*peek float(b+4))/div#
poke float result+4,(peek float(a+4)*peek float(b)+peek float(a)*peek float(b+4))/div#
endfunction
function zAdd(result as dword, a as dword, b as dword)
poke float result,peek float(a)+peek float(b)
poke float result+4,peek float(a+4)+peek float(b+4)
endfunction
function zSubtract(result as dword, a as dword, b as dword)
poke float result,peek float(a)-peek float(b)
poke float result+4,peek float(a+4)-peek float(b+4)
endfunction
function zMultiply(result as dword, a as dword, b as dword)
poke float result,peek float(a)*peek float(b)-peek float(a+4)-peek float(b+4)
poke float result+4,peek float(a+4)*peek float(b)+peek float(a)*peek float(b+4)
endfunction
function zParse(s as string)
a as string
b as string
lr as boolean
for n=1 to len(s)
temp$=mid$(s,n)
if asc(temp$)>=asc("0") and asc(temp$)<=asc("9")
endif
next
endfunction
function zArg(a as dword) //in degrees
ang#=atanfull(peek float(a+4),peek float(a))
endfunction ang#
function zAbs(a as dword)
ret#=sqrt(peek float(a)*peek float(a)+peek float(a+4)*peek float(a+4))
endfunction ret#
function zSet(mem as dword, a as float, b as float)
poke float mem,a
poke float mem+4,b
endfunction
function zPower(a as dword,b as dword,num as dword,num2 as dword) `(a1+a2*i)^(b1+b2*i)
`fills ret() with values taking zArg from -num*2*pi*i to num*2*pi*i
pzArg_degA=zArg(a) `principal complex argument of a in degrees
`...=e^(b1*ln(|a|)-b2*arg(a)) * e^(i*(arg(a)*b1+b2*ln(|a|)))
`---------^--------^---------------^------------^------- responsible for the infinite number of solutions :\\
`taking into account:
`ln(|a|)=standard_ln(|a|)+2*pi*i*k
`arg(a)=standard_arg(a)+2*pi*n
`where n and k are integers, the equation comes to this:
`...=e^(b1*ln(|a|)-b2*arg(a)+2*b2*pi*(n-k))*e^(b1*arg(a)-b2*ln(|a|)+2*b1*pi*(n+k))
al as float
be as float
al=log(zAbs(a))
be=zArg(a)
b1amb2b as float `b1*ln(|a|)-b2*arg(a)
b1bpb2a as float `b1*arg(a) +b2*ln(|a|)
b2pi2 as float `2*pi*b2
b1pi2 as float `2*pi*b2
b1amb2b=peek float(b)*al-peek float(b+4)*be*.017453293 `radians, \'cuz it\'s e^(rational approximation)
b1bpb2a=peek float(b)*be+peek float(b+4)*al*57.29577951 `degrees, \'cuz e*(ix)=cos(x)+isin(x) and dbpro uses radians.
b2pi2=peek float(b+4)*6.28318531
b1pi2=peek float(b)*360 `\'cuz it\'s degrees. damn differing radian/degrees standards.
text 0,0,str$( pzArg_degA)
n as integer
k as integer
for n=-num2 to num2
for k=-num to num
array insert at bottom ret()
ret(array count(ret()))=zCreate(0,0)
theta#=b1bpb2a+b1pi2*(n+k)
magnitude#=exp(b1amb2b+b2pi2*(n-k))
`now (a1+a2i)^(b1+b2i)=magnitude#*(cos(theta#)+isin(theta#))
``print n
`print newzArg
`print lnAbsA
`print newzArg
`print magnitude#
`sync
`wait key
poke float ret(array count(ret())),magnitude#*cos(theta#)
poke float ret(array count(ret()))+4,magnitude#*sin(theta#)
next
next
endfunction
function clearRet()
c as integer
c=array count(ret())
for n=0 to c
zDestroy(ret(n))
next
empty array ret()
endfunction
function drawGraphPaper(PPU as integer, midx as integer, midy as integer, subUnits as integer)
if subunits<=0 then subunits=1
if PPU<=0 then ppu=40
subDist as float
subDist=PPU*1.0/subUnits
counter1 as float =
counter1=-PPU
counter2 as float = 0
w=screen width()
h=screen height()
ink rgb(0,0,40)
line 0,h/2,w,h/2
ink rgb(0,40,0)
line w/2,0,w/2,h
while (counter1+midx<w) or (midx-counter1>0)
inc counter1, PPU
counter2=0
while (counter2<PPU)
inc counter2, subDist
ink rgb(150,150,150),0
line midx+counter1+counter2,midy-2,midx+counter1+counter2,midy+2
line midx-counter1+counter2,midy-2,midx-counter1+counter2,midy+2
endwhile
ink rgb(255,255,255),0
line midx+counter1,midy-5,midx+counter1,midy+5
line midx-counter1,midy-5,midx-counter1,midy+5
endwhile
counter1=-PPU
counter2=0
while (counter1+midy<w) or (midy-counter1>0)
inc counter1, PPU
counter2=0
while (counter2<PPU)
inc counter2, subDist
ink rgb(150,150,150),0
line midx-2,midy+counter1+counter2,midx+2,midy+counter1+counter2
line midx-2,midy-counter1-counter2,midx+2,midy-counter1-counter2
endwhile
ink rgb(255,255,255),0
line midx-4,midy+counter1,midx+4,midy+counter1
line midx-4,midy-counter1,midx+4,midy-counter1
endwhile
endfunction
function toUnits(x as float, y as float)
retx=(x-screen width()/2)/pixels_per_unit
rety=-(y-screen height()/2)/pixels_per_unit
endfunction
function toScreen(x as float, y as float)
retx=x*pixels_per_unit+screen width()/2
rety=-y*pixels_per_unit+screen height()/2
endfunction
how to solve for everything: (coded to avoid smileys)
Say for some weird reason, you want to find (a1+i*a2)^(b1+i*b2). to do so, convert a into polar form, and do some algebraic stuff to get this:
(a1+i*a2)^(b1+i*b2)=e^(b1*ln(|a|)-b2*arg(a)) * e^(i*(arg(a)*b1+b2*ln(|a|)))
where |a|=sqrt(a1^2+a2^2) and arg(a) is the complex argument of a (angle of it on the complex plane), or atanfull(a2,a1).
then one has to take into account the infinite solutions for both arg(a) and ln(|a|), where arg(a) can increase by any integer multiple of 2*pi, and ln(|a|) can increase by any integer multiple of 2*pi*i. Now you have this:
e^(b1*ln(|a|)-b2*arg(a)+2*b2*pi*(n-k))*e^(b1*arg(a)-b2*ln(|a|)+2*b1*pi*(n+k))
for any two integers n and k.
Really cool if, say, you wanted to find all 20 solutions to 1^(1/20). I hope to get something going with domain coloring or riemann surfaces going soon, to generate cool pictures.
what this program does:
Why does blue text appear every time you are near?