-
Notifications
You must be signed in to change notification settings - Fork 0
/
histo.go
82 lines (66 loc) · 1.77 KB
/
histo.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
package main
import (
"fmt"
"golang.org/x/image/colornames"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/text"
"golang.org/x/image/font/basicfont"
)
type bar struct {
Value int
isComparing bool
imd *imdraw.IMDraw
}
func (histo *bar) draw(target pixel.Target, pos int) {
w := float64(windowWidth) / float64(size)
h1 := float64(windowHeight) / (float64(size) + 1) * float64(histo.Value+1)
x0 := w * float64(pos)
x1 := x0 + w - 1
c := koColor
if pos == histo.Value {
c = okColor
}
if histo.isComparing {
c = ckColor
}
imd := histo.imd
imd.Clear()
imd.Reset()
imd.Color = c
imd.Push(pixel.V(x0, 0))
imd.Push(pixel.V(x1, h1))
imd.Rectangle(0)
imd.Draw(target)
}
var histogram []bar
var statsText *text.Text
var debugText *text.Text
var face *basicfont.Face
func initHisto() {
histogram = make([]bar, size)
for i := 0; i < size; i++ {
histogram[i] = bar{Value: i, imd: imdraw.New(nil)}
}
face = basicfont.Face7x13
atlas := text.NewAtlas(face, text.ASCII)
statsText = text.New(pixel.V(10, windowHeight-1.5*float64(face.Height)), atlas)
statsText.Color = colornames.Aqua
statsText.LineHeight = float64(face.Height) * 1.1
debugText = text.New(pixel.V(200, windowHeight-1.5*float64(face.Height)), atlas)
debugText.Color = colornames.Aqua
}
func drawHisto(target pixel.Target) {
for i, j := range globalData.array {
histogram[j].draw(target, i)
}
drawStats(target)
}
func drawStats(target pixel.Target) {
statsText.Clear()
statsText.WriteString(fmt.Sprintf("%v\n array size: %v\n tests: %v\n swaps: %v", algorithm, size, numOfComparison, numOfSwaps))
if dataProcessed {
statsText.WriteString(fmt.Sprintf("\n time: %v\n\n(press ESC to quit)", elapsedTime))
}
statsText.Draw(target, pixel.IM)
}