golang 實現clock

來源:互聯網
上載者:User

標籤:style   class   blog   code   color   檔案   

在網上看到某人實現了一個簡單的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.RGBA17 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/222         y := radius*math.Cos(radian_angle) + clock_size/223         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 * angle28     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/232         y := i*y_inc + clock_size/233         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,041     time := time.Now()42     hand(clock, (float64(time.Hour())+float64(time.Minute())/60)/12, radius*0.5)   // hour hand43     colour.R, colour.G, colour.B = 0,255,044     hand(clock, (float64(time.Minute())+float64(time.Second())/60)/60, radius*0.6) // minute hand45     colour.R, colour.G, colour.B = 0,0,25546     hand(clock, float64(time.Second())/60, radius*0.8) // Second hand47     out := bufio.NewWriter(os.Stdout)48     defer out.Flush()49     png.Encode(out, clock)50 }

執行時,將結果重新導向到.png檔案中就會產生png檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.