/plot-function-svg

Plot 3D math equation z=f(x, y) with SVG format.

Primary LanguageGo

plot-function-svg

Plot 3D math equation z=f(x, y) with SVG format.

Some codes are referred from https://github.com/adonovan/gopl.io
licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Creative Commons License

APIs

type PlotConfig struct {
	Width       int
	Height      int
	Cells       int
	XYrange     float64
	Xoffset     float64
	Yoffset     float64
	Zscale      float64
	CameraAngle float64
	Color       bool
	RightHand   bool
}

func DefaultPlotConfig() *PlotConfig {
	return &PlotConfig{
		Width:       600,
		Height:      320,
		Cells:       100,
		XYrange:     30.0,
		Xoffset:     0,
		Yoffset:     0,
		Zscale:      0.4,
		CameraAngle: math.Pi / 6,
		Color:       true,
		RightHand:   true,
	}
}

// Draw an SVG to io.Writer. Default w (use nil) is os.Stdout. Default cfg (use nil) is DefaultPlotConfig().
func PlotSVG(f func(x, y float64) float64, w io.Writer, cfg *PlotConfig) 

Right-hand coordinates (default):
    z
     |__ y
x /

Left-hand coordinates:
    z
     |__ x
y /

Examples

Example1:

f := func(x, y float64) float64 {
	r := math.Hypot(x, y)
	return math.Sin(r) / r
}
file, _ := os.Create("example1.svg")
plot.PlotSVG(f, file, nil)

image

Example2:

cfg := &plot.PlotConfig{
	Width:       600,
	Height:      400,
	Cells:       100,
	XYrange:     2,
	Xoffset:     0.4,
	Yoffset:     0.3,
	Zscale:      0.1,
	CameraAngle: math.Pi / 6,
	Color:       true,
	RightHand:   false,
}
f := func(x, y float64) float64 {
	return 2*math.Pow(x, 2) + 0.5*math.Sin(-7*y)
}
file, _ := os.Create("example2.svg")
plot.PlotSVG(f, file, cfg)

image