| 1 |
package gfx |
| 2 |
|
| 3 |
import ( |
| 4 |
"image" |
| 5 |
"image/color" |
| 6 |
"image/draw" |
| 7 |
"math" |
| 8 |
) |
| 9 |
|
| 10 |
// DrawPolygon filled or as line polygons if the thickness is >= 1. |
| 11 |
func DrawPolygon(dst draw.Image, p Polygon, thickness float64, c color.Color) { |
| 12 |
n := len(p) |
| 13 |
|
| 14 |
if n < 3 { |
| 15 |
return |
| 16 |
} |
| 17 |
|
| 18 |
switch { |
| 19 |
case thickness < 1: |
| 20 |
p.Fill(dst, c) |
| 21 |
default: |
| 22 |
for i := 0; i < n; i++ { |
| 23 |
if i+1 == n { |
| 24 |
polylineFromTo(p[n-1], p[0], thickness).Fill(dst, c) |
| 25 |
} else { |
| 26 |
polylineFromTo(p[i], p[i+1], thickness).Fill(dst, c) |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// DrawPolyline draws a polyline with the given color and thickness. |
| 33 |
func DrawPolyline(dst draw.Image, pl Polyline, thickness float64, c color.Color) { |
| 34 |
for _, p := range pl { |
| 35 |
DrawPolygon(dst, p, thickness, c) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// Polygon is represented by a list of vectors. |
| 40 |
type Polygon []Vec |
| 41 |
|
| 42 |
// Bounds return the bounds of the polygon rectangle. |
| 43 |
func (p Polygon) Bounds() image.Rectangle { |
| 44 |
return p.Rect().Bounds() |
| 45 |
} |
| 46 |
|
| 47 |
// Rect is the polygon rectangle. |
| 48 |
func (p Polygon) Rect() Rect { |
| 49 |
r := R(math.MaxFloat64, math.MaxFloat64, -math.MaxFloat64, -math.MaxFloat64) |
| 50 |
|
| 51 |
for _, u := range p { |
| 52 |
x, y := u.XY() |
| 53 |
|
| 54 |
if x > r.Max.X { |
| 55 |
r.Max.X = x |
| 56 |
} |
| 57 |
|
| 58 |
if y > r.Max.Y { |
| 59 |
r.Max.Y = y |
| 60 |
} |
| 61 |
|
| 62 |
if x < r.Min.X { |
| 63 |
r.Min.X = x |
| 64 |
} |
| 65 |
|
| 66 |
if y < r.Min.Y { |
| 67 |
r.Min.Y = y |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return r |
| 72 |
} |
| 73 |
|
| 74 |
// Project creates a new Polygon with all vertexes projected through the given Matrix. |
| 75 |
func (p Polygon) Project(m Matrix) Polygon { |
| 76 |
pp := make(Polygon, len(p)) |
| 77 |
|
| 78 |
for i, u := range p { |
| 79 |
pp[i] = m.Project(u) |
| 80 |
} |
| 81 |
|
| 82 |
return pp |
| 83 |
} |
| 84 |
|
| 85 |
// EachPixel calls the provided function for each pixel |
| 86 |
// in the polygon rectangle bounds. |
| 87 |
func (p Polygon) EachPixel(m image.Image, fn func(x, y int)) { |
| 88 |
if len(p) < 3 { |
| 89 |
return |
| 90 |
} |
| 91 |
|
| 92 |
b := p.Bounds() |
| 93 |
|
| 94 |
for x := b.Min.X; x < b.Max.X; x++ { |
| 95 |
for y := b.Min.Y; y < b.Max.Y; y++ { |
| 96 |
if IV(x, y).In(p) { |
| 97 |
fn(x, y) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Fill polygon on the image with the given color. |
| 104 |
func (p Polygon) Fill(dst draw.Image, c color.Color) (drawCount int) { |
| 105 |
if len(p) < 3 { |
| 106 |
return |
| 107 |
} |
| 108 |
|
| 109 |
b := p.Bounds() |
| 110 |
|
| 111 |
for x := b.Min.X; x < b.Max.X; x++ { |
| 112 |
for y := b.Min.Y; y < b.Max.Y; y++ { |
| 113 |
if IV(x, y).In(p) { |
| 114 |
Mix(dst, x, y, c) |
| 115 |
drawCount++ |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return drawCount |
| 121 |
} |
| 122 |
|
| 123 |
// Outline draws an outline of the polygon on dst. |
| 124 |
func (p Polygon) Outline(dst draw.Image, thickness float64, c color.Color) { |
| 125 |
for i := 1; i < len(p); i++ { |
| 126 |
DrawLine(dst, p[i-1], p[i], thickness, c) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// In returns true if the vector is inside the given polygon. |
| 131 |
func (u Vec) In(p Polygon) bool { |
| 132 |
if len(p) < 3 { |
| 133 |
return false |
| 134 |
} |
| 135 |
|
| 136 |
a := p[0] |
| 137 |
|
| 138 |
in := rayIntersectsSegment(u, p[len(p)-1], a) |
| 139 |
|
| 140 |
for _, b := range p[1:] { |
| 141 |
if rayIntersectsSegment(u, a, b) { |
| 142 |
in = !in |
| 143 |
} |
| 144 |
|
| 145 |
a = b |
| 146 |
} |
| 147 |
|
| 148 |
return in |
| 149 |
} |
| 150 |
|
| 151 |
// Points are a list of points. |
| 152 |
type Points []image.Point |
| 153 |
|
| 154 |
// Polygon based on the points. |
| 155 |
func (pts Points) Polygon() Polygon { |
| 156 |
var p Polygon |
| 157 |
|
| 158 |
for i := range pts { |
| 159 |
p = append(p, PV(pts[i])) |
| 160 |
} |
| 161 |
|
| 162 |
return p |
| 163 |
} |
| 164 |
|
| 165 |
// Segment intersect expression from |
| 166 |
// https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html |
| 167 |
// |
| 168 |
// Currently the compiler inlines the function by default. |
| 169 |
func rayIntersectsSegment(u, a, b Vec) bool { |
| 170 |
return (a.Y > u.Y) != (b.Y > u.Y) && |
| 171 |
u.X < (b.X-a.X)*(u.Y-a.Y)/(b.Y-a.Y)+a.X |
| 172 |
} |