Skip to content

Commit 016ae6f

Browse files
committed
feat: center the 3D model and improve scaling
1 parent 9ef889f commit 016ae6f

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

internal/app/cube/cube.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"cubectl/internal/logger"
1212
"cubectl/internal/terminal"
13+
14+
"github.com/nsf/termbox-go"
1315
)
1416

1517
type Options struct {
@@ -19,7 +21,6 @@ type Options struct {
1921

2022
func Render(ctx context.Context, opts Options) error {
2123
output := opts.Output
22-
w := opts.Watch
2324

2425
if output == "" {
2526
output = "wireframe" // default
@@ -89,8 +90,28 @@ func Render(ctx context.Context, opts Options) error {
8990
}
9091
}
9192

93+
// Find the maximum distance from the origin to any vertex
94+
maxR := 0.0
95+
for _, vertex := range v {
96+
dist := math.Sqrt(float64(vertex[0]*vertex[0] + vertex[1]*vertex[1] + vertex[2]*vertex[2]))
97+
if dist > maxR {
98+
maxR = dist
99+
}
100+
}
101+
92102
loop:
93103
for {
104+
105+
w, h := termbox.Size()
106+
cx := w / 2
107+
cy := h / 2
108+
109+
limitR := float64(h) / 2.2 * 0.25
110+
if float64(w)/4.0 < limitR {
111+
limitR = float64(w) / 4.0
112+
}
113+
MaxScale := limitR / maxR
114+
94115
select {
95116
case ev := <-ch:
96117
switch ev.Type {
@@ -112,7 +133,7 @@ loop:
112133
}
113134
if string(ev.Rune) == "z" {
114135
scale += 0.1
115-
scale = math.Min(1.3, scale)
136+
scale = math.Min(MaxScale, scale)
116137
}
117138
if string(ev.Rune) == "x" {
118139
scale -= 0.1
@@ -134,12 +155,12 @@ loop:
134155
logIndex++
135156
}
136157

137-
if w {
158+
if opts.Watch {
138159
yaw = math.Mod(yaw+0.02, twoPi)
139160
pitch = math.Mod(pitch+0.01, twoPi)
140161
}
141162

142-
faceData := m.GetShape(yaw, pitch, scale, 40, 20)
163+
faceData := m.GetShape(yaw, pitch, scale, cx, cy)
143164
for _, fd := range faceData {
144165
if output == "solid" {
145166
for _, p := range fd.Fill {

internal/app/pod/pod.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"cubectl/internal/logger"
1212
"cubectl/internal/terminal"
13+
14+
"github.com/nsf/termbox-go"
1315
)
1416

1517
type Options struct {
@@ -19,7 +21,6 @@ type Options struct {
1921

2022
func Render(ctx context.Context, opts Options) error {
2123
output := opts.Output
22-
w := opts.Watch
2324

2425
if output == "" {
2526
output = "wireframe" // default
@@ -130,11 +131,32 @@ func Render(ctx context.Context, opts Options) error {
130131
drawString := func(x, y int, str string) {
131132
for i, r := range str {
132133
s.SetCell(x+i, y, r, terminal.ColorDefault, terminal.ColorBlack)
134+
135+
}
136+
}
137+
138+
// Find the maximum distance from the origin to any vertex
139+
maxR := 0.0
140+
for _, vertex := range v {
141+
dist := math.Sqrt(float64(vertex[0]*vertex[0] + vertex[1]*vertex[1] + vertex[2]*vertex[2]))
142+
if dist > maxR {
143+
maxR = dist
133144
}
134145
}
135146

136147
loop:
137148
for {
149+
150+
w, h := termbox.Size()
151+
cx := w / 2
152+
cy := h / 2
153+
154+
limitR := float64(h) / 2.2 * 0.25
155+
if float64(w)/4.0 < limitR {
156+
limitR = float64(w) / 4.0
157+
}
158+
MaxScale := limitR / maxR
159+
138160
select {
139161
case ev := <-ch:
140162
switch ev.Type {
@@ -156,7 +178,7 @@ loop:
156178
}
157179
if string(ev.Rune) == "z" {
158180
scale += 0.1
159-
scale = math.Min(1.3, scale)
181+
scale = math.Min(MaxScale, scale)
160182
}
161183
if string(ev.Rune) == "x" {
162184
scale -= 0.1
@@ -178,12 +200,12 @@ loop:
178200
logIndex++
179201
}
180202

181-
if w {
203+
if opts.Watch {
182204
yaw = math.Mod(yaw+0.02, twoPi)
183205
pitch = math.Mod(pitch+0.01, twoPi)
184206
}
185207

186-
faceData := m.GetShape(yaw, pitch, scale, 40, 20)
208+
faceData := m.GetShape(yaw, pitch, scale, cx, cy)
187209
for _, fd := range faceData {
188210
if output == "solid" {
189211
for _, p := range fd.Fill {

0 commit comments

Comments
 (0)