Exemple #1
0
// Line draws a line between two points. It's 1 pixel thick.
func (w *Window) Line(v1, v2 *geom.Vec2, r, g, b byte) {
	// Always draw from left to right (x1 <= x2)
	if v1[0] > v2[0] {
		v1, v2 = v2, v1
	}
	dx := v2[0] - v1[0]
	dy := v2[1] - v1[1]
	var steps int
	if tmath.Absi(dx) > tmath.Absi(dy) {
		steps = dx
	} else {
		steps = tmath.Absi(dy)
	}
	xinc := float64(dx) / float64(steps)
	yinc := float64(dy) / float64(steps)
	x := float64(v1[0])
	y := float64(v1[1])
	for s := 0; s <= steps; s++ {
		w.Setxy(tmath.Round(x), tmath.Round(y), r, g, b)
		x += xinc
		y += yinc
	}
}
Exemple #2
0
// DrawLine draws a 1 pixel thick line between the (x1,y1) and (x2,y2) with the
// given color.
func (img *Image) DrawLine(x1, y1, x2, y2 int, c color.Color) {
	r := img.Rgba
	// Always draw from left to right (x1 <= x2)
	if x1 > x2 {
		x1, y1, x2, y2 = x2, y2, x1, y1
	}
	dx := x2 - x1
	dy := y2 - y1
	var steps int
	if tmath.Absi(dx) > tmath.Absi(dy) {
		steps = tmath.Absi(dx)
	} else {
		steps = tmath.Absi(dy)
	}
	xinc := float64(dx) / float64(steps)
	yinc := float64(dy) / float64(steps)
	x := float64(x1)
	y := float64(y1)
	for s := 0; s <= steps; s++ {
		r.Set(tmath.Round(x), tmath.Round(y), c)
		x += xinc
		y += yinc
	}
}