MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get_distance

Function get_distance

fractals/mandelbrot.py:23–47  ·  view source on GitHub ↗

Return the relative distance (= step/max_step) after which the complex number constituted by this x-y-pair diverges. Members of the Mandelbrot set do not diverge so their distance is 1. >>> get_distance(0, 0, 50) 1.0 >>> get_distance(0.5, 0.5, 50) 0.061224489795

(x: float, y: float, max_step: int)

Source from the content-addressed store, hash-verified

21
22
23def get_distance(x: float, y: float, max_step: int) -> float:
24 """
25 Return the relative distance (= step/max_step) after which the complex number
26 constituted by this x-y-pair diverges. Members of the Mandelbrot set do not
27 diverge so their distance is 1.
28
29 >>> get_distance(0, 0, 50)
30 1.0
31 >>> get_distance(0.5, 0.5, 50)
32 0.061224489795918366
33 >>> get_distance(2, 0, 50)
34 0.0
35 """
36 a = x
37 b = y
38 for step in range(max_step): # noqa: B007
39 a_new = a * a - b * b + x
40 b = 2 * a * b + y
41 a = a_new
42
43 # divergence happens for all complex number with an absolute value
44 # greater than 4
45 if a * a + b * b > 4:
46 break
47 return step / (max_step - 1)
48
49
50def get_black_and_white_rgb(distance: float) -> tuple:

Callers 1

get_imageFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected