#include <GDIP.au3>

$iWidth = 800
$iHeight = 300
$iDistance = 10

$nAngle = 60
$iIterations = 8 ;Bitte vorsichtig erhöhen, da ALLE Koordinaten des Fraktals zum selben Zeitpunkt im Speicher sind.

$hWnd = GUICreate("GDI+ Koch-Curve | Iterations: " & $iIterations, $iWidth, $iHeight)
GUISetState()

_GDIPlus_Startup()

$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hWnd)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphics)
$hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)
_GDIPlus_GraphicsClear($hGraphics, 0xFFFFFFFF)

$hPen = _GDIPlus_PenCreate(0xFF000000, 1)
$hPath = _GDIPlus_PathCreate()

;~ _GDIPlus_PathStartFigure($hPath)
_GDIPlus_PathAddLine($hPath, 0, 0, 1, 0)

For $iI = 1 To $iIterations
	$hPathBuffer = _GDIPlus_PathClone($hPath)

	$aPoint = _GDIPlus_PathGetLastPoint($hPath)
	$hMatrix = _GDIPlus_MatrixCreate()
	_GDIPlus_MatrixTranslate($hMatrix, $aPoint[0], $aPoint[1])
	_GDIPlus_MatrixRotate($hMatrix, -$nAngle)

	_GDIPlus_PathTransform($hPathBuffer, $hMatrix)
	_GDIPlus_PathAddPath($hPath, $hPathBuffer, True)
	_GDIPlus_MatrixDispose($hMatrix)
	_GDIPlus_PathDispose($hPathBuffer)

	$hPathBuffer = _GDIPlus_PathClone($hPath)
	_GDIPlus_PathReverse($hPathBuffer)

	$aPoint = _GDIPlus_PathGetLastPoint($hPath)
	$hMatrix = _GDIPlus_MatrixCreate()
	_GDIPlus_MatrixSetElements($hMatrix, -1, 0, 0, 1, $aPoint[0] * 2, 0)

	_GDIPlus_PathTransform($hPathBuffer, $hMatrix)
	_GDIPlus_PathAddPath($hPath, $hPathBuffer, True)
	_GDIPlus_MatrixDispose($hMatrix)
	_GDIPlus_PathDispose($hPathBuffer)
Next

;~ _GDIPlus_PathCloseFigure($hPath)

Do
	$aBounds = _GDIPlus_PathGetWorldBounds($hPath, 0, $hPen)
	If $aBounds[2] > $aBounds[3] Then
		$nScale = ($iWidth - $iDistance * 2) / $aBounds[2]
	Else
		$nScale = ($iHeight - $iDistance * 2) / $aBounds[3]
	EndIf

	$hMatrix = _GDIPlus_MatrixCreate()
	_GDIPlus_MatrixScale($hMatrix, $nScale, $nScale)
	_GDIPlus_PathTransform($hPath, $hMatrix)
	_GDIPlus_MatrixDispose($hMatrix)

	$aBounds = _GDIPlus_PathGetWorldBounds($hPath, 0, $hPen)
Until Abs(($iWidth - $iDistance * 2) - $aBounds[2]) <= 10 Or Abs(($iHeight - $iDistance * 2) - $aBounds[3]) <= 10

$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, -$aBounds[0] + $iWidth / 2 - $aBounds[2] / 2, -$aBounds[1] + $iHeight / 2 - $aBounds[3] / 2)
_GDIPlus_PathTransform($hPath, $hMatrix)
_GDIPlus_MatrixDispose($hMatrix)

_GDIPlus_GraphicsDrawPath($hBuffer, $hPath, $hPen)
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight)

_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_GraphicsDispose($hBuffer)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_PenDispose($hPen)
_GDIPlus_PathDispose($hPath)
_GDIPlus_Shutdown()

While GUIGetMsg() <> -3
WEnd