Mandelbrot set

Mandelbrot draws a mandelbrot set in little window. Mandelbrot demonstrates how to spawn a thread for drawing and do some RPN expressions to plot the pixels to window. Written in plain Ano script, no C involved.

Compilation

Easiest way to get Mandelbrot running is to go to examples directory in package root, and run:

$ ./build.sh gui_mandelbrot

build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.

Screenshots

Mandelbrot set

Preview

;
; @ANO_SCRIPT_NAME		gui_mandelbrot
; @ANO_SCRIPT_VERSION		0.0.6
; @ANO_SCRIPT_DESCRIPTION	Example how to plot some pixels to window
;
; @ANO_FLAGS_VAR_NAME_SUBS	[x]
; @ANO_FLAGS_VAR_WARN_UNUSED	[ ]
;
; Copyright (c) 2016-2024, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;

main {
	; Global uninitialized variables
	;
	var	[handle] hnd_win

	; Global pre-initialized variables
	;
	mov	win_w ([uint] 640)
	mov	win_h ([uint] 400)

	; Initialize windowing system...
	;
	gui_enabled {
		window_init

		; ...and open main window
		;
		window_open (\
			title_name:		"Mandelbrot", \
			title_charset:		NULL, \
			parent_handle:		NOPARENT, \
			widget_stack_id:	NOWIDGET, \
			widget_set:		NOWIDGET, \
			window_refresh_divider:	ACTIVE_REFRESH, \
			position_x:		POS_CENTERED, \
			position_y:		POS_CENTERED, \
			size_width:		win_w, \
			size_height:		win_h, \
			cb_main_loop:		NULL, \
			cb_expose:		NULL, \
			cb_key_press:		NULL, \
			cb_key_release:		NULL, \
			cb_button_press:	NULL, \
			cb_button_release:	NULL, \
			cb_client_message:	NULL, \
			cb_save_yourself:	NULL, \
			cb_configure_notify:	NULL, \
			cb_destroy_notify:	"cb_destroy", \
			cb_motion_notify:	NULL, \
			cb_map_notify:		NULL, \
			cb_unmap_notify:	NULL, \
			cb_open_notify:		"cb_open")
	}
}

_WINCB_OPEN_ callback cb_open (_hnd) {
	mov	hnd_win (_hnd)

	; Map main window
	;
	_hnd.map()

	; Create drawing thread
	;
	@mandelbrot_thread ("Mandelbrot")
}

_WINCB_DESTROY_ callback cb_destroy (_hnd) {
	_hnd.destroy()

	exit
}

thread mandelbrot_thread {
	&mandelbrot_draw (1.0, [uint] 120, [int] 0, [int] 0)
}

function mandelbrot_draw (_zoom, _maxiterations, _posx, _posy) {
	var	[color] _c

	loop (mov _y (0); _y < win_h; inc _y) {
		loop (mov _x (0); _x < win_w; inc _x) {
			mov	_pr (1.5 * (_x - win_w / 2.0) / (0.5 * _zoom * win_w) + _posx)
			mov	_pi ((_y - win_h / 2.0) / (0.5 * _zoom * win_h) + _posy)

			mov	_ne (0)
			mov	_nf (0)

			mov	_i ([int] 0)

		mandelbrot_loop_c:
			mov	_oe (_ne)
			mov	_of (_nf)

			mov	_ne (_oe * _oe - _of * _of + _pr)
			mov	_nf (2.0 * _oe * _of + _pi)

			mov	_t (_ne * _ne + _nf * _nf)

			if _t >= 4 : mandelbrot_loop_d

			inc	_i

			if _i < _maxiterations : mandelbrot_loop_c

		mandelbrot_loop_d:
			; Plot the pixel adjusting colors a bit
			;
			mov	_c (# 0)
			add	_c.red (_i)
			add	_c.green (_i * 2.0)

			hnd_win.pixel(\
				position_x: _x, \
				position_y: _y, \
				color: _c)
		}
	}
}

Copyright © 2024, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.