diff --git a/archive/graphics/bezier/bezier.go b/archive/graphics/bezier/bezier.go new file mode 100644 index 0000000..9925180 --- /dev/null +++ b/archive/graphics/bezier/bezier.go @@ -0,0 +1,53 @@ +package bezier + +import ( + "image" + "image/color" + "image/draw" + "math" +) + +// Plot plots a Bézier curve based on the provided control points, which serve +// as weights for the Bernstein cubic polynomial basis functions. +func Plot(p [4]float64) image.Image { + const ( + width, height = 500, 500 + scale = 1.0 / width + ) + dst := image.NewRGBA(image.Rect(0, 0, width, height)) + draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.ZP, draw.Src) + for x := 0; x < width; x++ { + u := scale * float64(x) + y := int((p[0]*b0(u) + p[1]*b1(u) + p[2]*b2(u) + p[3]*b3(u)) / scale) + dst.Set(x, y, color.Black) + } + return dst +} + +// b0 represents the 0th basis function of the Bernstein cubic polynomials. +// +// b0(u) = (1-u)^3 +func b0(u float64) float64 { + return math.Pow(1-u, 3) +} + +// b1 represents the 1st basis function of the Bernstein cubic polynomials. +// +// b1(u) = 3u(1-u)^2 +func b1(u float64) float64 { + return 3 * u * math.Pow(1-u, 2) +} + +// b2 represents the 2nd basis function of the Bernstein cubic polynomials. +// +// b2(u) = 3u^2(1-u) +func b2(u float64) float64 { + return 3 * math.Pow(u, 2) * (1 - u) +} + +// b3 represents the 3rd basis function of the Bernstein cubic polynomials. +// +// b3(u) = u^3 +func b3(u float64) float64 { + return math.Pow(u, 3) +} diff --git a/archive/graphics/cmd/plot/plot.go b/archive/graphics/cmd/plot/plot.go new file mode 100644 index 0000000..0bf3c8b --- /dev/null +++ b/archive/graphics/cmd/plot/plot.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "log" + + "github.com/mewkiz/pkg/imgutil" + "github.com/mewmew/playground/archive/graphics/bezier" +) + +func main() { + for p0 := 0.0; p0 <= 1.0; p0 += 0.1 { + for p1 := 0.0; p1 <= 1.0; p1 += 0.1 { + for p2 := 0.0; p2 <= 1.0; p2 += 0.1 { + for p3 := 0.0; p3 <= 1.0; p3 += 0.1 { + p := [4]float64{p0, p1, p2, p3} + img := bezier.Plot(p) + path := fmt.Sprintf("plot_%.1f_%.1f_%.1f_%.1f.png", p0, p1, p2, p3) + err := imgutil.WriteFile(path, img) + if err != nil { + log.Fatalln(err) + } + } + } + } + } +}