Reverse Engineering for Beginners

(avery) #1

CHAPTER 83. DEMOS CHAPTER 83. DEMOS


83.2.1 Theory


A word about complex numbers


A complex number is a number that consists of two parts—real (Re) and imaginary (Im).


The complex plane is a two-dimensional plane where any complex number can be placed: the real part is one coordinate
and the imaginary part is the other.


Some basic rules we need to know:



  • Addition:(a+bi) + (c+di) = (a+c) + (b+d)i


In other words:

Re(sum) =Re(a) +Re(b)
Im(sum) =Im(a) +Im(b)


  • Multiplication:(a+bi)(c+di) = (ac−bd) + (bc+ad)i


In other words:

Re(product) =Re(a)⋅Re(c)−Re(b)⋅Re(d)

Im(product) =Im(b)⋅Im(c) +Im(a)⋅Im(d)


  • Square:(a+bi)^2 = (a+bi)(a+bi) = (a^2 −b^2 ) + (2ab)i


In other words:

Re(square) =Re(a)^2 −Im(a)^2

Im(square) = 2⋅Re(a)⋅Im(a)

How to draw the Mandelbrot set


The Mandelbrot set is a set of points for which thezn+1=zn^2 +crecursive sequence (wherezandcare complex numbers
andcis the starting value) does not approach infinity.


In plain English language:



  • Enumerate all points on screen.

  • Check if the specific point is in the Mandelbrot set.

  • Here is how to check it:

    • Represent the point as a complex number.

    • Calculate the square of it.

    • Add the starting value of the point to it.

    • Does it go off limits? If yes, break.

    • Move the point to the new place at the coordinates we just calculated.

    • Repeat all this for some reasonable number of iterations.



  • The point is still in limits? Then draw the point.

  • The point has eventually gone off limits?

    • (For a black-white image) do not draw anything.

    • (For a colored image) transform the number of iterations to some color. So the color shows the speed with which
      point has gone off limits.




Here is Pythonesque algorithm for both complex and integer number representations:


Listing 83.3: For complex numbers

def check_if_is_in_set(P):
P_start=P
iterations=0


while True:
if (P>bounds):
break
P=P^2+P_start
Free download pdf