tins2021/scenery.go

95 lines
2.5 KiB
Go
Raw Normal View History

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 * float64(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 {
var polygon geom.PolygonF
pt := func(rotation float64) geom.PointF {
a := .5*geom.Pi + 2*geom.Pi*rotation
return geom.PtF(geom.Cos(a), geom.Sin(a))
}
const sides = 6
for side := 0; side < 6; side++ {
polygon.Points = append(polygon.Points,
pt(float64(side)/float64(sides)),
)
}
return polygon
}
func CreateStar(sides int) geom.PolygonF {
var polygon geom.PolygonF
pt := func(rotation float64) geom.PointF {
a := .5*geom.Pi + 2*geom.Pi*rotation
return geom.PtF(geom.Cos(a), geom.Sin(a))
}
for side := 0; side < sides; side++ {
polygon.Points = append(polygon.Points,
pt(float64(side)/float64(sides)),
pt((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
}