go Language Series - output sine function

The experiment used standard libraries and packages

Understanding between the library and the package can be likened to: a database table and database thereof

Library name effect
image Access and generate common graphics formats
log Logging Library
math Math Library
the Platform-independent operating system platforms packaging operation

View library and help documentation package:

  • go doc library / package
  • https://golang.google.cn/

Test all the code

main Package 
Import ( 
    " Image " 
    " Image / Color " 
    " Image / PNG " 
    " log " 
    " Math " 
    " OS " 
) 
FUNC main () { 
    // Image size 
    const size = 300 
    // The given size to create grayscale 
    PIC: = image.NewGray (image.Rect ( 0 , 0 , size, size))
     // iterate through each pixel 
    for X: = 0 ; X <size; X ++ {
         for Y: = 0 ; Y <size; Y ++{
             // filled with white 
            pic.SetGray (x, Y, color.Gray { 255 }) 
        } 
    } 
    // generated from the pixel 0 to the maximum x coordinate 
    for x: = 0 ; x <size; x ++ {
         // make a sin values range between ~ 2Pi 0 
        S: float64 = (X) * 2 * Math.PI / size
         // amplitude of sin is half a pixel. Half of the pixels shifted down and inverted 
        Y: size = / 2 - math.Sin (S) * size / 2 
        // Draw sin trajectory black 
        pic.SetGray (X, int (Y), color.Gray { 0 }) 
    } 
    // create a file
    File, ERR: = os.Create ( " sin.png " )
     IF ! ERR = nil { 
        log.Fatal (ERR) 
    } 
    // write data to a file using the png format 
    png.Encode (File, PIC) // the image information written to the file
     // close file 
    File.close () 
}

Screenshot results:

Detailed experiments:

Set background color picture

 1 // 图片大小
 2 const size = 300
 3 // 根据给定大小创建灰度图
 4 pic := image.NewGray(image.Rect(0, 0, size, size))
 5 // 遍历每个像素
 6 for x := 0; x < size; x++ {
 7     for y := 0; y < size; y++ {
 8         // 填充为白色
 9         pic.SetGray(x, y, color.Gray{255})
10     }
11 }

代码说明:

  • 第 2 行,声明一个 size 常量,值为 300。
  • 第 5 行,使用 image 包的 NewGray() 函数创建一个图片对象,使用区域由 image.Rect 结构提供。image.Rect 描述一个方形的两个定位点 (x1,y1) 和 (x2,y2)。image.Rect(0,0,size,size) 表示使用完整灰度图像素,尺寸为宽 300,长 300。
  • 第 8 行和第 9 行,遍历灰度图的所有像素。
  • 第 11 行,将每一个像素的灰度设为 255,也就是白色

注:灰度图是一种常见的图片格式,一般情况下颜色由 8 位组成,灰度范围为 0~255,0 表示黑色,255 表示白色。初始化好的灰度图对象内存区域默认值都是 0,对应全是黑色,考虑到显示效果和习惯,将所有像素设置为 255,也就是白色。

绘制正弦函数轨迹

正弦函数是一个周期函数。定义域是实数集,值域范围是 [-1, 1]。用编程的通俗易懂的话来说就是:math.Sin 函数的参数支持任意浮点数范围,函数返回值的范围总是在 -1~1之间(两端包含)。
要将正弦函数放在图片上需要考虑以下一些因素:

  • math.Sin 的返回值在 -1~1 之间。需要考虑将正弦的输出幅度变大,可以将 math.Sin 的返回值乘以一个常量进行放大。
  • 图片的坐标系原点在左上角,而 math.Sin 基于笛卡尔坐标系原点在左下角。需要对图像进行上下翻转和平移。

将这些处理逻辑汇总为代码:

1 // 从0到最大像素生成x坐标
2 for x := 0; x < size; x++ {
3     // 让sin的值的范围在0~2Pi之间
4     s := float64(x) * 2 * math.Pi / size
5     // sin的幅度为一半的像素。向下偏移一半像素并翻转
6     y := size/2 - math.Sin(s)*size/2
7     // 用黑色绘制sin轨迹
8     pic.SetGray(x, int(y), color.Gray{0})
9 }

代码说明:

  • 第 2 行,生成 0 到 size(300)的 x 坐标轴。
  • 第 4 行,计算 math.Sin 的定义域,这段代码等效为:
    rate := x / size
    s := rate * 2 * math.Pi

     x 的范围是 0 到 size,因此除以 size 后,rate 的范围是 0~1 之间,再乘以 2π 后,s 的范围刚好是 0~2π 之间。float64(x) 表示将整型的 x 变量转换为 float64 类型,之后运算的所有表达式将以 float64 类型进行。

  • 第6行中,math.Sin(s)*size/2 表示将正弦函数的返回值幅度从 1 扩大到二分之一的 size。负号表示将正弦函数图形以图形中心上下翻转。叠加 size/2 表示将图形在 y 轴上向下偏移二分之一的 size(图片坐标系的 y 向下)。
  •  第8行将计算好的 x 轴和 y 轴数据,以灰度为 0(黑色)使用 SetGray() 方法填充到像素中。    

写入图片的正弦函数图像如下图所示:

写入图片文件

内存中的正弦函数图形是不可见的,我们选用 PNG 格式将图形输出为文件。Go 语言提供了文件创建函数和 PNG 格式写入函数,代码如下:

1 // 创建文件
2 file, err := os.Create("sin.png")
3 if err != nil {
4     log.Fatal(err)
5 }
6 // 使用PNG格式将数据写入文件
7 png.Encode(file, pic)  //将image信息写入文件中
8 // 关闭文件
9 file.Close()

代码说明:

  • 第 2 行,创建 sin.png 的文件。
  • 第 3行,如果创建文件失败,返回错误,打印错误并终止。
  • 第 7 行,使用 PNG 包,将图形对象写入文件中。
  • 第 9 行,关闭文件。

Guess you like

Origin www.cnblogs.com/zisefeizhu/p/11183089.html