### Initialize and Run Simulation Model Source: https://context7.com/fogleman/physarum/llms.txt Orchestrates the simulation cycle by managing particles, grids, and configurations. Requires defining simulation parameters and running the step loop. ```go package main import ( "fmt" "math/rand" "time" "github.com/fogleman/physarum/pkg/physarum" ) func main() { rand.Seed(time.Now().UTC().UnixNano()) // Simulation parameters width := 1024 height := 1024 numParticles := 1 << 22 // ~4 million particles blurRadius := 1 blurPasses := 2 zoomFactor := float32(1.0) // Create random configurations for 3 particle species numSpecies := 3 configs := physarum.RandomConfigs(numSpecies) attractionTable := physarum.RandomAttractionTable(numSpecies) // Initialize the model model := physarum.NewModel( width, height, numParticles, blurRadius, blurPasses, zoomFactor, configs, attractionTable, ) // Run simulation for 400 iterations for i := 0; i < 400; i++ { model.Step() if i%100 == 0 { fmt.Printf("Iteration %d complete\n", i) } } // Get grid data for rendering gridData := model.Data() fmt.Printf("Generated %d grids of size %dx%d\n", len(gridData), width, height) } ``` -------------------------------- ### Run Interactive Viewer Application Source: https://context7.com/fogleman/physarum/llms.txt Launches an interactive viewer for real-time simulation visualization using OpenGL. Keyboard controls are available for simulation management and visual adjustments. ```bash # Run the interactive viewer go run cmd/viewer/main.go # Keyboard controls: # Space - Generate new random simulation # R - Restart current simulation # P - Apply new random palette # O - Shuffle current palette colors # A - Auto-level color intensity ``` -------------------------------- ### Run Continuous Simulation and Image Generation Source: https://context7.com/fogleman/physarum/llms.txt Executes an indefinite simulation loop that generates new random configurations and outputs timestamped PNG images. Ensure sufficient disk space for generated images. ```go package main import ( "math/rand" "time" "github.com/fogleman/physarum/pkg/physarum" ) func main() { // Seed random number generator rand.Seed(time.Now().UTC().UnixNano()) // Run continuous generation (outputs timestamped PNG files) // This runs indefinitely, generating new images with random configs physarum.Run() } ``` -------------------------------- ### Utility Functions for Angle Conversion, Color, and Coordinates Source: https://context7.com/fogleman/physarum/llms.txt Provides utility functions for converting between degrees and radians, creating colors from hex values, checking if a number is a power of two, and wrapping coordinates within a toroidal space. Also includes image saving functionality. ```go package main import ( "fmt" "image" "image/color" "image/png" "github.com/fogleman/physarum/pkg/physarum" ) func main() { // Angle conversion degrees := float32(45.0) radians := physarum.Radians(degrees) backToDegrees := physarum.Degrees(radians) fmt.Printf("%.1f degrees = %.4f radians = %.1f degrees\n", degrees, radians, backToDegrees) // Create color from hex value orange := physarum.HexColor(0xFF8000) fmt.Printf("Orange: R=%d G=%d B=%d A=%d\n", orange.R, orange.G, orange.B, orange.A) // Check power of two (required for grid dimensions) fmt.Printf("512 is power of two: %v\n", physarum.IsPowerOfTwo(512)) fmt.Printf("500 is power of two: %v\n", physarum.IsPowerOfTwo(500)) // Coordinate wrapping (toroidal space) wrapped := physarum.Shift(1050.0, 1024.0) // Wraps to 26.0 fmt.Printf("1050 wrapped to 1024 space: %.1f\n", wrapped) // Save image to PNG with compression control im := image.NewRGBA(image.Rect(0, 0, 100, 100)) for y := 0; y < 100; y++ { for x := 0; x < 100; x++ { im.Set(x, y, color.RGBA{uint8(x * 2), uint8(y * 2), 128, 255}) } } physarum.SavePNG("test.png", im, png.BestSpeed) } ``` -------------------------------- ### Generate Particle Behavior Configurations Source: https://context7.com/fogleman/physarum/llms.txt Defines behavior parameters for particle species. Use random generators to create configurations and attraction tables for multi-species interactions. ```go package main import ( "fmt" "math/rand" "github.com/fogleman/physarum/pkg/physarum" ) func main() { rand.Seed(42) // Generate a single random configuration config := physarum.RandomConfig() fmt.Printf("Single Config:\n") fmt.Printf(" SensorAngle: %.4f rad\n", config.SensorAngle) fmt.Printf(" SensorDistance: %.4f\n", config.SensorDistance) fmt.Printf(" RotationAngle: %.4f rad\n", config.RotationAngle) fmt.Printf(" StepDistance: %.4f\n", config.StepDistance) fmt.Printf(" DepositionAmount: %.4f\n", config.DepositionAmount) fmt.Printf(" DecayFactor: %.4f\n", config.DecayFactor) // Generate multiple configurations for multi-species simulation numSpecies := 4 configs := physarum.RandomConfigs(numSpecies) // Create attraction table defining species interactions // Positive values = attraction, negative values = repulsion table := physarum.RandomAttractionTable(numSpecies) // Print configuration summary physarum.PrintConfigs(configs, table) physarum.SummarizeConfigs(configs) } ``` -------------------------------- ### Manage and Visualize Color Palettes Source: https://context7.com/fogleman/physarum/llms.txt Provides functionalities to generate, shuffle, and display color palettes for visualizing particle species. Custom colors can be created using HexColor. ```go package main import ( "fmt" "math/rand" "github.com/fogleman/physarum/pkg/physarum" ) func main() { rand.Seed(42) // Get a random palette from the predefined set palette := physarum.RandomPalette() fmt.Println("Random palette colors:") palette.Print() // Shuffle an existing palette shuffled := physarum.ShuffledPalette(palette) fmt.Println("Shuffled palette:") shuffled.Print() // Access predefined palettes directly fmt.Printf("Available palettes: %d\n", len(physarum.Palettes)) firstPalette := physarum.Palettes[0] fmt.Println("First predefined palette:") firstPalette.Print() // Create custom colors using HexColor utility customRed := physarum.HexColor(0xFF0000) customGreen := physarum.HexColor(0x00FF00) fmt.Printf("Custom red: R=%d G=%d B=%d\n", customRed.R, customRed.G, customRed.B) fmt.Printf("Custom green: R=%d G=%d B=%d\n", customGreen.R, customGreen.G, customGreen.B) } ``` -------------------------------- ### Manage Chemical Trail Grids Source: https://context7.com/fogleman/physarum/llms.txt Handles chemical concentration storage with support for toroidal wrapping and diffusion effects. Grid dimensions must be powers of two. ```go package main import ( "fmt" "github.com/fogleman/physarum/pkg/physarum" ) func main() { // Grid dimensions must be powers of two width := 512 height := 512 // Create a new grid (initialized with random values) grid := physarum.NewGrid(width, height) // Add chemical at specific coordinates grid.Add(256.0, 256.0, 5.0) // Add 5.0 units at center grid.Add(100.5, 200.7, 3.0) // Coordinates support sub-pixel precision // Read chemical concentration at position (wraps around edges) value := grid.Get(256.0, 256.0) fmt.Printf("Chemical at center: %.4f\n", value) // Apply blur and decay blurRadius := 1 blurIterations := 2 decayFactor := float32(0.9) // 10% decay per step grid.BoxBlur(blurRadius, blurIterations, decayFactor) fmt.Printf("After blur/decay: %.4f\n", grid.Get(256.0, 256.0)) } ``` -------------------------------- ### Render Simulation Grid Data to Image Source: https://context7.com/fogleman/physarum/llms.txt Converts simulation grid data into a colorized image using a specified color palette. Supports gamma correction and automatic level adjustment. Ensure the output filename is valid. ```go package main import ( "image/png" "math/rand" "github.com/fogleman/physarum/pkg/physarum" ) func main() { rand.Seed(42) // Set up and run a simulation width, height := 512, 512 configs := physarum.RandomConfigs(3) table := physarum.RandomAttractionTable(3) model := physarum.NewModel(width, height, 1<<20, 1, 2, 1.0, configs, table) // Run simulation for i := 0; i < 200; i++ { model.Step() } // Generate a random color palette palette := physarum.RandomPalette() // Create image from grid data // Parameters: width, height, grids, palette, min, max, gamma // When min == max (both 0), auto-leveling is applied gridData := model.Data() im := physarum.Image(width, height, gridData, palette, 0, 0, 1/2.2) // Save to PNG file err := physarum.SavePNG("output.png", im, png.DefaultCompression) if err != nil { panic(err) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.