-
Notifications
You must be signed in to change notification settings - Fork 9
/
shape.go
166 lines (139 loc) · 2.96 KB
/
shape.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package imgo
import (
"image"
"image/color"
)
// Pixel draws a pixel at given (x, y) coordinate with given color.
func (i *Image) Pixel(x, y int, c color.Color) *Image {
if i.Error != nil {
return i
}
i.image.Set(x, y, c)
return i
}
// Line draws a line from (x1, y1) to (x2, y2) with given color.
// TODO: width is not working yet.
func (i *Image) Line(x1, y1, x2, y2 int, c color.Color, width ...int) *Image {
if i.Error != nil {
return i
}
w := 1
if len(width) != 0 {
w = width[0]
}
if w <= 0 {
return i
}
bg := i.image
dx := x2 - x1
dy := y2 - y1
if dx == 0 && dy == 0 { // Line is a point.
bg.Set(x1, y1, c)
i.image = bg
return i
} else if dx == 0 { // vertical line
if y1 > y2 {
y1, y2 = y2, y1
}
for y := y1; y <= y2; y++ {
bg.Set(x1, y, c)
}
} else if dy == 0 { // horizontal line
if x1 > x2 {
x1, x2 = x2, x1
}
for x := x1; x <= x2; x++ {
bg.Set(x, y1, c)
}
} else { // diagonal line
k := float64(dy) / float64(dx)
if x1 > x2 {
x1, x2 = x2, x1
y1, y2 = y2, y1
}
if -1 < k && k < 1 {
for x := x1; x <= x2; x++ {
y := int(float64(y1) + float64(x-x1)*k)
bg.Set(x, y, c)
}
} else {
ySmaller, yBigger := y1, y2
if y1 > y2 {
ySmaller, yBigger = y2, y1
}
for y := ySmaller; y <= yBigger; y++ {
x := int(float64(x1) + float64(y-y1)/k)
bg.Set(x, y, c)
}
}
}
return i
}
// Circle draws a circle at given center coordinate (x, y) with given radius and color.
func (i *Image) Circle(x, y, radius int, c color.Color) *Image {
if i.Error != nil {
return i
}
if radius <= 0 || radius >= i.width || radius >= i.height {
return i
}
x1 := x - radius
y1 := y - radius
x2 := x + radius
y2 := y + radius
bg := i.image
for x3 := x1; x3 < x2; x3++ {
for y3 := y1; y3 < y2; y3++ {
if (x3-x)*(x3-x)+(y3-y)*(y3-y) <= radius*radius {
bg.Set(x3, y3, c)
}
}
}
return i
}
// Rectangle draws a rectangle at given coordinate (x, y) with given width and height and color.
func (i *Image) Rectangle(x, y, width, height int, c color.Color) *Image {
if i.Error != nil {
return i
}
if width <= 0 || height <= 0 {
return i
}
x1 := x
y1 := y
x2 := x + width
y2 := y + height
bg := i.image
for x3 := x1; x3 < x2; x3++ {
for y3 := y1; y3 < y2; y3++ {
if image.Pt(x3, y3).In(i.image.Bounds()) {
bg.Set(x3, y3, c)
}
}
}
return i
}
// Ellipse draws an ellipse at given center coordinate (x, y) with given width and height and color.
func (i *Image) Ellipse(x, y, width, height int, c color.Color) *Image {
if i.Error != nil {
return i
}
if width <= 0 || height <= 0 {
return i
}
a := float64(width) / 2
b := float64(height) / 2
x1 := x - int(a)
y1 := y - int(b)
x2 := x + int(a)
y2 := y + int(b)
bg := i.image
for x3 := x1; x3 <= x2; x3++ {
for y3 := y1; y3 <= y2; y3++ {
if (float64(x3)-float64(x))*(float64(x3)-float64(x))/a/a+(float64(y3)-float64(y))*(float64(y3)-float64(y))/b/b <= 1.0 {
bg.Set(x3, y3, c)
}
}
}
return i
}