feat: x bit polygons

This commit is contained in:
2026-02-26 18:22:28 +01:00
Unverified
parent 33ca895f96
commit 457547d96a
2 changed files with 141 additions and 6 deletions

147
main.go
View File

@@ -3,11 +3,150 @@ package main
import ( import (
"crypto/sha1" "crypto/sha1"
"fmt" "fmt"
"slices"
"strconv" "strconv"
"github.com/fogleman/gg" "github.com/fogleman/gg"
) )
func main() {
dc := gg.NewContext(800, 800)
XBitPolygons(dc, "Jaybee18", 8, 8)
dc.SavePNG("out.png")
}
func XBitPolygons(dc *gg.Context, username string, columns int, rows int) {
hash := hash(username)
dc.SetRGB255(255, 255, 255)
dc.DrawRectangle(0, 0, 800, 800)
dc.Fill()
colorString := hash[0:6]
r, g, b := rgbFromHex(colorString)
dc.SetRGB255(r, g, b)
enabledVertices := [][]bool{}
for _, character := range hash[6:6+rows+1] {
integer, _ := strconv.ParseUint(string(character), 16, columns/2+1)
row := []bool{}
for j := range columns/2+1 {
on := hasBit(integer, j)
row = append(row, on)
}
enabledVertices = append(enabledVertices, row)
}
w := 800 / columns
h := 800 / rows
for i := range rows {
for j := range columns/2 {
topLeft, tlx, tly := enabledVertices[i][j], float64(j*w), float64(i*h)
topRight, trx, try := enabledVertices[i][j+1], float64(j*w+w), float64(i*h)
bottomRight, brx, bry := enabledVertices[i+1][j+1], float64(j*w+w), float64(i*h+h)
bottomLeft, blx, bly := enabledVertices[i+1][j], float64(j*w), float64(i*h+h)
mx, my := tlx + float64(w/2), tly + float64(h/2)
count := 0
if topLeft {count++}
if topRight {count++}
if bottomRight {count++}
if bottomLeft {count++}
if topLeft && topRight {
dc.NewSubPath()
dc.MoveTo(mx, my)
dc.LineTo(tlx, tly)
dc.LineTo(trx, try)
dc.ClosePath()
}
if topRight && bottomRight {
dc.NewSubPath()
dc.MoveTo(mx, my)
dc.LineTo(trx, try)
dc.LineTo(brx, bry)
dc.ClosePath()
}
if bottomRight && bottomLeft {
dc.NewSubPath()
dc.MoveTo(mx, my)
dc.LineTo(brx, bry)
dc.LineTo(blx, bly)
dc.ClosePath()
}
if bottomLeft && topLeft {
dc.NewSubPath()
dc.MoveTo(mx, my)
dc.LineTo(blx, bly)
dc.LineTo(tlx, tly)
dc.ClosePath()
}
dc.Fill()
}
}
// Reverse
for i := range len(enabledVertices) {
slices.Reverse(enabledVertices[i])
}
// Mirror left side
for i := range rows {
for j := range columns/2 {
topLeft, tlx, tly := enabledVertices[i][j], float64(w*(columns/2)+j*w), float64(i*h)
topRight, trx, try := enabledVertices[i][j+1], float64(w*(columns/2)+j*w+w), float64(i*h)
bottomRight, brx, bry := enabledVertices[i+1][j+1], float64(w*(columns/2)+j*w+w), float64(i*h+h)
bottomLeft, blx, bly := enabledVertices[i+1][j], float64(w*(columns/2)+j*w), float64(i*h+h)
mx, my := tlx+float64(w/2), tly+float64(h/2)
count := 0
if topLeft {count++}
if topRight {count++}
if bottomRight {count++}
if bottomLeft {count++}
if topLeft && topRight {
dc.NewSubPath()
dc.MoveTo(mx, my)
dc.LineTo(tlx, tly)
dc.LineTo(trx, try)
dc.ClosePath()
}
if topRight && bottomRight {
dc.NewSubPath()
dc.MoveTo(mx, my)
dc.LineTo(trx, try)
dc.LineTo(brx, bry)
dc.ClosePath()
}
if bottomRight && bottomLeft {
dc.NewSubPath()
dc.MoveTo(mx, my)
dc.LineTo(brx, bry)
dc.LineTo(blx, bly)
dc.ClosePath()
}
if bottomLeft && topLeft {
dc.NewSubPath()
dc.MoveTo(mx, my)
dc.LineTo(blx, bly)
dc.LineTo(tlx, tly)
dc.ClosePath()
}
dc.Fill()
}
}
}
/* /*
8x8 square of 100x100 px squares 8x8 square of 100x100 px squares
@@ -27,10 +166,8 @@ c = 1 1 0 0
... ...
b = 1 0 1 1 b = 1 0 1 1
*/ */
func main() { func FourBitSquares(dc *gg.Context, username string) {
dc := gg.NewContext(800, 800) hash := hash(username)
hash := hash("Jaybee18")
colorString := hash[0:6] colorString := hash[0:6]
r, g, b := rgbFromHex(colorString) r, g, b := rgbFromHex(colorString)
@@ -51,8 +188,6 @@ func main() {
dc.Fill() dc.Fill()
} }
} }
dc.SavePNG("out.png")
} }
func rgbFromHex(hex string) (int, int, int) { func rgbFromHex(hex string) (int, int, int) {

BIN
out.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 13 KiB