- Joined
- Sep 14, 2012

- Messages
- 3,413

If it can be used by someone I share it :

Feel free to use it if you want to do complex geometry in your map or something.

You can use it for example to create some fractals like Mandelbrot's or Julia's.

JASS:

```
library Complex/*
************************************************************************************
* Description
* -------------------------
*
* This is a library that allows you to use complex number to do some gemotry
* or some special calculus.
*
*
* Credits to :
* -------------------------
* - looking_for_help for the InternLn function :)
*
*
* Fields
* -------------------------
*
* struct Complex
* real re
* real im
*
*
* Methods
* -------------------------
*
* static method create takes nothing returns thistype
* static method createPolar takes real abs, real phase returns thistype
*
* method destroy takes nothing returns nothing
*
*
* method add takes thistype z returns thistype
* ~ It returns this+z
*
* method sub takes thistype z returns thistype
* ~ It returns this-z
*
* method mul takes thistype z returns thistype
* ~ It returns this*z
*
* method div takes thistype z returns thistype
* ~ It returns this/z
*
* method conj takes nothing returns thistype
* ~ It returns the conjugate of this
*
* method operator abs takes nothing returns real
* ~ It returns abs(this)
*
* method operator abs= takes real value returns nothing
* ~ Change the abs of the complex of the number to value
*
* method operator arg takes nothing returns real
* ~ It returns arg(this)
*
* method operator arg= takes real value returns nothing
* ~ Change the argument of the complex of the number to value
*
* method toString takes nothing returns string
* ~ It returns "a+bi"
*
* method toStringPolar takes nothing returns string
* ~ It returns "abs*e^(arg*i)"
*
* method ln takes nothing returns thistype
* ~ It returns ln(this)
*
* method exp takes nothing returns thistype
* ~ It returns e^this
*
* method pow takes thistype a returns thistype
* ~ It returns this^a
*
* method sqrt takes nothing returns thistype
* ~ It returns sqrt(this)
*
* method equals takes thistype z returns boolean
* ~ It returns if this==z or not
************************************************************************/
globals
private constant real E = 2.718282
private constant real PI = 3.141592
private constant real INV_E = 1/E
//Change the maximum number of instance of complex number
private constant integer MAX_NUMBER = 8190
endglobals
struct Complex [MAX_NUMBER]
real re
real im
static method create takes real r, real i returns thistype
local thistype this = thistype.allocate()
set this.re = r
set this.im = i
return this
endmethod
static method createPolar takes real abs, real phase returns thistype
if (abs < 0) then
debug call BJDebugMsg("thistype : Creating a Complex Number with abs<0, crashing the thread!")
return 1/0
endif
return create(abs*Cos(phase), abs*Sin(phase))
endmethod
//Thanks to looking_for_help !
private static method internLn takes real r returns real
local real sum = 0.0
local real sign = 1.0
if r < 1.0 then
set r = 1.0/r
set sign = -1.0
endif
loop
exitwhen r < E
set r = r*INV_E
set sum = sum + 1.0
endloop
loop
exitwhen r < 1.2840254
set r = r*0.778808
set sum = sum + 0.25
endloop
return sign*(sum + 0.125*(r - 1.0)*(1 + 9.0/(2.0 + r) + 4.5/(0.5 + r) + 1.0/r))
endmethod
method add takes thistype z returns thistype
return create(this.re + z.re, this.im + z.im)
endmethod
method sub takes thistype z returns thistype
return create(this.re - z.re, this.im - z.im)
endmethod
method mul takes thistype z returns thistype
return create(this.re*z.re - this.im*z.im, this.re*z.im + this.im*z.re)
endmethod
method conj takes nothing returns thistype
return create(this.re, -this.im)
endmethod
method div takes thistype z returns thistype
local real dv = z.re*z.re + z.im*z.im
if (dv!=0) then
return create((this.re*z.re + this.im*z.im)/dv, (this.im*z.re - this.re*z.im)/dv)
endif
debug call BJDebugMsg("thistype Error : Divide by 0!! Crashing thread")
return 1/0
endmethod
method operator abs takes nothing returns real
return SquareRoot(this.re*this.re + this.im*this.im)
endmethod
method operator arg takes nothing returns real
return Atan2(this.im, this.re)
endmethod
method operator abs= takes real value returns nothing
local real angle = Atan2(this.im, this.re)
set this.re = value*Cos(angle)
set this.im = value*Sin(angle)
endmethod
method operator arg= takes real value returns nothing
local real abs = SquareRoot(this.re*this.re + this.im*this.im)
set this.re = abs*Cos(value)
set this.im = abs*Sin(value)
endmethod
method toString takes nothing returns string
if (this.im >= 0) then
return R2S(this.re)+"+"+R2S(this.im)+"i"
endif
return R2S(this.re)+R2S(this.im)+"i"
endmethod
method toStringPolar takes nothing returns string
return R2S(this.abs)+"*e^("+R2S(this.arg)+"*i)"
endmethod
method ln takes nothing returns thistype
if this.re == 0 and this.im == 0 then
debug call BJDebugMsg("thistype Error : Attempt to do something bad like ln(0) !! Crashing thread")
return 1/0
endif
return create(internLn(this.abs), this.arg)
endmethod
method exp takes nothing returns thistype
return create(Pow(E, this.re)*Cos(this.im), Pow(E, this.re)*Sin(this.im))
endmethod
method pow takes thistype z returns thistype
return this.ln().mul(z).exp()
endmethod
method sqrt takes nothing returns thistype
local thistype c = thistype.allocate()
if (this.im == 0) then
if (this.re > 0) then
set c.re = SquareRoot(this.re)
set c.im = 0
else
set c.re = 0
set c.im = SquareRoot(-this.re)
endif
else
set c.re = SquareRoot((this.re + this.abs)/2)
set c.im = SquareRoot((this.abs - this.re)/2)
if this.im < 0 then
set c.im = -c.im
endif
endif
return c
endmethod
method equals takes thistype z returns boolean
return this.re==z.re and this.im==z.im
endmethod
endstruct
endlibrary
```

Feel free to use it if you want to do complex geometry in your map or something.

You can use it for example to create some fractals like Mandelbrot's or Julia's.

Last edited: