-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (89 loc) · 2.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
type algo string
var (
win *pixelgl.Window
windowWidth, windowHeight float64
algorithm algo
size int
speed int
koColor, bgColor, okColor, ckColor pixel.RGBA
runningTime time.Time
elapsedTime time.Duration
debug bool
startDelay int
dataAsc, dataDesc bool
dataRndSeed int64
)
const (
golibSort algo = "golibsort"
quickSort algo = "quicksort"
bubbleSort algo = "bubblesort"
insertionSort algo = "insertionsort"
selectionSort algo = "selectionsort"
shellSort algo = "shellsort"
cocktailSort algo = "cocktailsort"
mergeSort algo = "mergesort" //TODO
)
var algos map[algo]sorter
func init() {
algos = make(map[algo]sorter)
algos[golibSort] = golibSortAlgo{}
algos[quickSort] = quickSortAlgo{}
algos[bubbleSort] = bubbleSortAlgo{}
algos[insertionSort] = insertionSortAlgo{}
algos[selectionSort] = selectionSortAlgo{}
algos[shellSort] = shellSortAlgo{}
//algos[cocktailSort] = cocktailSortAlgo{}
//algos[mergeSort] = mergeSortAlgo{} // TODO
koColor = pixel.RGB(1, 0, 0)
bgColor = pixel.RGB(0, 0, 0)
okColor = pixel.RGB(0, 1, 0)
ckColor = pixel.RGB(1, 1, 0)
}
func main() {
parseArguments()
initHisto()
pixelgl.Run(run)
}
func run() {
cfg := pixelgl.WindowConfig{
Title: fmt.Sprintf("Visual Sort [%v]", algorithm),
Bounds: pixel.R(0, 0, float64(windowWidth), float64(windowHeight)),
VSync: true,
Resizable: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
event := time.Tick(time.Duration(speed) * time.Millisecond)
newData(event)
var algo sorter
var found bool
if algo, found = algos[algorithm]; !found {
panic("Algorithm not found")
}
time.Sleep(time.Duration(startDelay) * time.Second)
go func() {
runningTime = time.Now()
algo.sort(&globalData)
globalData.endOfWork()
}()
for !win.Closed() {
if dataProcessed {
time.Sleep(50 * time.Millisecond)
if win.Pressed(pixelgl.KeyEscape) {
break
}
}
win.Clear(bgColor)
drawHisto(win)
win.Update()
}
}