Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created January 5, 2017 02:49
Show Gist options
  • Save unixpickle/35404fb4aac196531b7ddfa97b764dcf to your computer and use it in GitHub Desktop.
Save unixpickle/35404fb4aac196531b7ddfa97b764dcf to your computer and use it in GitHub Desktop.
RBF Network Diagrams
package main
import (
"image"
"image/color"
"image/png"
"math"
"os"
"strconv"
)
const (
Width = 886
Height = 687
C1X = 181
C1Y = 172
C2X = 570
C2Y = 360
Radius = 140
)
func main() {
diagrams := []image.Image{rbfDiagram(), rbfSqrtDiagram(), invLinDiagram(), invSquareDiagram(),
sharpDiagram()}
for i, dig := range diagrams {
outFile, _ := os.Create("diagram" + strconv.Itoa(i) + ".png")
png.Encode(outFile, dig)
outFile.Close()
}
}
func rbfDiagram() image.Image {
return diagram(func(d1, d2 float64) float64 {
return math.Min(1, (math.Exp(-d1*d1/(Radius*Radius)) + math.Exp(-d2*d2/(Radius*Radius))))
})
}
func rbfSqrtDiagram() image.Image {
return diagram(func(d1, d2 float64) float64 {
return math.Min(1, 1.5*(math.Exp(-d1/Radius)+math.Exp(-d2/Radius)))
})
}
func invLinDiagram() image.Image {
return diagram(func(d1, d2 float64) float64 {
return math.Min(1, 0.5*(Radius/d1+Radius/d2))
})
}
func invSquareDiagram() image.Image {
return diagram(func(d1, d2 float64) float64 {
return math.Min(1, 0.5*(Radius*Radius/(d1*d1)+Radius*Radius/(d2*d2)))
})
}
func sharpDiagram() image.Image {
return diagram(func(d1, d2 float64) float64 {
if d1 < Radius || d2 < Radius {
return 1
}
return 0
})
}
func diagram(intensityFunc func(d1, d2 float64) float64) image.Image {
res := image.NewRGBA(image.Rect(0, 0, Width, Height))
for y := 0; y < Height; y++ {
for x := 0; x < Width; x++ {
d1 := dist(C1X, C1Y, x, y)
d2 := dist(C2X, C2Y, x, y)
intensity := intensityFunc(d1, d2)
color := color.RGBA{
R: uint8(intensity * 0xcc),
G: uint8((1 - intensity) * 0xcc),
B: 0x10,
A: 0xff,
}
res.Set(x, y, color)
}
}
return res
}
func dist(x1, y1, x2, y2 int) float64 {
return math.Sqrt(math.Pow(float64(x1-x2), 2) + math.Pow(float64(y1-y2), 2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment