• Getting a computer to generate i
    9 replies, posted
As we all know, there is the imaginary plane. So I have i (root -1) How would I go about calculating this on a computer? I know that it is impossible, it cannot be done, but I am trying to make a Mandelbrot and I was just wandering how you would go about doing this.
[url=http://www.cplusplus.com/reference/std/complex/]Hello C++ standard library.[/url] [editline]22nd October 2010[/editline] That aside, I recall there's at least one way of writing a Mandelbrot fractal without imaginary numbers.
Hmm. That C++ isn't useful as I don't know C++ well enough to do this. But could you provide a link for Mandelbrot without imaginary numbers?
Use the imaginary part as the y component and the real part as the x component?
Could work, I'd have to take a look at it. Bit busy at the moment though :P
Actually, the Mandelbrot fractal seems to be a plotting of the Mandelbrot set on the complex plane, where x is the real part and y is the imaginary part. So it would work, you'd just have to scale the values, obviously, to get a reasonably sized one (The values of the set are apparently in the range (-2…1, -1…1) ) The scaling is done like this, if someone's interested: [cpp] screenX = minScreenX + ( ( origX + 2 ) / ( 1 + 2 ) ) * ( maxScreenX - minScreenX ) // For x screenY = minScreenY + ( ( origY + 1 ) / ( 1 + 1 ) ) * ( maxScreenY - minScreenY ) // For y [/cpp]
[QUOTE=Loli;25568152] I know that it is impossible, it cannot be done, but I am trying to make a Mandelbrot and I was just wandering how you would go about doing this.[/QUOTE] That's just plain wrong. Almost any mathematical system can be programmed on a computer. Math follows extremely strict rules and programming languages excel at expressing systems that follow strict, well-defined rule sets. Just because most languages use real numbers for their usual arithmetic doesn't mean it's impossible to program with imaginary numbers. I recall another thread where multiple said that it's similarly "impossible" to make a graphics library that would use spherical - rather than Euclidean - geometry. Ridiculous. Quick'n'dirty complex numbers in Ruby: [code] class ComplexNumber attr_accessor :real, :imaginary def initialize(real, imaginary) @real = real @imaginary = imaginary end def +(n) return ComplexNumber.new(@real + n.real, @imaginary + n.imaginary) end def *(n) return ComplexNumber.new(@real * n.real - @imaginary * n.imaginary, @real * n.imaginary + n.real * @imaginary) end def to_s return "#{@real} + #{@imaginary}i" end end [/code] Actually Ruby is an ideal language for working with complex numbers, because you can override standard library functions. So I could even do something like [code] class Numeric def **(x) #exponentiation if self < 0 return ComplexNumber.new(0, (-1 * self) ** x) else ... end end end [/code] so then (-1)**(0.5) would even return i as expected.
[QUOTE=Larikang;25580361]Actually Ruby is an ideal language for working with complex numbers, because you can override standard library functions.[/QUOTE] Operator overloading is a pretty common feature in OOP languages.
[QUOTE=Siemens;25580470]Operator overloading is a pretty common feature in OOP languages.[/QUOTE] That's not what he meant. He uses operator overloading in the first code snippet too. The second snippet demonstrates Ruby's open classes: Numeric is the base class for all Ruby number classes.
[QUOTE=jA_cOp;25580864]The second snippet demonstrates Ruby's open classes: Numeric is the base class for all Ruby number classes.[/QUOTE] Oh, didn't see that. That's awesome!
Sorry, you need to Log In to post a reply to this thread.