Skip to content

Instantly share code, notes, and snippets.

@G33kDude
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save G33kDude/f4ba4825c8b069bc9b3f to your computer and use it in GitHub Desktop.
Save G33kDude/f4ba4825c8b069bc9b3f to your computer and use it in GitHub Desktop.
Quicksort fo /r/DailyProgrammer
Size := 600
PixSize := 1
GuiSize := Size * PixSize
List := []
Loop, % Size
List.Insert(Rand(0.0, 1.0))
Gui, +hWndhWnd -Caption
hDC := DllCall("GetDC", "UPtr", hWnd, "UPtr")
hMemDC := DllCall("CreateCompatibleDC", "UPtr", hDC, "UPtr")
hBitmap := DllCall("CreateCompatibleBitmap", "UPtr", hDC, "Int", GuiSize, "Int", GuiSize, "UPtr")
DllCall("SelectObject", "UPtr", hMemDC, "UPtr", hBitmap)
OnExit, ExitSub
OnMessage(0xF, "WM_PAINT")
Gui, Show, w%GuiSize% h%GuiSize%
QuickSort(List, 1, Size) ; Built in draw
SetRectangle(hMemDC, 0, 0, GuiSize, GuiSize, 0)
Draw(List, hMemDC, PixSize)
BitBlt(hDC, 0, 0, GuiSize, GuiSize, hMemDC)
MsgBox, Done
ExitApp
return
GuiEscape:
GuiClose:
ExitApp
return
ExitSub:
DllCall("SelectObject", "UPtr", hMemDC, "UPtr", hBitmap)
DllCall("DeleteObject", "UPtr", hBitmap)
DllCall("DeleteObject", "UPtr", hMemDC)
DllCall("ReleaseDC", "UPtr", hWnd, "UPtr", hDC)
ExitApp
return
QuickSort(List, Low, High)
{
global hDC, hMemDC, PixSize, GuiSize
if (Low < High)
{
Pivot := ChoosePivot(List, Low, High)
Mid := Partition(List, Pivot, Low, High)
SetRectangle(hMemDC, (Low-1)*PixSize, 0, (High-Low+1)*PixSize, GuiSize, 0)
Draw(List, hMemDC, PixSize, Low, High)
BitBlt(hDC, 0, 0, GuiSize, GuiSize, hMemDC)
QuickSort(List, Low, Mid-1)
QuickSort(List, Mid+1, High)
}
}
Partition(Array, Pivot, Low, High)
{
PivotValue := Array[Pivot]
Array[Pivot] := Array[High]
Array[High] := PivotValue
Midpoint := Low
Loop, % High-Low
{
Index := Low+A_Index-1
if (Array[Index] < PivotValue || (Array[Index] = PivotValue && Rand(0, 1)))
{
; Move item to left section
Tmp := Array[Index]
Array[Index] := Array[Midpoint]
Array[Midpoint] := Tmp
Midpoint++
}
}
; Put pivot back in place
Tmp := Array[Midpoint]
Array[Midpoint] := Array[High]
Array[High] := Tmp
return Midpoint
}
ChoosePivot(Array, Low, High)
{
return Rand(Low, High) ; lol
}
Draw(List, hDC, PixSize, Start=-1, End=-1)
{
Size := List.MaxIndex()
for Index, Number in List
{
if (Index < Start)
Continue
if (End > 0 && Index > End)
Continue
x := (Index-1)*PixSize
y := Number*PixSize*(Size-1) ; -1 to leave room for the extra height
SetRectangle(hDC, x, y, PixSize, PixSize, Number*0xFFFFFF)
}
}
WM_PAINT(wParam, lParam, Msg, hWnd)
{
global hDC, GuiSize, hMemDC
BitBlt(hDC, 0, 0, GuiSize, GuiSize, hMemDC)
}
SetPixel(hDC, x, y, Color)
{
DllCall("SetPixelV", "UPtr", hDC, "Int", x, "Int", y, "UInt", Color)
}
SetRectangle(hDC, x, y, w, h, Color)
{
static Pens:=[], Brushes:=[]
if (w == 1 && h == 1)
return SetPixel(hDC, x, y, Color)
; If this.hPen && !DllCall("DeleteObject","UPtr",this.hPen)
; If this.hBrush && !DllCall("DeleteObject","UPtr",this.hBrush)
if !Pens[Color]
Pens[Color] := DllCall("CreatePen", "Int", 0, "Int", 0, "UInt", Color, "UPtr") ;PS_SOLID
if !Brushes[Color]
Brushes[Color] := DllCall("CreateSolidBrush", "UInt", Color, "UPtr")
; Replace the original pen and brush with our own
hOriginalPen := DllCall("SelectObject", "UPtr", hDC, "UPtr", Pens[Color], "UPtr")
hOriginalBrush := DllCall("SelectObject", "UPtr", hDC, "UPtr", Brushes[Color], "UPtr")
DllCall("Rectangle", "UPtr", hDC, "Int", x, "Int", y, "Int", x+w, "Int", y+h)
; Reselect the original pen and brush
DllCall("SelectObject", "UPtr", hDC, "UPtr", hOriginalPen, "UPtr")
DllCall("SelectObject", "UPtr", hDC, "UPtr", hOriginalBrush, "UPtr")
}
BitBlt(hDC, x, y, w, h, hSrcDC)
{
DllCall("BitBlt", "UPtr", hDC, "Int", x, "Int", y, "Int", w, "Int", h, "UPtr", hSrcDC, "Int", 0, "Int", 0, "UInt", 0xCC0020) ;SRCCOPY
}
Rand(Min, Max)
{
Random, Rand, Min, Max
return Rand
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment