• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kelindar / simplex / 16867578561

10 Aug 2025 11:29PM UTC coverage: 94.565% (-5.4%) from 100.0%
16867578561

push

github

web-flow
Merge pull request #3 from kelindar/grads

Refactor noise computation and update benchmark results in README

20 of 20 new or added lines in 1 file covered. (100.0%)

5 existing lines in 1 file now uncovered.

87 of 92 relevant lines covered (94.57%)

205655.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.57
/simplex.go
1
package simplex
2

3
const (
4
        f2 = 0.36602542 // float32(0.5 * (math.Sqrt(3) - 1))
5
        g2 = 0.21132487 // float32((3 - math.Sqrt(3)) / 6)
6
)
7

8
var (
9
        perm [512]uint8
10
        grad [512][2]float32
11
)
12

13
var table = []uint8{151, 160, 137, 91, 90, 15,
14
        131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
15
        190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
16
        88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
17
        77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
18
        102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
19
        135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123,
20
        5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
21
        223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
22
        129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
23
        251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
24
        49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
25
        138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180}
26

27
func init() {
1✔
28
        var g2d = [12]uint16{
1✔
29
                0x0101, // [+1, +1]
1✔
30
                0xff01, // [-1, +1]
1✔
31
                0x01ff, // [+1, -1]
1✔
32
                0xffff, // [-1, -1]
1✔
33
                0x0100, // [+1, +0]
1✔
34
                0xff00, // [-1, +0]
1✔
35
                0x0100, // [+1, +0]
1✔
36
                0xff00, // [-1, +0]
1✔
37
                0x0001, // [+0, +1]
1✔
38
                0x00ff, // [+0, -1]
1✔
39
                0x0001, // [+0, +1]
1✔
40
                0x00ff, // [+0, -1]
1✔
41
        }
1✔
42

1✔
43
        for i := 0; i < 512; i++ {
513✔
44
                perm[i] = table[i&255]
512✔
45
                idx := g2d[perm[i]%12]
512✔
46
                gx := int8(idx >> 8)
512✔
47
                gy := int8(idx)
512✔
48
                grad[i] = [2]float32{float32(gx), float32(gy)}
512✔
49
        }
512✔
50
}
51

52
// Noise2 computes a two dimensional simplex noise
53
// Public Domain: https://weber.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
54
// Reference: https://mrl.cs.nyu.edu/~perlin/noise/
55
func Noise2(x, y float32) float32 {
250,000✔
56

250,000✔
57
        // Skew the input space to determine which simplex cell we're in
250,000✔
58
        s := (x + y) * f2
250,000✔
59
        i := floor(x + s)
250,000✔
60
        j := floor(y + s)
250,000✔
61

250,000✔
62
        // Unskew the cell origin back to (x,y) space
250,000✔
63
        t := float32(i+j) * g2
250,000✔
64
        x0 := x - (float32(i) - t)
250,000✔
65
        y0 := y - (float32(j) - t)
250,000✔
66

250,000✔
67
        // For the 2D case, the simplex shape is an equilateral triangle.
250,000✔
68
        // Determine which simplex we are in
250,000✔
69
        i1, j1 := float32(0), float32(1) // upper triangle
250,000✔
70
        if x0 > y0 {                     // lower triangle
372,893✔
71
                i1 = 1
122,893✔
72
                j1 = 0
122,893✔
73
        }
122,893✔
74

75
        // Offsets for middle corner in (x,y) unskewed coords
76
        x1 := x0 - i1 + g2
250,000✔
77
        y1 := y0 - j1 + g2
250,000✔
78

250,000✔
79
        // Offsets for middle corner in (x,y) unskewed coords
250,000✔
80
        const g = 2 * g2
250,000✔
81
        x2 := x0 - 1 + g
250,000✔
82
        y2 := y0 - 1 + g
250,000✔
83

250,000✔
84
        // Work out the hashed gradient indices of the three simplex corners
250,000✔
85
        ii := int(i & 255)
250,000✔
86
        jj := int(j & 255)
250,000✔
87

250,000✔
88
        // Use slice windows
250,000✔
89
        pp := perm[jj:]
250,000✔
90
        gg := grad[ii:]
250,000✔
91
        p0 := int(pp[0])
250,000✔
92
        p1 := int(pp[int(j1)])
250,000✔
93
        p2 := int(pp[1])
250,000✔
94
        g0 := gg[p0]
250,000✔
95
        g1 := gg[int(i1)+p1]
250,000✔
96
        g2 := gg[1+p2]
250,000✔
97

250,000✔
98
        // Calculate the contribution from the three corners
250,000✔
99
        n := float32(0.0)
250,000✔
100
        if t := 0.5 - x0*x0 - y0*y0; t > 0 {
476,854✔
101
                n += pow4(t) * (g0[0]*x0 + g0[1]*y0)
226,854✔
102
        }
226,854✔
103
        if t := 0.5 - x1*x1 - y1*y1; t > 0 {
474,558✔
104
                n += pow4(t) * (g1[0]*x1 + g1[1]*y1)
224,558✔
105
        }
224,558✔
106
        if t := 0.5 - x2*x2 - y2*y2; t > 0 {
476,730✔
107
                n += pow4(t) * (g2[0]*x2 + g2[1]*y2)
226,730✔
108
        }
226,730✔
109

110
        // Add contributions from each corner to get the final noise value.
111
        // The result is scaled to return values in the interval [-1,1].
112
        return 70.0 * n
250,000✔
113
}
114

115
// pow4 lifts the value to the power of 4
116
func pow4(v float32) float32 {
678,142✔
117
        v *= v
678,142✔
118
        v *= v
678,142✔
119
        return v
678,142✔
120
}
678,142✔
121

122
// dot2D computes dot product with the gradient
UNCOV
123
func dot2D(g uint16, x, y float32) float32 {
×
UNCOV
124
        gx := float32(int8(g >> 8))
×
UNCOV
125
        gy := float32(int8(g))
×
UNCOV
126
        return gx*x + gy*y
×
UNCOV
127
}
×
128

129
// floor floors the floating-point value to an integer
130
func floor(x float32) int {
500,003✔
131
        v := int(x)
500,003✔
132
        if x < float32(v) {
500,004✔
133
                return v - 1
1✔
134
        }
1✔
135
        return v
500,002✔
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc