Created
August 30, 2015 21:05
-
-
Save colelawrence/58f5a6c0f10cf37775d6 to your computer and use it in GitHub Desktop.
drawing lines and stuff in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "golang.org/x/tour/pic" | |
| import "math" | |
| func lemp(x1, y1, dx, dy, t float64) (tx, ty float64) { | |
| tx = x1 - t * dx | |
| ty = y1 - t * dy | |
| return | |
| } | |
| func drawLine(canvas [][]uint8, x1, y1, x2, y2 float64) { | |
| dx, dy := x1 - x2, y1 - y2 | |
| length := math.Sqrt(dx * dx + dy * dy) | |
| dt := 1.0 / length | |
| var draw_X, draw_Y float64 | |
| for t := 1.0; t >= 0; t -= dt { | |
| draw_X, draw_Y = lemp(x1, y1, dx, dy, t) | |
| drawDot(canvas, draw_X, draw_Y) | |
| } | |
| } | |
| func drawDot(canvas [][]uint8, x1, y1 float64) { | |
| canvas[int(y1)][int(x1)] += 50 | |
| } | |
| func Pic(dx, dy int) [][]uint8 { | |
| rows := make([][]uint8, dy) | |
| // populate array [][]uint8 | |
| for i := range rows { | |
| rows[i] = make([]uint8, dx) | |
| } | |
| rowslen := len(rows) | |
| colslen := len(rows[0]) | |
| for i:= 10; i < rowslen; i += 5 { | |
| drawLine(rows,float64(i),float64(colslen - 10),20,0) | |
| drawLine(rows,float64(rowslen - 10), float64(i),20,0) | |
| } | |
| return rows | |
| } | |
| func main() { | |
| pic.Show(Pic) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment