graphics: plot a graph / Mandelbrot set

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

graphics: plot a graph / Mandelbrot set

13 Aug 2017, 17:17

E.g. plot points/areas:

Code: Select all

ListLines, Off
#NoEnv
AutoTrim, Off
SetBatchLines, -1
Process, Priority,, High

;settings:
vRangeX := "-10,10"
vRangeY := "-10,10"
vDiffX := 0.1
vDiffY := 0.1
vList := "1,1|4,4|9,9|-1,-1|-4,-4|-9,-9"

;==================================================

vPi := 3.141592653589793
vRangeX1 := StrSplit(vRangeX, ",")[1]
vRangeX2 := StrSplit(vRangeX, ",")[2]
vRangeY1 := StrSplit(vRangeY, ",")[1]
vRangeY2 := StrSplit(vRangeY, ",")[2]
vImgW := Ceil((vRangeX2 - vRangeX1) / vDiffX) + 1
vImgH := Ceil((vRangeY2 - vRangeY1) / vDiffY) + 1

;ARGB values:
vColWhite := 0xffffffff
vColBlack := 0xff000000
vColRed := 0xffff0000
vColYellow := 0xffffff00
vColLime := 0xff00ff00
vColBlue := 0xff0000ff

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmap(vImgW, vImgH)
G := Gdip_GraphicsFromImage(pBitmap)
pBrush := Gdip_BrushCreateSolid(vColWhite) ;background colour
Gdip_FillRectangle(G, pBrush, 0, 0, vImgW, vImgH)

;==================================================

;list axes points
vListAxes := ""
vX := vRangeX1
Loop
{
	vY := vRangeY1
	Loop
	{
		if (vX=0) || (vY=0)
			vListAxes .= vX "," vY "|"
		vY++
		if (vY > vRangeY2)
			break
	}
	vX++
	if (vX > vRangeX2)
		break
}
vListAxes := SubStr(vListAxes, 1, -1)

;==================================================

;draw points (calculate)
vX := vRangeX1
Loop
{
	vY := vRangeY1
	Loop
	{
		vY += vDiffY
		vX3 := vX-2, vY3 := vY-2
		vArg := DllCall("msvcrt\atan2", "Double",vX, "Double",vY, "CDECL Double")
		if (Sqrt(vX3**2+vY3**2) >= 4) && (Sqrt(vX3**2+vY3**2) <= 8)
		{
			vX2 := Round((vX - vRangeX1) / vDiffX)
			vY2 := Round((vRangeY2 - vY) / vDiffY)
			if (-vPi/4 < vArg) && (vArg < vPi/4)
				Gdip_SetPixel(pBitmap, vX2, vY2, vColYellow)
			else
				Gdip_SetPixel(pBitmap, vX2, vY2, vColLime)
		}
		if (vY > vRangeX2)
			break
	}
	vX += vDiffX
	if (vX > vRangeX2)
		break
}

;draw points (axes)
Loop, Parse, vListAxes, |
{
	oTemp := StrSplit(A_LoopField, ",")
	vX := Round((oTemp.1 - vRangeX1) / vDiffX)
	vY := Round((vRangeY2 - oTemp.2) / vDiffY)
	;MsgBox, % A_LoopField "`r`n" vX " " vY
	Gdip_SetPixel(pBitmap, vX, vY, vColBlack)
}

;draw points (list)
Loop, Parse, vList, |
{
	oTemp := StrSplit(A_LoopField, ",")
	vX := Round((oTemp.1 - vRangeX1) / vDiffX)
	vY := Round((vRangeY2 - oTemp.2) / vDiffY)
	;MsgBox, % A_LoopField "`r`n" vX " " vY
	Gdip_SetPixel(pBitmap, vX, vY, vColRed)
}

;==================================================

vPath = %A_Desktop%\z graph %A_Now%.png
Gdip_SaveBitmapToFile(pBitmap, vPath, 100)

Gdip_DeleteBrush(pBrush)
Gdip_DeleteGraphics(G)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
Run, % vPath
return

;==================================================
==================================================

E.g. Mandelbrot set:

Code: Select all

ListLines, Off
#NoEnv
AutoTrim, Off
SetBatchLines, -1
Process, Priority,, High

;settings:
vRangeX := "-2,2"
vRangeY := "-2,2"
vDiff := 0.01
;vDiff := 0.005
;vDiff := 0.001
vLoop := 10 ;increase to give more accuracy

;==================================================

vPi := 3.141592653589793
vRangeX1 := StrSplit(vRangeX, ",")[1]
vRangeX2 := StrSplit(vRangeX, ",")[2]
vRangeY1 := StrSplit(vRangeY, ",")[1]
vRangeY2 := StrSplit(vRangeY, ",")[2]
vImgW := Ceil((vRangeX2 - vRangeX1) / vDiff) + 1
vImgH := Ceil((vRangeY2 - vRangeY1) / vDiff) + 1

;ARGB values:
;Progress/SplashImage
;https://autohotkey.com/docs/commands/Progress.htm
vColWhite := 0xffffffff
vColBlack := 0xff000000
vColRed := 0xffff0000
vColYellow := 0xffffff00
vColLime := 0xff00ff00
vColBlue := 0xff0000ff

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmap(vImgW, vImgH)
G := Gdip_GraphicsFromImage(pBitmap)
pBrush := Gdip_BrushCreateSolid(vColWhite) ;background colour
Gdip_FillRectangle(G, pBrush, 0, 0, vImgW, vImgH)

;==================================================

;list axes points
vListAxes := ""
vX := vRangeX1
Loop
{
	vY := vRangeY1
	Loop
	{
		if (vX=0) || (vY=0)
			vListAxes .= vX "," vY "|"
		vY++
		if (vY > vRangeY2)
			break
	}
	vX++
	if (vX > vRangeX2)
		break
}
vListAxes := SubStr(vListAxes, 1, -1)

;==================================================

vX := vRangeX1
Loop
{
	vY := vRangeY1
	Loop
	{
		if JEE_IsInMandelbrotSet(vX, vY, vLoop)
		{
			vX2 := Round((vX - vRangeX1) / vDiff)
			vY2 := Round((vRangeY2 - vY) / vDiff)
			Gdip_SetPixel(pBitmap, vX2, vY2, vColBlack)
		}
		vY += vDiff
		if (vY > vRangeY2)
			break
	}
	vX += vDiff
	if (vX > vRangeX2)
		break
}

;==================================================

vPath = %A_Desktop%\z graph %A_Now%.png
Gdip_SaveBitmapToFile(pBitmap, vPath, 100)

Gdip_DeleteBrush(pBrush)
Gdip_DeleteGraphics(G)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
Run, % vPath
return

;==================================================

JEE_IsInMandelbrotSet(vR, vI, vLoop)
{
	vR1 := vI1 := 0, vInSet := 1
	Loop, % vLoop
	{
		vR2 := vR1, vI2 := vI1
		vR1 := ((vR2 ** 2) - (vI2 ** 2)) + vR
		vI1 := (2 * vR2 * vI2) + vI

		;if complex modulus > 2
		if (Sqrt((vR1 ** 2) + (vI1 ** 2)) > 2)
		{
			vInSet := 0
			break
		}
	}
	return vInSet
}

;==================================================
Although there is a (in various ways) better Mandelbrot set script here:
Fun with GDIPlus (GDI+) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 260#p36260

==================================================

Notes:

If you plot a graph, with range -10 to 10, that is 21 points.
Similarly, if you plot a graph, with range -100 to 100, that is 201 points.

Code: Select all

be aware of Cartesian coordinates: y decreases as you go downwards
     y
     |
-x — + — x
     |
    -y

v. screen coordinates: y increases as you go downwards
     + — x
     |
     y
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 23 guests