Skip to content

Commit a46d2b2

Browse files
committed
Switched to int32 to match the c source
1 parent 390e4e2 commit a46d2b2

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

example_test.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,55 @@ const (
1717
func ExampleNewPerlinRandSource() {
1818
p := perlin.NewPerlinRandSource(alpha, beta, n, rand.NewSource(seed))
1919
for x := 0.; x < 3; x++ {
20-
fmt.Printf("%0.0f\t%0.4f\n", x, p.Noise1D(x/10))
20+
fmt.Printf("%0.0f;%0.4f\n", x, p.Noise1D(x/10))
2121
}
2222
// Output:
23-
// 0 0.0000
24-
// 1 -0.1670
25-
// 2 -0.2011
23+
// 0;0.0000
24+
// 1;-0.0086
25+
// 2;-0.0017
2626
}
2727

2828
func ExamplePerlin_Noise1D() {
2929
p := perlin.NewPerlin(alpha, beta, n, seed)
3030
for x := 0.; x < 3; x++ {
31-
fmt.Printf("%0.0f\t%0.4f\n", x, p.Noise1D(x/10))
31+
fmt.Printf("%0.0f;%0.4f\n", x, p.Noise1D(x/10))
3232
}
3333
// Output:
34-
// 0 0.0000
35-
// 1 -0.1670
36-
// 2 -0.2011
34+
// 0;0.0000
35+
// 1;-0.0086
36+
// 2;-0.0017
3737
}
3838

3939
func ExamplePerlin_Noise2D() {
4040
p := perlin.NewPerlin(alpha, beta, n, seed)
4141
for x := 0.; x < 2; x++ {
4242
for y := 0.; y < 2; y++ {
43-
fmt.Printf("%0.0f\t%0.0f\t%0.4f\n", x, y, p.Noise2D(x/10, y/10))
43+
fmt.Printf("%0.0f;%0.0f;%0.4f\n", x, y, p.Noise2D(x/10, y/10))
4444
}
4545
}
4646
// Output:
47-
// 0 0 0.0000
48-
// 0 1 -0.0060
49-
// 1 0 0.1230
50-
// 1 1 0.0625
47+
// 0;0;0.0000
48+
// 0;1;-0.2002
49+
// 1;0;-0.3389
50+
// 1;1;-0.5045
5151
}
5252

5353
func ExamplePerlin_Noise3D() {
5454
p := perlin.NewPerlin(alpha, beta, n, seed)
5555
for x := 0.; x < 2; x++ {
5656
for y := 0.; y < 2; y++ {
5757
for z := 0.; z < 2; z++ {
58-
fmt.Printf("%0.0f\t%0.0f\t%0.0f\t%0.4f\n", x, y, z, p.Noise3D(x/10, y/10, z/10))
58+
fmt.Printf("%0.0f;%0.0f;%0.0f;%0.4f\n", x, y, z, p.Noise3D(x/10, y/10, z/10))
5959
}
6060
}
6161
}
6262
// Output:
63-
// 0 0 0 0.0000
64-
// 0 0 1 0.1881
65-
// 0 1 0 0.1384
66-
// 0 1 1 0.2507
67-
// 1 0 0 -0.0416
68-
// 1 0 1 0.1232
69-
// 1 1 0 0.0536
70-
// 1 1 1 0.1826
63+
// 0;0;0;0.0000
64+
// 0;0;1;0.2616
65+
// 0;1;0;-0.0755
66+
// 0;1;1;0.2020
67+
// 1;0;0;-0.2138
68+
// 1;0;1;0.0616
69+
// 1;1;0;-0.2208
70+
// 1;1;1;0.0304
7171
}

perlin.go

+27-25
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ const (
2020
type Perlin struct {
2121
alpha float64
2222
beta float64
23-
n int
23+
n int32
2424

25-
p [B + B + 2]int
25+
p [B + B + 2]int32
2626
g3 [B + B + 2][3]float64
2727
g2 [B + B + 2][2]float64
2828
g1 [B + B + 2]float64
@@ -33,7 +33,7 @@ type Perlin struct {
3333
// Typically it is 2, As this approaches 1 the function is noisier.
3434
// "beta" is the harmonic scaling/spacing, typically 2, n is the
3535
// number of iterations and seed is the math.rand seed value to use
36-
func NewPerlin(alpha, beta float64, n int, seed int64) *Perlin {
36+
func NewPerlin(alpha, beta float64, n int32, seed int64) *Perlin {
3737
return NewPerlinRandSource(alpha, beta, n, rand.NewSource(seed))
3838
}
3939

@@ -42,9 +42,9 @@ func NewPerlin(alpha, beta float64, n int, seed int64) *Perlin {
4242
// Typically it is 2, As this approaches 1 the function is noisier.
4343
// "beta" is the harmonic scaling/spacing, typically 2, n is the
4444
// number of iterations and source is source of pseudo-random int64 values
45-
func NewPerlinRandSource(alpha, beta float64, n int, source rand.Source) *Perlin {
45+
func NewPerlinRandSource(alpha, beta float64, n int32, source rand.Source) *Perlin {
4646
var p Perlin
47-
var i, j, k int
47+
var i, j, k int32
4848

4949
p.alpha = alpha
5050
p.beta = beta
@@ -54,23 +54,23 @@ func NewPerlinRandSource(alpha, beta float64, n int, source rand.Source) *Perlin
5454

5555
for i = 0; i < B; i++ {
5656
p.p[i] = i
57-
p.g1[i] = float64((r.Int()%(B+B))-B) / B
57+
p.g1[i] = float64((r.Int31()%(B+B))-B) / B
5858

5959
for j = 0; j < 2; j++ {
60-
p.g2[i][j] = float64((r.Int()%(B+B))-B) / B
60+
p.g2[i][j] = float64((r.Int31()%(B+B))-B) / B
6161
}
6262

6363
normalize2(&p.g2[i])
6464

6565
for j = 0; j < 3; j++ {
66-
p.g3[i][j] = float64((r.Int()%(B+B))-B) / B
66+
p.g3[i][j] = float64((r.Int31()%(B+B))-B) / B
6767
}
6868
normalize3(&p.g3[i])
6969
}
7070

7171
for ; i > 0; i-- {
7272
k = p.p[i]
73-
j = r.Int() % B
73+
j = r.Int31() % B
7474
p.p[i] = p.p[j]
7575
p.p[j] = k
7676
}
@@ -123,9 +123,9 @@ func (p *Perlin) noise1(arg float64) float64 {
123123
vec[0] = arg
124124

125125
t := vec[0] + N
126-
bx0 := int(t) & BM
126+
bx0 := int32(t) & BM
127127
bx1 := (bx0 + 1) & BM
128-
rx0 := t - float64(int(t))
128+
rx0 := t - float64(int32(t))
129129
rx1 := rx0 - 1.
130130

131131
sx := sCurve(rx0)
@@ -137,15 +137,15 @@ func (p *Perlin) noise1(arg float64) float64 {
137137

138138
func (p *Perlin) noise2(vec [2]float64) float64 {
139139
t := vec[0] + N
140-
bx0 := int(t) & BM
140+
bx0 := int32(t) & BM
141141
bx1 := (bx0 + 1) & BM
142-
rx0 := t - float64(int(t))
142+
rx0 := t - float64(int32(t))
143143
rx1 := rx0 - 1.
144144

145145
t = vec[1] + N
146-
by0 := int(t) & BM
146+
by0 := int32(t) & BM
147147
by1 := (by0 + 1) & BM
148-
ry0 := t - float64(int(t))
148+
ry0 := t - float64(int32(t))
149149
ry1 := ry0 - 1.
150150

151151
i := p.p[bx0]
@@ -176,21 +176,21 @@ func (p *Perlin) noise2(vec [2]float64) float64 {
176176

177177
func (p *Perlin) noise3(vec [3]float64) float64 {
178178
t := vec[0] + N
179-
bx0 := int(t) & BM
179+
bx0 := int32(t) & BM
180180
bx1 := (bx0 + 1) & BM
181-
rx0 := t - float64(int(t))
181+
rx0 := t - float64(int32(t))
182182
rx1 := rx0 - 1.
183183

184184
t = vec[1] + N
185-
by0 := int(t) & BM
185+
by0 := int32(t) & BM
186186
by1 := (by0 + 1) & BM
187-
ry0 := t - float64(int(t))
187+
ry0 := t - float64(int32(t))
188188
ry1 := ry0 - 1.
189189

190190
t = vec[2] + N
191-
bz0 := int(t) & BM
191+
bz0 := int32(t) & BM
192192
bz1 := (bz0 + 1) & BM
193-
rz0 := t - float64(int(t))
193+
rz0 := t - float64(int32(t))
194194
rz1 := rz0 - 1.
195195

196196
i := p.p[bx0]
@@ -240,10 +240,10 @@ func (p *Perlin) noise3(vec [3]float64) float64 {
240240
func (p *Perlin) Noise1D(x float64) float64 {
241241
var scale float64 = 1
242242
var sum, val float64
243-
243+
var i int32
244244
px := x
245245

246-
for i := 0; i < p.n; i++ {
246+
for i = 0; i < p.n; i++ {
247247
val = p.noise1(px)
248248
sum += val / scale
249249
scale *= p.alpha
@@ -257,11 +257,12 @@ func (p *Perlin) Noise2D(x, y float64) float64 {
257257
var scale float64 = 1
258258
var sum, val float64
259259
var px [2]float64
260+
var i int32
260261

261262
px[0] = x
262263
px[1] = y
263264

264-
for i := 0; i < p.n; i++ {
265+
for i = 0; i < p.n; i++ {
265266
val = p.noise2(px)
266267
sum += val / scale
267268
scale *= p.alpha
@@ -276,6 +277,7 @@ func (p *Perlin) Noise3D(x, y, z float64) float64 {
276277
var scale float64 = 1
277278
var sum, val float64
278279
var px [3]float64
280+
var i int32
279281

280282
if z < 0.0000 {
281283
return p.Noise2D(x, y)
@@ -285,7 +287,7 @@ func (p *Perlin) Noise3D(x, y, z float64) float64 {
285287
px[1] = y
286288
px[2] = z
287289

288-
for i := 0; i < p.n; i++ {
290+
for i = 0; i < p.n; i++ {
289291
val = p.noise3(px)
290292
sum += val / scale
291293
scale *= p.alpha

0 commit comments

Comments
 (0)