From 5cf764db2d60de8b2840f533cf9089ea14f0a652 Mon Sep 17 00:00:00 2001 From: potassium Date: Sun, 26 Jan 2025 06:48:03 +0300 Subject: [PATCH] it works :) --- chatgpt.go | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++ go.sum | 4 ++ readme | 2 + 4 files changed, 158 insertions(+) create mode 100644 chatgpt.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 readme diff --git a/chatgpt.go b/chatgpt.go new file mode 100644 index 0000000..edc2ad0 --- /dev/null +++ b/chatgpt.go @@ -0,0 +1,147 @@ +package main + +import ( + "image" + "image/color" + "image/draw" + "math" + + "git.niplace.ru/XoxJlopeZi4BB/dev/fb" +) + +var Width, Height = fb.Resolution() +const ( + cubeSize = 50 + Zoom = 1000.0 + FOV = 350 + frameCount = 120 + rotationStep = math.Pi / 60 +) + +type Point3D struct { + X, Y, Z float64 +} + +type Point2D struct { + X, Y int +} + +func rotateX(p Point3D, angle float64) Point3D { + cosA := math.Cos(angle) + sinA := math.Sin(angle) + return Point3D{ + X: p.X, + Y: p.Y*cosA - p.Z*sinA, + Z: p.Y*sinA + p.Z*cosA, + } +} + +func rotateY(p Point3D, angle float64) Point3D { + cosA := math.Cos(angle) + sinA := math.Sin(angle) + return Point3D{ + X: p.X*cosA + p.Z*sinA, + Y: p.Y, + Z: -p.X*sinA + p.Z*cosA, + } +} + +func project(p Point3D) Point2D { + scale := Zoom / (p.Z + FOV) + return Point2D{ + X: int(p.X*scale + float64(Width)/2), + Y: int(p.Y*scale + float64(Height)/2), + } +} + +func drawLine(img *image.Paletted, p1, p2 Point2D, col color.Color) { + dx := abs(p2.X - p1.X) + dy := abs(p2.Y - p1.Y) + sx := -1 + if p1.X < p2.X { + sx = 1 + } + sy := -1 + if p1.Y < p2.Y { + sy = 1 + } + err := dx - dy + + for { + img.Set(p1.X, p1.Y, col) + if p1.X == p2.X && p1.Y == p2.Y { + break + } + e2 := 2 * err + if e2 > -dy { + err -= dy + p1.X += sx + } + if e2 < dx { + err += dx + p1.Y += sy + } + } +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} + +/* +func Map[S, T any](v S, f func(T) T) S { +} +*/ + +func main() { + // Define the vertices of the cube + vertices := []Point3D{ + {-cubeSize, -cubeSize, -cubeSize}, + {cubeSize, -cubeSize, -cubeSize}, + {cubeSize, cubeSize, -cubeSize}, + {-cubeSize, cubeSize, -cubeSize}, + {-cubeSize, -cubeSize, cubeSize}, + {cubeSize, -cubeSize, cubeSize}, + {cubeSize, cubeSize, cubeSize}, + {-cubeSize, cubeSize, cubeSize}, + } + + // Define the edges of the cube + edges := [][2]int{ + {0, 1}, {1, 2}, {2, 3}, {3, 0}, + {4, 5}, {5, 6}, {6, 7}, {7, 4}, + {0, 4}, {1, 5}, {2, 6}, {3, 7}, + } + + // Palette for the GIF + palette := []color.Color{color.Black, color.White} + + // Generate frames + for i := 0; i < frameCount; i++ { + angle := float64(i) * rotationStep + img := image.NewPaletted(image.Rect(0, 0, Width, Height), palette) + draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.Point{}, draw.Src) + + // Rotate and project vertices + projVertices := make([]Point2D, len(vertices)) + for j, v := range vertices { + rotated := rotateY(rotateX(v, angle), angle) + projVertices[j] = project(rotated) + } + + // Draw edges + for _, edge := range edges { + p1 := projVertices[edge[0]] + p2 := projVertices[edge[1]] + drawLine(img, p1, p2, color.White) + } + + err := fb.Draw(img) + if err != nil { + panic(err) + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ac27739 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.niplace.ru/XoxJlopeZi4BB/cube + +go 1.23.4 + +require git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033838-b2021445b668 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1e75d79 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033410-5c05476970ae h1:anDr2+KuivGQ2TnZogDlGxWFU6FTeZIkqcRin/drkjQ= +git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033410-5c05476970ae/go.mod h1:7YjlidjZZr0+dNjfgYgy5hPfdE3pV1bBT5UcNvz5LjY= +git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033838-b2021445b668 h1:Kh9BL+0ia8Lo+lvIsZ3a5pYqzq9ZYwk0+gho7L7BASI= +git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033838-b2021445b668/go.mod h1:7YjlidjZZr0+dNjfgYgy5hPfdE3pV1bBT5UcNvz5LjY= diff --git a/readme b/readme new file mode 100644 index 0000000..32c2458 --- /dev/null +++ b/readme @@ -0,0 +1,2 @@ +example of working with /dev/fb0 device +cube animation is done by chatgpt