Skip to content

Instantly share code, notes, and snippets.

@max-kov
Created March 8, 2016 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-kov/2b8a35306dccbdde8d32 to your computer and use it in GitHub Desktop.
Save max-kov/2b8a35306dccbdde8d32 to your computer and use it in GitHub Desktop.
bad mandelbrot implement using python
__author__ = 'mk070_000'
#R KEY - INCREASE FUNCTION LOOPS OR ACCURACY
#LEFT MOUSE, HOLD, MOVE, RELEASE - CHOOSE FUNCTION MERITS (max values, can be use to zoom in)
#RIGHT MOUSE - CREATE A JULIA SET , USING THE MOUSE COORDINATES
import math, pygame, cmath, sys
from pygame import gfxdraw
def mandelbrot(point ,koeff , n):
if abs(point) > 5:
return 254-(254.0/accuracy)*n
else:
if n > 1:
return mandelbrot(point ** 2 + koeff, koeff, n - 1)
else:
return 254
pygame.init()
temp =pygame.display.Info()
displLengths=(temp.current_w, temp.current_h)
surface = pygame.display.set_mode(displLengths,pygame.FULLSCREEN)
plotx = (-2,2)
ploty = (-2,2)
accuracy=50
multkoeffx = (plotx[1]-plotx[0]) / float(displLengths[0])
multkoeffy = (ploty[1]-ploty[0]) / float(displLengths[1])
x=plotx[0]
y=ploty[0]
pixelx=0
pixely=0
isJulia=False
#Mandelbrot loop \
while not isJulia:
multkoeffx = (plotx[1]-plotx[0]) / float(displLengths[0])
multkoeffy = (ploty[1]-ploty[0]) / float(displLengths[1])
x=plotx[0]
y=ploty[0]
pixelx=0
pixely=0
while y<ploty[1]:
while x<plotx[1]:
gfxdraw.pixel(surface,int(pixelx-plotx[0]),int(pixely-ploty[0]),
(0,0,mandelbrot(complex(x,y),complex(x,y),accuracy)))
x+=multkoeffx
pixelx+=1
x = plotx[0]
pixely+=1
pixelx=0
y+=multkoeffy
pygame.display.update()
#EVENT HADLER
notDone=True
isJulia=False
while notDone:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mousex0, mousey0=event.pos
while notDone:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
mousex1, mousey1=event.pos
plotx = ((mousex0*multkoeffx)+plotx[0],
(mousex1*multkoeffx)+plotx[0])
ploty = ((mousey0*multkoeffy)+ploty[0],
(mousey1*multkoeffy)+ploty[0])
notDone=False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
accuracy+=50
notDone=False
elif event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == pygame.K_j:
coord = pygame.mouse.get_pos()
notDone=False
isJulia=True
#JULIA LOOP
plotx = (-2,2)
ploty = (-2,2)
accuracy=50
complexCoord = complex((coord[0]*multkoeffx)+plotx[0],(coord[1]*multkoeffy)+ploty[0])
while isJulia:
multkoeffx = (plotx[1]-plotx[0]) / float(displLengths[0])
multkoeffy = (ploty[1]-ploty[0]) / float(displLengths[1])
x=plotx[0]
y=ploty[0]
pixelx=0
pixely=0
while y<ploty[1]:
while x<plotx[1]:
gfxdraw.pixel(surface,int(pixelx-plotx[0]),int(pixely-ploty[0]),
(0,0,mandelbrot(complex(x,y),complexCoord,accuracy)))
x+=multkoeffx
pixelx+=1
x = plotx[0]
pixely+=1
pixelx=0
y+=multkoeffy
pygame.display.update()
#EVENT HADLER
notDone=True
while notDone:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mousex0, mousey0=event.pos
while notDone:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
mousex1, mousey1=event.pos
plotx = ((mousex0*multkoeffx)+plotx[0],
(mousex1*multkoeffx)+plotx[0])
ploty = ((mousey0*multkoeffy)+ploty[0],
(mousey1*multkoeffy)+ploty[0])
notDone=False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
accuracy+=50
notDone=False
elif event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment