Skip to content

Commit c4ed1c1

Browse files
committed
fix: static analysis and lint check
1 parent 46fdef1 commit c4ed1c1

File tree

8 files changed

+125
-77
lines changed

8 files changed

+125
-77
lines changed

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ linters-settings:
9797
statements: 40
9898
gocognit:
9999
# minimal code complexity to report, 30 by default (but we recommend 10-20)
100-
min-complexity: 10
100+
min-complexity: 15
101101
gocritic:
102102
# Which checks should be enabled; can't be combined with 'disabled-checks';
103103
# See https://go-critic.github.io/overview#checks-overview
@@ -114,7 +114,7 @@ linters-settings:
114114
sizeThreshold: 70
115115
gocyclo:
116116
# minimal code complexity to report, 30 by default (but we recommend 10-20)
117-
min-complexity: 10
117+
min-complexity: 15
118118
godot:
119119
# check that each sentence starts with a capital letter
120120
capital: true

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ In the example below, 1 dimmentional method `Noise.Eval64(x)` was used to genera
1414
//
1515
// noiseType := noise.Perlin
1616
// noiseType := noise.OpenSimplex
17-
n := noise.New(noiseType, seed)
17+
n, err := noise.New(noiseType, seed)
1818

1919
yy := n.Eval64(x / smoothness) // yy is between -1.0 and 1.0 of float64
20-
21-
y := (yy + 1) / 2 * 500 // y is between 0 and 500
20+
y := (yy + 1) / 2 * 500 // y is between 0 and 500
2221
```
2322

2423
![](./_example/2d/2d_perlin.png)
@@ -30,8 +29,8 @@ y := (yy + 1) / 2 * 500 // y is between 0 and 500
3029

3130
```go
3231
// Obtain the noise value at the position (x, y)
33-
n := noise.New(noiseType, seed)
34-
v := n.Eval64(x, y) // y is between -1.0 and 1.0 of float64
32+
n, err := noise.New(noiseType, seed)
33+
v := n.Eval64(x, y) // v is between -1.0 and 1.0 of float64
3534
```
3635

3736
To create a 2D image, plot the `v` value at the position `(x, y)` in the 2D space. The 2D image example is equivalent to a frame of the 3D image example below.
@@ -46,8 +45,8 @@ The X and Y axes are the 2D image and the Z axis is the "time", or animation fra
4645

4746
```go
4847
// Obtain the noise value at the position (x, y, z)
49-
n := noise.New(noise.Perlin, seed)
50-
v := n.Eval64(x, y, z) // y is between -1.0 and 1.0 of float64
48+
n, err := noise.New(noise.Perlin, seed)
49+
v := n.Eval64(x, y, z) // v is between -1.0 and 1.0 of float64
5150
```
5251

5352
![](./_example/3d/animation_perlin.gif)
@@ -58,8 +57,8 @@ v := n.Eval64(x, y, z) // y is between -1.0 and 1.0 of float64
5857

5958
```go
6059
// Obtain the noise value at the position (x, y, z)
61-
n := noise.New(noise.OpenSimplex, seed)
62-
v := n.Eval64(x, y, z) // y is between -1.0 and 1.0 of float64
60+
n, err := noise.New(noise.OpenSimplex, seed)
61+
v := n.Eval64(x, y, z) // v is between -1.0 and 1.0 of float64
6362
```
6463

6564
![](./_example/3d/animation_opensimplex.gif)
@@ -135,6 +134,10 @@ const frame = 50
135134
// Create new noise generator of Perlin type
136135
genNoise, err := noise.New(noise.Perlin, seed)
137136

137+
if err != nil {
138+
// error handle
139+
}
140+
138141
for z := 0; z < frame; z++ {
139142
zz := float64(z) / steps
140143

@@ -165,3 +168,16 @@ for z := 0; z < frame; z++ {
165168
```
166169

167170
- [Complete Source](./_example/3d)
171+
172+
#### Advanced
173+
174+
To change `alpha` and `beta` values of Perling noise.
175+
176+
```go
177+
alpha := 0.5
178+
beta := 0.5
179+
180+
n, err := noise.New(noise.Perlin, seed)
181+
n.Smoothness = alpha
182+
n.Scale = beta
183+
```

example_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
)
1010

1111
func ExampleNew() {
12-
noise, err := noise.New(noise.OpenSimplex, rand.Int63())
12+
//nolint:gosec // Example value
13+
seed := rand.Int63()
14+
15+
noise, err := noise.New(noise.OpenSimplex, seed)
1316
if err != nil {
1417
log.Fatal(err)
1518
}
@@ -31,7 +34,7 @@ func ExampleNew() {
3134
// OpenSimplex Noise
3235
// ----------------------------------------------------------------------------
3336

34-
func ExampleNew_one_dimention_opensimplex_noise_in_float32() {
37+
func ExampleNew_one_dimension_opensimplex_noise_in_float32() {
3538
const seed = 100
3639

3740
gen, err := noise.New(noise.OpenSimplex, seed)
@@ -53,7 +56,7 @@ func ExampleNew_one_dimention_opensimplex_noise_in_float32() {
5356
// 2;0.1385
5457
}
5558

56-
func ExampleNew_two_dimention_opensimplex_noise_in_float32() {
59+
func ExampleNew_two_dimension_opensimplex_noise_in_float32() {
5760
const seed = 100
5861

5962
gen, err := noise.New(noise.OpenSimplex, seed)
@@ -78,7 +81,7 @@ func ExampleNew_two_dimention_opensimplex_noise_in_float32() {
7881
// 1;1;0.0950
7982
}
8083

81-
func ExampleNew_three_dimention_opensimplex_noise_in_float32() {
84+
func ExampleNew_three_dimension_opensimplex_noise_in_float32() {
8285
const seed = 100
8386

8487
gen, err := noise.New(noise.OpenSimplex, seed)
@@ -109,7 +112,7 @@ func ExampleNew_three_dimention_opensimplex_noise_in_float32() {
109112
// 1;1;1;-0.1585
110113
}
111114

112-
func ExampleNew_one_dimention_opensimplex_in_float64() {
115+
func ExampleNew_one_dimension_opensimplex_in_float64() {
113116
const seed = 100
114117

115118
gen, err := noise.New(noise.OpenSimplex, seed)
@@ -125,9 +128,9 @@ func ExampleNew_one_dimention_opensimplex_in_float64() {
125128
}
126129

127130
// Output:
128-
// 0;0.0000
129-
// 1;0.0950
130-
// 2;0.1385
131+
// 0.0000;0.0000
132+
// 0.1000;0.0950
133+
// 0.2000;0.1385
131134
}
132135

133136
// ----------------------------------------------------------------------------

noise.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import (
66
"github.com/pkg/errors"
77
)
88

9-
type NoiseType int
9+
// Algo represents a noise type of the algorithm to generate noise.
10+
type Algo int
1011

1112
const (
12-
Unknown NoiseType = iota
13+
// Unknown noise type.
14+
Unknown Algo = iota
15+
// Perlin noise type.
1316
Perlin
17+
// OpenSimplex noise type.
1418
OpenSimplex
1519
)
1620

@@ -43,7 +47,7 @@ type Noise interface {
4347
// ----------------------------------------------------------------------------
4448

4549
// New returns a new noise generator.
46-
func New(noiseType NoiseType, seed int64) (Noise, error) {
50+
func New(noiseType Algo, seed int64) (Noise, error) {
4751
switch noiseType {
4852
case Perlin:
4953
return perlin.New(seed), nil

pkg/opensimplex/opensimplex.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ func New(seed int64) *Noise {
1717
// Type: Noise
1818
// ----------------------------------------------------------------------------
1919

20+
// Noise holds parameter values for OpenSimplex noise. It is an implementation
21+
// of Noise interface.
2022
type Noise struct {
2123
// Seed holds the seed value for the noise.
2224
Seed int64
2325
}
2426

2527
// ----------------------------------------------------------------------------
26-
// Methods
28+
// Methods (Public)
2729
// ----------------------------------------------------------------------------
2830

2931
// Eval32 returns a float32 noise value for the given coordinates.
@@ -54,42 +56,46 @@ func (n *Noise) Eval64(dim ...float64) float64 {
5456
return 0
5557
}
5658

57-
// eval1D32 generates 1-dimensional OpenSimplex Noise value.
59+
// ----------------------------------------------------------------------------
60+
// Methods (Private)
61+
// ----------------------------------------------------------------------------
62+
63+
// eval1D32 generates float32 OpenSimplex noise value from 1-dimensional coordinate.
5864
func (n *Noise) eval1D32(x float32) float32 {
5965
p := orig.New32(n.Seed)
6066

6167
return p.Eval2(x, x)
6268
}
6369

64-
// eval2D32 generates 2-dimensional OpenSimplex Noise value.
70+
// eval2D32 generates float32 OpenSimplex noise value from 2-dimensional coordinates.
6571
func (n *Noise) eval2D32(x, y float32) float32 {
6672
p := orig.New32(n.Seed)
6773

6874
return p.Eval2(x, y)
6975
}
7076

71-
// eval3D32 generates 3-dimensional OpenSimplex Noise value.
77+
// eval3D32 generates float32 OpenSimplex noise value from 3-dimensional coordinates.
7278
func (n *Noise) eval3D32(x, y, z float32) float32 {
7379
p := orig.New32(n.Seed)
7480

7581
return p.Eval3(x, y, z)
7682
}
7783

78-
// eval1D64 generates 1-dimensional OpenSimplex Noise value.
84+
// eval1D64 generates float64 OpenSimplex noise value from 1-dimensional coordinate.
7985
func (n *Noise) eval1D64(x float64) float64 {
8086
p := orig.New(n.Seed)
8187

8288
return p.Eval2(x, x)
8389
}
8490

85-
// eval2D64 generates 2-dimensional OpenSimplex Noise value.
91+
// eval2D64 generates float64 OpenSimplex noise value from 2-dimensional coordinates.
8692
func (n *Noise) eval2D64(x, y float64) float64 {
8793
p := orig.New(n.Seed)
8894

8995
return p.Eval2(x, y)
9096
}
9197

92-
// eval3D64 generates 3-dimensional OpenSimplex Noise value.
98+
// eval3D64 generates float64 OpenSimplex noise value from 3-dimensional coordinates.
9399
func (n *Noise) eval3D64(x, y, z float64) float64 {
94100
p := orig.New(n.Seed)
95101

pkg/opensimplex/opensimplex_test.go

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,41 @@ import (
1010
"github.com/stretchr/testify/require"
1111
)
1212

13+
// samples is a list of assert values for testing from the original OpenSimplex package.
1314
var samples [][]float64
1415

16+
func TestSamplesMatch(t *testing.T) {
17+
samples = loadSamples(t)
18+
19+
n := opensimplex.New(0)
20+
21+
for _, s := range samples {
22+
var expected, actual float64
23+
24+
switch len(s) {
25+
case 3:
26+
expected = s[2]
27+
actual = n.Eval64(s[0], s[1])
28+
case 4:
29+
expected = s[3]
30+
actual = n.Eval64(s[0], s[1], s[2])
31+
case 5:
32+
continue
33+
default:
34+
t.Fatalf("Unexpected size sample: %d", len(s))
35+
}
36+
37+
if expected != actual {
38+
t.Fatalf("Expected %v, got %v for %dD sample at %v",
39+
expected, actual, len(s)-1, s[:len(s)-1])
40+
}
41+
}
42+
}
43+
44+
// ----------------------------------------------------------------------------
45+
// Helper Functions
46+
// ----------------------------------------------------------------------------
47+
1548
func loadSamples(t *testing.T) [][]float64 {
1649
t.Helper()
1750

@@ -25,6 +58,7 @@ func loadSamples(t *testing.T) [][]float64 {
2558
defer f.Close()
2659

2760
dec := json.NewDecoder(f)
61+
2862
for {
2963
var sample []float64
3064

@@ -42,30 +76,3 @@ func loadSamples(t *testing.T) [][]float64 {
4276

4377
return samples
4478
}
45-
46-
func TestSamplesMatch(t *testing.T) {
47-
samples := loadSamples(t)
48-
49-
n := opensimplex.New(0)
50-
51-
for _, s := range samples {
52-
var expected, actual float64
53-
switch len(s) {
54-
case 3:
55-
expected = s[2]
56-
actual = n.Eval64(s[0], s[1])
57-
case 4:
58-
expected = s[3]
59-
actual = n.Eval64(s[0], s[1], s[2])
60-
case 5:
61-
continue
62-
default:
63-
t.Fatalf("Unexpected size sample: %d", len(s))
64-
}
65-
66-
if expected != actual {
67-
t.Fatalf("Expected %v, got %v for %dD sample at %v",
68-
expected, actual, len(s)-1, s[:len(s)-1])
69-
}
70-
}
71-
}

0 commit comments

Comments
 (0)