News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

DrawLeaf

Started by six_L, April 02, 2022, 12:09:36 PM

Previous topic - Next topic

six_L

QuoteA Barnsley fern is a fractal named after British mathematician Michael Barnsley and can be created using an iterated function system (IFS).


Task

Create this fractal fern, using the following transformations:

    ƒ1   (chosen 1% of the time)

        x(n + 1) = 0
        y(n + 1) = 0.16 yn

    ƒ2   (chosen 85% of the time)

        x(n + 1) = 0.85 xn + 0.04 yn
        y(n + 1) = −0.04 xn + 0.85 yn + 1.6

    ƒ3   (chosen 7% of the time)

        x(n + 1) = 0.2 xn − 0.26 yn
        y(n + 1) = 0.23 xn + 0.22 yn + 1.6

    ƒ4   (chosen 7% of the time)

        x(n + 1) = −0.15 xn + 0.28 yn
        y(n + 1) = 0.26 xn + 0.24 yn + 0.44.

Starting position: x = 0, y = 0
option casemap:none
option win64:7

include \UASM64\include\windows.inc

includelib \UASM64\Lib\user32.lib
includelib \UASM64\Lib\kernel32.lib
includelib \UASM64\Lib\gdi32.lib

; function style macros for direct insertion of data types *
FP10 MACRO value
        LOCAL vname
        .data
align 8
vname REAL10 value
        .code
        EXITM <vname>
ENDM

ICO_MAIN equ 1000
DLG_MAIN equ 100

.data?
hInstance dq ?
Main_hWnd dq ?
x11 dd ?
y11 dd ?
x_max dq ?
dwPointColor dd ?
hBrushBackground dq ?
rect RECT <>

.code

iRand proc _dqMin:QWORD, _dqMax:QWORD
local @stCount:LARGE_INTEGER
   
invoke QueryPerformanceCounter,addr @stCount
mov rcx,@stCount.QuadPart
mov rax,100               
rdtsc                           
pause                           
mul rcx
xor rax,095AC9329AC4BC9B5h
bswap rax                     ; reverse byte order
mov rcx,0100000001B3h
mul rcx
ror rax,17
bswap rax                     

mov rcx, _dqMax   
sub rcx, _dqMin
inc rcx
xor rdx, rdx 
div rcx       
mov rax, rdx
add rax,_dqMin    ; rax = Rand_Number[dwMin,dwMax]
ret

iRand endp

BarnsleyFern proc USES rbx iter:QWORD
local x0:REAL10
local y0:REAL10
local x1:REAL10
local y1:REAL10
local diceThrow:QWORD
local tmpIV:QWORD

        finit
; x0=0,y0=0
fldz
fstp x0

fldz
fstp y0

mov rbx,iter
.while rbx > 0
invoke iRand,0,100
mov diceThrow,rax
.if diceThrow == 0
;x1 = 0;
;y1 = 0.16*y0;

fldz
fstp x1

fld y0
fld FP10(0.16)
fmul
fstp y1

mov dwPointColor,08000h
.elseif (diceThrow >= 1) && (diceThrow <= 7)
;x1 = 0.2*x0 - 0.26*y0;
;y1 = 0.23*x0 + 0.22*y0 + 1.6;

fld x0
fld FP10(0.2)
fmul

fld y0
fld FP10(0.26)
fmul

fsub st(1),st(0) ;
fxch st(1)
fstp x1
ffree st(0)

fld x0
fld FP10(0.23)
fmul

fld y0
fld FP10(0.22)
fmul

fadd st(0),st(1)
fld FP10(1.6)
fadd st(0),st(1) ;0.23*x0 + 0.22*y0 + 1.6;

fstp y1
ffree st(0)
ffree st(1)

mov dwPointColor,0ffh
.elseif (diceThrow >= 8) && (diceThrow <= 15)
;x1 = -0.15*x0 + 0.28*y0;
;y1 = 0.26*x0 + 0.24*y0 + 0.44;

fld x0
fld FP10(-0.15)
fmul

fld y0
fld FP10(0.28)
fmul

fadd st(0),st(1)

fstp x1
ffree st(0)

fld x0
fld FP10(0.26)
fmul

fld y0
fld FP10(0.24)
fmul

fadd st(0),st(1)

fld FP10(0.44)
fadd st(0),st(1)

fstp y1
ffree st(0)
ffree st(1)

mov dwPointColor,000D7FFh ;045A5A5h

.else
;x1 = 0.85*x0 + 0.04*y0;
;y1 = -0.04*x0 + 0.85*y0 + 1.6;

fld x0
fld FP10(0.85)
fmul

fld y0
fld FP10(0.04)
fmul

fadd st(0),st(1) ;0.85*x0 + 0.04*y0
fstp x1
ffree st(0)

fld x0
fld FP10(0.04)
fchs ;-0.04
fmul

fld y0
fld FP10(0.85)
fmul

fadd st(0),st(1)
fld FP10(1.6)
fadd st(0),st(1) ;-0.04*x0 + 0.85*y0 + 1.6;

fstp y1
ffree st(0)
ffree st(1)

mov dwPointColor,0FF00h;08000h-dark green

.endif

;x0 = x1;
;y0 = y1;

fld x1
fstp x0

fld y1
fstp y0

;30*x1 + windowWidth/2.0
fld x1
mov tmpIV,30
fild tmpIV
fmul ;st(0)=30*x1

fild x_max
mov tmpIV,2
fild tmpIV
fdiv st(1),st(0)
fxch st(1)

fadd st(0),st(2)
fistp x11
ffree st(0)
ffree st(1)

;30*y1
fld y1
mov tmpIV,30
fild tmpIV
fmul
fistp y11

invoke InvalidateRect,Main_hWnd,0,FALSE
invoke UpdateWindow,Main_hWnd
;invoke Sleep,5
dec rbx
.endw
ret

BarnsleyFern endp

_ProcDlgMain proc hWnd:qword,wMsg:dword,wParam:qword,lParam:qword
LOCAL ps:PAINTSTRUCT

mov eax,wMsg
.if eax == WM_INITDIALOG
mov rax,hWnd
mov Main_hWnd,rax
invoke LoadIcon,hInstance,ICO_MAIN
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax

invoke CreateSolidBrush,0h ;Black
mov hBrushBackground,rax

invoke GetClientRect,hWnd,addr rect
mov eax,rect.right
sub eax,rect.left
mov x_max,rax

invoke CreateThread, 0, 0, offset BarnsleyFern, 15000, 0, NULL
invoke CloseHandle, rax

.elseif eax==WM_PAINT
invoke BeginPaint,hWnd,addr ps
invoke  SetPixelV,ps.hdc,x11,y11,dwPointColor        ;022beffh-golden;000FF00h-green; 0FFh-red ; 0FF0000h-blue;0FFFFh-yellow;
invoke  EndPaint,hWnd,addr ps

.elseif eax==WM_ERASEBKGND
invoke FillRect,wParam, addr rect, hBrushBackground
.elseif eax == WM_CLOSE
invoke  DeleteObject,hBrushBackground
invoke EndDialog,hWnd,NULL

.else
mov rax,FALSE
ret
.endif
mov rax,TRUE
ret

_ProcDlgMain endp

WinMainCRTStartup Proc
invoke GetModuleHandle,NULL
mov hInstance,rax
invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
invoke ExitProcess,NULL
WinMainCRTStartup Endp


end




#include <\UASM64\include\resource.h>

#define ICO_MAIN 1000
#define DLG_MAIN 100

ICO_MAIN ICON "main.ico"

DLG_MAIN DIALOG 0, 0, 237, 170
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "DrawLeaf"
FONT 10, "Consolas"
{
}

Say you, Say me, Say the codes together for ever.

Biterider

Hi six:L
Works fine here (Win10 64 21H1)
Really cool  :thumbsup:

Biterider

johnsa

Very nice! Thanks for sharing

Gunther

Six_L,

good and solid work. :thumbsup: What gave you this idea?
You have to know the facts before you can distort them.

six_L

Hi, Biterider/johnsa/Gunther
Thanks your kind words.
QuoteWhat gave you this idea?
All come from internet. only translate.
our member Mikl__ has providid many codes about this type. http://masm32.com/board/index.php?topic=4880.0
his codes is the masm64 style, more complexity.

regars.
Say you, Say me, Say the codes together for ever.