Skip to content

Commit 3454c2d

Browse files
author
Ronna Steinberg
committed
revised
1 parent b058482 commit 3454c2d

File tree

9 files changed

+119
-13
lines changed

9 files changed

+119
-13
lines changed

gopher.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222

2323
func ColorGopherFunc(img image.Image) renderer.ColorFunc {
2424
// to get the original image height uncomment this:
25-
//imgHeight := img.Bounds().Max.Y - img.Bounds().Min.Y
25+
imgHeight := img.Bounds().Max.Y - img.Bounds().Min.Y
2626
return func(x, xOffset, y, yOffset, height int) color.Color {
27-
return color.Black
27+
ratio := float64(imgHeight) / float64(height)
28+
return img.At(int(float64(x-xOffset)*ratio), int(float64(y-yOffset)*ratio))
2829
}
2930
}
3031

gopher_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func (m mockImage) At(x, y int) color.Color {
3030
func TestColorGopherFunc(t *testing.T) {
3131
rand.Seed(time.Now().UnixNano())
3232

33-
dim := rand.Intn(100)
34-
newDim := rand.Intn(dim)
33+
dim := rand.Intn(100) + 1
34+
newDim := rand.Intn(dim) + 1
3535

3636
recX, recY := math.MinInt64, math.MinInt64
3737
res := color.Gray{Y: uint8(rand.Intn(255))}

renderer/color.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package renderer
33
import "image/color"
44

55
type (
6-
ColorFunc func(int, int, int, int, int) color.Color
6+
ColorFunc func(x, xOffset, y, yOffset, height int) color.Color
77

88
Colorer struct {
99
Fn ColorFunc

renderer/project.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ func max(points []int) int {
2323
// therefore the result slice will be [3,6,18]
2424

2525
func Project(in []int, newMax int) []int {
26-
return nil
26+
out := make([]int, len(in))
27+
m := max(in)
28+
for i, p := range in {
29+
out[i] = p * newMax / m
30+
}
31+
return out
2732
}

renderer/rectangle.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ type Img interface {
1616
// paint each pixel To color a pixel use img.Set
1717

1818
func DrawRectangle(img Img, x1, y1, x2, y2 int, height int, colorFunc ColorFunc) {
19+
for x := x1; x < x2; x++ {
20+
for y := y1; y < y2; y++ {
21+
img.Set(x, y, colorFunc(x, x1, y, y1, height))
22+
}
23+
}
1924
}

sampler/abs_avg.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package sampler
22

3+
import "math"
4+
35
// AbsAvg calculates the average of the absolute values in the given slice
46
func AbsAvg(in []int) int {
5-
return 0
7+
sum := 0
8+
for _, i := range in {
9+
sum += int(math.Abs(float64(i)))
10+
}
11+
return sum / len(in)
612
}

sampler/avg_abs.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package sampler
22

3+
import "math"
4+
35
// AvgAbs calculates the absolute value of the average
46
// of the values in the given slice
57
func AvgAbs(in []int) int {
6-
return 0
8+
sum := 0
9+
for _, i := range in {
10+
sum += i
11+
}
12+
return int(math.Abs(float64(sum))) / len(in)
713
}

sampler/compress.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@ var errCountInvalid = errors.New("count must be <= length of the slice")
1717
// * hint: use windowSize
1818

1919
func Compress(samples []int, count int, fn SamplerFunc) ([]int, error) {
20-
return nil, nil
20+
if count > len(samples) {
21+
return nil, errCountInvalid
22+
}
23+
out := make([]int, count)
24+
w := windowSize(len(samples), count)
25+
for i := 0; i < count; i++ {
26+
low := i * w
27+
high := (i + 1) * w
28+
if low > len(samples) {
29+
out[i] = fn([]int{})
30+
} else {
31+
if high > len(samples) {
32+
out[i] = fn(samples[low:])
33+
} else {
34+
out[i] = fn(samples[low:high])
35+
}
36+
}
37+
}
38+
return out, nil
2139
}
2240

2341
func windowSize(l, c int) int {

sampler/compress_test.go

+69-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sampler
22

33
import (
4+
"math"
45
"reflect"
56
"testing"
67
)
@@ -17,7 +18,6 @@ func TestTransform(t *testing.T) {
1718
return v[0]
1819
}
1920

20-
//Test basic transform
2121
in = []int{1, 2, 3}
2222
s, err := Compress(in, 2, fn)
2323
if err != nil {
@@ -29,15 +29,80 @@ func TestTransform(t *testing.T) {
2929
if !reflect.DeepEqual(in, acc) {
3030
t.Fatalf("Compress was not called with all the integers in the given slice expected: %v, got: %v", in, acc)
3131
}
32-
acc = acc[:0]
32+
}
33+
34+
func TestTransformBounds(t *testing.T) {
35+
var acc []int
36+
var in []int
37+
38+
fn := func(v []int) int {
39+
acc = append(acc, v...)
40+
sum := 0
41+
for _, i := range v {
42+
sum += i
43+
}
44+
return sum
45+
}
46+
47+
in = []int{1, 2, 3, 4}
48+
s, err := Compress(in, 2, fn)
49+
if err != nil {
50+
t.Fatal("Compress was expected to succeed")
51+
}
52+
expected := []int{3, 7}
53+
if !reflect.DeepEqual(expected, s) {
54+
t.Fatalf("Compress was expected to return %v but returned %v", expected, s)
55+
}
56+
if !reflect.DeepEqual(in, acc) {
57+
t.Fatalf("Compress was not called with all the integers in the given slice expected: %v, got: %v", in, acc)
58+
}
59+
}
60+
61+
func TestTransformBounds2(t *testing.T) {
62+
var acc []int
63+
var in []int
64+
65+
fn := func(v []int) int {
66+
if len(v) == 0 {
67+
return math.MaxUint8
68+
}
69+
acc = append(acc, v...)
70+
sum := 0
71+
for _, i := range v {
72+
sum += i
73+
}
74+
return sum
75+
}
76+
77+
in = []int{1, 2, 3, 4, 5, 6, 7, 8}
78+
s, err := Compress(in, 6, fn)
79+
if err != nil {
80+
t.Fatal("Compress was expected to succeed")
81+
}
82+
expected := []int{3, 7, 11, 15, 255, 255}
83+
if !reflect.DeepEqual(expected, s) {
84+
t.Fatalf("Compress was expected to return %v but returned %v", expected, s)
85+
}
86+
if !reflect.DeepEqual(in, acc) {
87+
t.Fatalf("Compress was not called with all the integers in the given slice expected: %v, got: %v", in, acc)
88+
}
89+
}
90+
91+
func TestTransformError(t *testing.T) {
92+
var in []int
93+
94+
fn := func(v []int) int {
95+
t.Fatal("sampler was expected te be called")
96+
return 0
97+
}
3398

3499
//Test transform with count larger than number of elements
35-
s, err = Compress(in, len(in)+1, fn)
100+
_, err := Compress(in, len(in)+1, fn)
36101
if err == nil {
37102
t.Fatal("Compress was expected to fail")
38103
}
39-
}
40104

105+
}
41106
func TestWindowSize(t *testing.T) {
42107
if windowSize(1, 3) != 1 {
43108
t.Error("window size was expected to return 1")

0 commit comments

Comments
 (0)