Jump to content

Hilbert curve: Difference between revisions

Added Go
m (→‎{{header|Ring}}: Remove vanity tags)
(Added Go)
Line 80:
{{output}}
<pre>Same as Kotlin entry.</pre>
 
=={{header|Go}}==
{{libheader|Go Graphics}}
<br>
The following is based on the recursive algorithm and C code in [https://www.researchgate.net/profile/Christoph_Schierz2/publication/228982573_A_recursive_algorithm_for_the_generation_of_space-filling_curves/links/0912f505c2f419782c000000/A-recursive-algorithm-for-the-generation-of-space-filling-curves.pdf this paper]. The image produced is similar to the one linked to in the zkl example.
<lang go>package main
 
import "github.com/fogleman/gg"
 
var points []gg.Point
 
const width = 64
 
func hilbert(x, y, lg, i1, i2 int) {
if lg == 1 {
px := float64(width-x) * 10
py := float64(width-y) * 10
points = append(points, gg.Point{px, py})
return
}
lg >>= 1
hilbert(x+i1*lg, y+i1*lg, lg, i1, 1-i2)
hilbert(x+i2*lg, y+(1-i2)*lg, lg, i1, i2)
hilbert(x+(1-i1)*lg, y+(1-i1)*lg, lg, i1, i2)
hilbert(x+(1-i2)*lg, y+i2*lg, lg, 1-i1, i2)
}
 
func main() {
hilbert(0, 0, width, 0, 0)
dc := gg.NewContext(650, 650)
dc.SetRGB(0, 0, 0) // Black background
dc.Clear()
for _, p := range points {
dc.LineTo(p.X, p.Y)
}
dc.SetHexColor("#90EE90") // Light green curve
dc.SetLineWidth(1)
dc.Stroke()
dc.SavePNG("hilbert.png")
}</lang>
 
=={{header|Kotlin}}==
9,492

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.