Created
March 7, 2023 00:36
-
-
Save c4pt0r/ae90bd2c43f238230ffb6aa2b764b0c9 to your computer and use it in GitHub Desktop.
Vortex1.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
"math/rand" | |
"os" | |
"os/exec" | |
"strconv" | |
"strings" | |
"time" | |
) | |
var ( | |
width int = 60 | |
height int = 30 | |
) | |
func getTerminalSize() (int, int) { | |
cmd := exec.Command("stty", "size") | |
cmd.Stdin = os.Stdin | |
out, _ := cmd.Output() | |
size := string(out) | |
size = size[:len(size)-1] // remove newline character | |
s := strings.Split(size, " ") | |
width, _ := strconv.Atoi(s[1]) | |
height, _ := strconv.Atoi(s[0]) | |
return width, height | |
} | |
func main() { | |
width, height = getTerminalSize() | |
fmt.Println("width:", width, "height:", height) | |
for { | |
for i := 0; i < 360; i += 10 { | |
fmt.Printf("\033[2J\033[1;1H") // clear the screen | |
fmt.Println(getVortex(i)) | |
time.Sleep(100 * time.Millisecond) | |
} | |
} | |
} | |
// get random color for print | |
func getColor() (string, string) { | |
colors := []string{"\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", "\033[37m"} | |
return colors[rand.Intn(len(colors))], "\033[0m" | |
} | |
func getRandomChar() rune { | |
chars := []rune("#_*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") | |
return chars[rand.Intn(len(chars))] | |
} | |
func getVortex(angle int) string { | |
// create vortex 2d array | |
vortex := make([][]rune, height) | |
for i := range vortex { | |
vortex[i] = make([]rune, width) | |
} | |
for y := 0; y < height; y++ { | |
for x := 0; x < width; x++ { | |
dx := float64(x - width/2) | |
dy := float64(y - height/2) | |
r := math.Sqrt(dx*dx + dy*dy) | |
a := math.Atan2(dy, dx) + math.Pi*2 | |
r2 := r + math.Sin(float64(angle)/180.0*math.Pi*2+r/10.0)*10.0 | |
x2 := int(r2*math.Cos(a)) + width/2 | |
y2 := int(r2*math.Sin(a)) + height/2 | |
if x2 >= 0 && x2 < width && y2 >= 0 && y2 < height { | |
vortex[y2][x2] = getRandomChar() | |
} | |
} | |
} | |
// Convert the vortex to a string | |
var result string | |
for y := 0; y < height; y++ { | |
for x := 0; x < width; x++ { | |
if vortex[y][x] != 0 { | |
color, reset := getColor() | |
result += color + string(vortex[y][x]) + reset | |
} else { | |
result += " " | |
} | |
} | |
result += "\n" | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment