tins2021/polygons.go

91 lines
2.3 KiB
Go

package tins2021
import (
"image"
"github.com/llgcode/draw2d/draw2dimg"
"opslag.de/schobers/geom"
)
const TextureSize = 128
func CreateHeart() geom.PolygonF {
var polygon geom.PolygonF
const segments = 100
for segment := 0; segment < 100; segment++ {
t := 2 * geom.Pi * Percentage(segment, segments)
st := geom.Sin(t)
polygon.Points = append(polygon.Points, geom.PtF(
16*st*st*st,
13*geom.Cos(t)-5*geom.Cos(2*t)-2*geom.Cos(3*t)-geom.Cos(4*t)))
}
return polygon.Reverse().Mul(1. / 16)
}
func CreateHexagon() geom.PolygonF {
const sides = 6
return CreateRegularPolygon(6)
}
func CreateRegularPolygon(sides int) geom.PolygonF {
var polygon geom.PolygonF
for side := 0; side < sides; side++ {
polygon.Points = append(polygon.Points,
PolarUnity(float64(side)/float64(sides)),
)
}
return polygon
}
func CreateStar(sides int) geom.PolygonF {
var polygon geom.PolygonF
for side := 0; side < sides; side++ {
polygon.Points = append(polygon.Points,
PolarUnity(float64(side)/float64(sides)),
PolarUnity((float64(side)+0.5)/float64(sides)).Mul(.5),
)
}
return polygon
}
func RenderPolygon2D(polygon geom.PolygonF, hexColor string) image.Image {
im := image.NewRGBA(image.Rect(0, 0, TextureSize, TextureSize))
color := mustHexColor(hexColor)
gc := draw2dimg.NewGraphicContext(im)
fillStrokeToGC(gc, color, polygon.Points...)
return im
}
func RenderTriangles2D(triangles []geom.TriangleF, hexColor string) image.Image {
im := image.NewRGBA(image.Rect(0, 0, TextureSize, TextureSize))
color := mustHexColor(hexColor)
gc := draw2dimg.NewGraphicContext(im)
for _, triangle := range triangles {
strokeToGC(gc, color, triangle.Points[:]...)
}
return im
}
func RenderTriangleSides2D(triangles []geom.TriangleF) image.Image {
im := image.NewRGBA(image.Rect(0, 0, TextureSize, TextureSize))
r, g, b := mustHexColor(`#FF0000`), mustHexColor(`#00FF00`), mustHexColor(`#0000FF`)
gc := draw2dimg.NewGraphicContext(im)
gc.SetLineWidth(2)
for _, triangle := range triangles {
inset := triangle.Inset(5)
gc.SetStrokeColor(r)
gc.MoveTo(inset.Points[0].XY())
gc.LineTo(inset.Points[1].XY())
gc.Stroke()
gc.SetStrokeColor(g)
gc.MoveTo(inset.Points[1].XY())
gc.LineTo(inset.Points[2].XY())
gc.Stroke()
gc.SetStrokeColor(b)
gc.MoveTo(inset.Points[2].XY())
gc.LineTo(inset.Points[0].XY())
gc.Stroke()
}
return im
}