Skip to content

Commit 0c8632e

Browse files
author
Elli Beres
committed
Add normalized function
1 parent d17730f commit 0c8632e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

opensimplex2d.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,28 @@ func (n *noise) Noise2D(x, y float64) float64 {
144144
return noisyValue
145145
}
146146

147+
func (n *noise) NormalizedNoise2D(x, y float64) float64 {
148+
noisy := n.Noise2D(x, y)
149+
150+
normalizedNoisy := (noisy + NormMinConstant2) / (2.0 * NormMinConstant2)
151+
152+
return clamp(normalizedNoisy, 0.0, 1.0)
153+
}
154+
147155
func extrapolate2(perm []int16, xsb, ysb, dx, dy float64) float64 {
148156
index := perm[(int32(perm[int32(xsb)&0xff])+int32(ysb))&0xff] & 0x0e
149157
g1 := float64(Gradients2[index])
150158
g2 := float64(Gradients2[index+1])
151159

152160
return g1*dx + g2*dy
153161
}
162+
163+
func clamp(x, min, max float64) float64 {
164+
if x > max {
165+
return max
166+
}
167+
if x < min {
168+
return min
169+
}
170+
return x
171+
}

opensimplex2d_constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const (
44
StretchConstant2 float64 = -0.21132486540518708
55
SquishConstant2 float64 = 0.3660254037844386
66
NormConstant2 float64 = 47
7+
NormMinConstant2 float64 = 0.8659203878240322
78
)
89

910
var Gradients2 [16]int8 = [16]int8{

0 commit comments

Comments
 (0)