示例#1
0
// функция грубой отрисовки части дуги окружности
// по алгоритму Ulrich Mierendorf (imageSmoothArc_optimized)
func smootharc(p *gd.Image, cx, cy, a, b float64, fillColor gd.Color, start, stop, seg float64) {
	color := p.ColorsForIndex(fillColor)
	var xp, yp, xa, ya float64

	switch seg {
	case 0:
		xp, yp, xa, ya = 1, -1, 1, -1
	case 1:
		xp, yp, xa, ya = -1, -1, 0, -1
	case 2:
		xp, yp, xa, ya = -1, 1, 0, 0
	case 3:
		xp, yp, xa, ya = 1, 1, 1, 0
	}

	for x := float64(0); x <= a; x++ {
		y := b * math.Sqrt(1-(x*x)/(a*a))
		error := y - float64(int(y))
		y = float64(int(y))

		alpha := int(127 - float64(127-color["alpha"])*error)
		diffColor := p.ColorExactAlpha(color["red"], color["green"], color["blue"], alpha)

		xx := int(cx + xp*x + xa)

		p.SetPixel(xx, int(cy+yp*(y+1)+ya), diffColor)
		p.Line(xx, int(cy+yp*y+ya), xx, int(cy+ya), fillColor)
	}

	for y := float64(0); y < b; y++ {
		x := a * math.Sqrt(1-(y*y)/(b*b))
		error := x - float64(int(x))
		x = float64(int(x))

		alpha := int(127 - float64(127-color["alpha"])*error)
		diffColor := p.ColorExactAlpha(color["red"], color["green"], color["blue"], alpha)
		p.SetPixel(int(cx+xp*(x+1)+xa), int(cy+yp*y+ya), diffColor)
	}
}