Advertisement
Guest User

Untitled

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