设置背景色
//图片的大小
const size = 300
//根据给定的大小创建灰度图
pic := image.NewGray(image.Rect(0, 0, size, size))
//遍历每个像素
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
//填充为白色
pic.SetGray(x, y, color.Gray{255})
}
}
绘制正弦函数轨迹
//从0到最大像素生成x坐标
for x := 0; x < size; x++ {
//让sin的值的范围在0-2Pi之间
s := float64(x) * 2 * math.Pi / size
//sin的幅度为一半的像素。向下偏移一半像素并翻转
y := size/2 - math.Sin(s)*size/2
//用黑色绘制sin轨迹
pic.SetGray(x, int(y), color.Gray{0})
}
写入图片文件
//创建文件
file, err := os.Create("sin.png")
if err != nil {
log.Fatal(err)
}
//使用png格式将数据写入文件
png.Encode(file, pic)
//关闭文件
file.Close()
完整代码
package main
import (
"image"
"image/color"
"image/png"
"log"
"math"
"os"
)
func main() {
//图片的大小
const size = 300
//根据给定的大小创建灰度图
pic := image.NewGray(image.Rect(0, 0, size, size))
//遍历每个像素
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
//填充为白色
pic.SetGray(x, y, color.Gray{255})
}
}
//从0到最大像素生成x坐标
for x := 0; x < size; x++ {
//让sin的值的范围在0-2Pi之间
s := float64(x) * 2 * math.Pi / size
//sin的幅度为一半的像素。向下偏移一半像素并翻转
y := size/2 - math.Sin(s)*size/2
//用黑色绘制sin轨迹
pic.SetGray(x, int(y), color.Gray{0})
}
//创建文件
file, err := os.Create("sin.png")
if err != nil {
log.Fatal(err)
}
//使用png格式将数据写入文件
png.Encode(file, pic)
//关闭文件
file.Close()
}
输出结果:
参考:《GO语言从入门到进阶实战》一书