Advertisement
thedemonsmsc

Sorting visualiz

Sep 20th, 2019
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 5.96 KB | None | 0 0
  1.  
  2. #include <WinAPI.au3>
  3. #include <GDIPlus.au3>
  4. #include <misc.au3>
  5. #include <WindowsConstants.au3>
  6. #include <Array.au3>
  7.  
  8. ; gdiplus
  9. Global $g_hGfxCtxt, $hDC, $hHBitmap, $buffer
  10.  
  11. ; pen & color
  12. Global $hPen[0], $hPenPading, $hPenChoosing
  13. Global $c_Pen, $c_PenPadding, $c_Choosing
  14. Global $cBackground = 0xFF000000
  15.  
  16. ; value
  17. Global $nValues = 200 ; number of array
  18. Global $Values[ $nValues ]
  19. Global $maxValue = 1000
  20. Global $isLoop = False
  21. Global $sVal = 0
  22. Global $sLoop = 0
  23. Global $Steps = $nValues / 10 ; step for sorting, bigger = faster
  24.  
  25. ; gui
  26. Global $iPadding = 10
  27. Global $GuiW = 800, $GuiH = 600
  28. Global $valueWidth = $GuiW / $nValues
  29. Global $iChoosing = 0
  30.  
  31. $GuiW += $iPadding * 2
  32. $GuiH += $iPadding * 2
  33.  
  34.  
  35. Global $DisplayMode = 1 ; Displaymode: 0 = color; 1 = length
  36.  
  37. $GUI = GUICreate("Sorting algorithm visualize", $GuiW, $GuiH)
  38. GUISetState()
  39.  
  40. _Graphic_StartUp()
  41. CreateValues()
  42.  
  43. While 1
  44.  
  45.     For $i = 1 To $Steps
  46.         $iChoosing = Sort()
  47.     Next
  48.  
  49.     Draw()
  50.  
  51.     Switch GUIGetMsg()
  52.         Case - 3
  53.             Exit
  54.     EndSwitch
  55. WEnd
  56.  
  57. Func Draw()
  58.  
  59.     _GDIPlus_GraphicsClear($g_hGfxCtxt, $cBackground)
  60.  
  61.     Switch $DisplayMode
  62.  
  63.         Case 0
  64.  
  65.             $x = $iPadding + $valueWidth / 2
  66.             For $iVal = 0 To $nValues - 1
  67.  
  68.                 _GDIPlus_GraphicsDrawLine($g_hGfxCtxt, $x, $iPadding, $x, $GuiH - $iPadding, $hPen[$iVal])
  69.  
  70.                 If $iVal = $iChoosing Then _GDIPlus_GraphicsDrawLine($g_hGfxCtxt, $x, $iPadding, $x, $GuiH - $iPadding, $hPenChoosing)
  71.  
  72.                 $x += $valueWidth ; padding + index of value * width of value
  73.             Next
  74.  
  75.         Case 1
  76.  
  77.             $x = $iPadding + $valueWidth / 2
  78.             For $iVal = 0 To $nValues - 1
  79.  
  80.                 $height = __map( $Values[$iVal], 0, $maxValue, $iPadding, $GuiH - $iPadding * 2)
  81.  
  82.                 _GDIPlus_GraphicsDrawLine($g_hGfxCtxt, $x, $GuiH - $iPadding - $height, $x, $GuiH - $iPadding, $hPen)
  83.  
  84.                 If $iVal = $iChoosing Then _GDIPlus_GraphicsDrawLine($g_hGfxCtxt, $x, $GuiH - $iPadding - $height, $x, $GuiH - $iPadding, $hPenChoosing)
  85.  
  86.                 $x += $valueWidth ; padding + index of value * width of value
  87.             Next
  88.  
  89.     EndSwitch
  90.     $padXY = $iPadding - 1
  91.     $padW = $GuiW - $iPadding * 2 + 2
  92.     $padH = $GuiH - $iPadding * 2 + 2
  93.  
  94.     _GDIPlus_GraphicsDrawRect($g_hGfxCtxt, $padXY, $padXY, $padW, $padH, $hPenPading) ; padding line
  95.  
  96.     _WinAPI_BitBlt($hDC, 0, 0, $GuiW, $GuiH, $buffer, 0, 0, $SRCCOPY)
  97. EndFunc
  98.  
  99. Func DrawFinal()
  100.  
  101.     For $i = 0 To $nValues - 1 Step + 2
  102.         $iChoosing = $i
  103.         Draw()
  104.     Next
  105. EndFunc
  106.  
  107. Func Sort()
  108.  
  109. ;~  If $sLoop <= $nValues - 1 Then
  110.  
  111.     If $sVal < $nValues - 1 Then
  112.         _sort()
  113.  
  114.         If $isLoop = False Then
  115.  
  116.             $sVal += 1
  117.  
  118.             Return $sVal
  119.         Else
  120.  
  121.             $sLoop -= 1
  122.             If $sLoop < 1 Then $isLoop = False
  123.  
  124.             Return $sLoop
  125.  
  126.         EndIf
  127.     Else
  128.         $sVal = 0
  129.         DrawFinal()
  130.         CreateValues()
  131.     EndIf
  132.  
  133.     Return False
  134. EndFunc
  135.  
  136. Func _sort()
  137.  
  138.     If $isLoop = False Then
  139.  
  140.         If $Values[ $sVal ] > $Values[ $sVal + 1] Then
  141.  
  142.             _swap( $Values[ $sVal ], $Values[ $sVal + 1] )
  143.  
  144.             If $DisplayMode = 0 Then _swap( $hPen[ $sVal ], $hPen[ $sVal + 1] )
  145.  
  146.             $isLoop = True
  147.             $sLoop = $sVal + 1
  148.         EndIf
  149.  
  150.     Else
  151.  
  152.         If $Values[$sLoop] >= $Values[$sLoop - 1] Then $isLoop = False
  153.  
  154.         If $isLoop Then
  155.  
  156.             _swap( $Values[ $sLoop ], $Values[ $sLoop - 1] )
  157.             If $DisplayMode = 0 Then _swap( $hPen[ $sLoop ], $hPen[ $sLoop - 1] )
  158.         EndIf
  159.  
  160.     EndIf
  161. EndFunc
  162.  
  163. Func _swap(ByRef $x, ByRef $y)
  164.     $temp = $x
  165.     $x = $y
  166.     $y = $temp
  167. EndFunc
  168.  
  169. Func CreateValues()
  170.  
  171.     For $iVal = 0 To $nValues - 1
  172.  
  173.         $Values[ $iVal ] = Random(0, $maxValue, 1) ; 000000 - FFFFFF hex colors
  174.     Next
  175.  
  176.     Switch $DisplayMode
  177.         Case 0
  178.  
  179.             ReDim $hPen[$nValues]
  180.             For $iVal = 0 To $nValues - 1
  181.                 $hPen[$iVal] = _GDIPlus_PenCreate( __getColor( $Values[$iVal] ), $valueWidth)
  182.             Next
  183.         Case 1
  184.             $hPen = _GDIPlus_PenCreate( $c_Pen, $valueWidth)
  185.     EndSwitch
  186. EndFunc
  187.  
  188. Func _Graphic_StartUp()
  189.  
  190.     _GDIPlus_Startup()
  191.  
  192.     ; install gdi
  193.  
  194.     $hDC = _WinAPI_GetDC($GUI)
  195.     $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $GuiW, $GuiH)
  196.  
  197.     $buffer = _WinAPI_CreateCompatibleDC($hDC)
  198.     _WinAPI_SelectObject($buffer, $hHBitmap)
  199.  
  200.     $g_hGfxCtxt = _GDIPlus_GraphicsCreateFromHDC($buffer)
  201.     _GDIPlus_GraphicsSetSmoothingMode($g_hGfxCtxt, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
  202.     _GDIPlus_GraphicsSetPixelOffsetMode($g_hGfxCtxt, $GDIP_PIXELOFFSETMODE_HIGHQUALITY)
  203.  
  204.     Switch $DisplayMode
  205.  
  206.         Case 0
  207.             $c_Pen = 0xFFFF0000
  208.             $c_PenPadding = 0xFFFFFFFF
  209.             $c_Choosing = 0xFFFFFFFF
  210.  
  211.         Case 1
  212.             $c_Pen = 0xFFFFFFFF
  213.             $c_PenPadding = 0xFFFFFFFF
  214.             $c_Choosing = 0xFF00FF00
  215.     EndSwitch
  216.  
  217.     $hPenPading = _GDIPlus_PenCreate($c_PenPadding, 2)
  218.     $hPenChoosing = _GDIPlus_PenCreate($c_Choosing, $valueWidth)
  219. EndFunc
  220.  
  221. Func __getColor($value)
  222.  
  223.     Local $Step[5]
  224.  
  225.     $istep = 5
  226.     For $i = 0 To 4
  227.  
  228.         $Step[$i] = $maxValue * $istep / 6
  229.         $istep -= 1
  230.     Next
  231.  
  232.     If $value >= $Step[0] Then
  233.  
  234.         $color = __map($value, $Step[0], $maxValue, 0xFF, 0x00)
  235.         $color = Round( $color )
  236.         Return 0xFFFF0000 + $color ; FF 00 xx
  237.  
  238.     ElseIf $value >= $Step[1] Then
  239.  
  240.         $color = __map($value, $Step[1], $Step[0], 0x00, 0xFF)
  241.         $color = Round( $color )
  242.         Return 0xFF0000FF + $color * 0x10000 ; xx 00 FF
  243.  
  244.     ElseIf $value >= $Step[2] Then
  245.  
  246.         $color = __map($value, $Step[2], $Step[1], 0xFF, 0x00)
  247.         $color = Round( $color )
  248.         Return 0xFF0000FF + $color * 0x100; 00 xx FF
  249.  
  250.     ElseIf $value >= $Step[3] Then
  251.  
  252.         $color = __map($value, $Step[3], $Step[2], 0x00, 0xFF)
  253.         $color = Round( $color )
  254.         Return 0xFF00FF00 + $color ; 00 FF xx
  255.  
  256.     ElseIf $value >= $Step[4] Then
  257.  
  258.         $color = __map($value, $Step[4], $Step[3], 0xFF, 0x00)
  259.         $color = Round( $color )
  260.         Return 0xFF00FF00 + $color * 0x10000; xx FF 00
  261.  
  262.     Else
  263.  
  264.         $color = __map($value, 0, $Step[4], 0x00, 0xFF)
  265.         $color = Round( $color )
  266.         Return 0xFFFF0000 + $color * 0x100; xx FF 00
  267.  
  268.     EndIf
  269.  
  270. EndFunc
  271.  
  272. Func __minmax($x, $min, $max)
  273.     Return ($x - $min) / ($max - $min)
  274. EndFunc
  275.  
  276. Func __map($x, $min1, $max1, $min2, $max2)
  277.     If $max2 > $min2 Then
  278.         Return __minmax($x, $min1, $max1) * ($max2 - $min2) + $min2
  279.     Else
  280.         Return  (1 - __minmax($x, $min1, $max1)) * ($min2 - $max2) + $max2
  281.     EndIf
  282. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement