//输出正弦函数的sin值 打印出一张sin图标来
// 思路:
// 1.设置一张设置图片的背景色为白色
// 2.在图片上根据sin轨迹进行打点
// 3.把图片文件打印出来
package main
import (
"image"
"image/color"
"image/png"
"log"
"math"
"os"
)
func main() {
const size = 300 //设置一张图片规格常量 大小为300
//image 包下的NewGray方法 传入了image包下的Rect函数 返回一个灰度图句柄
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})
}
}
// 绘制正弦函数的轨迹
for x := 0; x < size; x++ {
s := float64(x) * 2 * math.Pi / size
y := size/2 - math.Sin(s)*size/2
pic.SetGray(x, int(y), color.Gray{0})
pic.SetGray(x, int(y), color.Gray{0})
//写入图片文件
file, err := os.Create("sin.png")
if err != nil {
log.Fatal(err)
}
png.Encode(file, pic)
file.Close()
}
}
知识点:
创建灰度图:image.NewGray(image.Rect(0,0,size,size))
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交