在网上看到某人实现了一个简单的clock,发现代码编译不过,略作修改:
1 package main 2 3 import ( 4 "bufio" 5 "image" 6 "image/color" 7 "image/png" 8 "math" 9 "os" 10 "time" 11 ) 12 13 const clock_size = 200 14 const radius = clock_size / 3 15 16 var colour color.RGBA 17 18 func circle(clock *image.RGBA) { 19 for angle := float64(0); angle < 360; angle++ { 20 radian_angle := math.Pi * 2 * angle / 360 21 x := radius*math.Sin(radian_angle) + clock_size/2 22 y := radius*math.Cos(radian_angle) + clock_size/2 23 clock.Set(int(x), int(y), colour) 24 } 25 } 26 func hand(clock *image.RGBA, angle float64, length float64) { 27 radian_angle := math.Pi * 2 * angle 28 x_inc := math.Sin(radian_angle) 29 y_inc := -math.Cos(radian_angle) 30 for i := float64(0); i < length; i++ { 31 x := i*x_inc + clock_size/2 32 y := i*y_inc + clock_size/2 33 clock.Set(int(x), int(y), colour) 34 } 35 } 36 func main() { 37 clock := image.NewRGBA(image.Rect(0,0,clock_size, clock_size)) 38 colour.A = 255 39 circle(clock) 40 colour.R, colour.G, colour.B = 255,0,0 41 time := time.Now() 42 hand(clock, (float64(time.Hour())+float64(time.Minute())/60)/12, radius*0.5) // hour hand 43 colour.R, colour.G, colour.B = 0,255,0 44 hand(clock, (float64(time.Minute())+float64(time.Second())/60)/60, radius*0.6) // minute hand 45 colour.R, colour.G, colour.B = 0,0,255 46 hand(clock, float64(time.Second())/60, radius*0.8) // Second hand 47 out := bufio.NewWriter(os.Stdout) 48 defer out.Flush() 49 png.Encode(out, clock) 50 }
执行时,将结果重定向到.png文件中就会生成png文件
原文:http://www.cnblogs.com/ronghantao/p/3807726.html