mandelbrot

package module
v0.0.0-...-cbf2d26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2023 License: MIT Imports: 11 Imported by: 0

README

mandelbrot

Documentation

Mandelbrot Fractal Generator library in Go with accompanying CLI

Library

Basic example usage of the library:

package main

import (
  "os"
  "image/color"

  "github.com/linuxfreak003/mandelbrot"
)

// Simple example color scheme function
func ColorMe(i int) color.RGBA {
  black := color.RGBA{0,0,0,0xff}
  white := color.RGBA{255,255,255,0xff}
  if i == -1 {
    return black
  }
  return mandelbrot.Gradient(black, white, 1000, i)
}

func main() {
  // Finds an interesting point to use
  x, y := mandelbrot.FindInterestingPoint(0,0)

  // Get a new generator, use ColorMe for
  // the colorscheme
  g := mandelbrot.NewGenerator(1024, 768, x, y).
    WithColorizeFunc(ColorMe)

  // Generate the image
  g.Generate()

  // Save the image to file
  f, err := os.Create("test.png")
  if err != nil {
    panic(err)
  }
  // As a PNG
  err = g.WritePNG(f)
  if err != nil {
    panic(err)
  }
}

Refer to the docs for more options like zoom, antialiasing, etc.

CLI

Installation
go get -u github.com/linuxfreak003/mandelbrot/mandel-go
Usage
Usage of mandel-go:
  -aa int
        AntiAlias level (default 1)
  -colorfunc Color Function
        Color Function (default "SimpleGrayscale")
  -h    Show usage help
  -height int
        Resolution height (default 768)
  -j    Julia Set
  -limit int
        Fractal Calculation limit (default 1000)
  -o string
        Output Filename (default "test.png")
  -r    Use random interesting point (will override x/y)
  -width int
        Resolution width (default 1024)
  -x float
        X
  -y float
        Y
  -zoom float
        Zoom (default 1)

Documentation

Overview

Package mandel implements a simple library for generating mandelbrot fractals.

Functions are provided for writing a png or jpeg Basic usage of package is

// Creates a new Generator g := NewGenerator(1024, 768, 0,0) // Generates the image g.Generate() // Writes the image as a png to "test.png" f, _ := os.Create("test.png") g.WritePNG(f)

Index

Constants

View Source
const VERSION = "v1.0"

Variables

This section is empty.

Functions

func Average

func Average(colors ...color.RGBA) color.RGBA

Average takes a list of color.RGBA and returns the average

func Calculate

func Calculate(x, y float64, limit int) int

Calculate does the actual mandelbrot calculation if it reached the limit it will bail out with (-1) if it exits the mandelbrot constraint, it will return the number of iterations to do so.

func DefaultColorize

func DefaultColorize(iter int) color.RGBA

DefaultColorze is a very simple Greyscale ColorFunc

func FindInterestingPoint

func FindInterestingPoint(x, y float64, limit int) (float64, float64)

FindInterestingPoint is a helper function that picks a bunch of random points on the fractal until it find one that is interesting

func Gradient

func Gradient(start, end color.RGBA, size, steps int) color.RGBA

Gradient breaks down the path between the two colors into `size` number of parts, and returns the color that is `steps` number of steps from the start color to the end color

Types

type ColorFunc

type ColorFunc func(int) color.RGBA

ColorFucn is what is used to generate the colorscheme It takes the number of iterations from the mandelbrot calculation and returns a color DefaultColorize is an extremely basic example

type Generator

type Generator struct {
	// Width and Height specify the resolution to use
	Width  int
	Height int
	// X and Y specify what point on the fractal to center on
	X float64
	Y float64
	// Zoom specifies how much to zoom in
	Zoom float64
	// Limit specifies when the mandelbrot calculation
	// should bail out and return -1 instead of
	// the number of iterations
	Limit int
	// AntiAlias specifies what level of antialiasing to use
	// An AntiAlias of 2 will average 4 points for each pixel
	// 3 will average 9 points. The increase is exponential
	AntiAlias int
	// Colorize is the ColorFunc used to generate the colorscheme
	Colorize ColorFunc

	// StartX specifies the starting x if generating video
	StartX float64
	// StartY specifies the starting y if generating video
	StartY float64
	// StartZoom specifies the starting zoom if generating video
	StartZoom float64
	// Frames specifies the number of frames to use if generating video
	Frames int
	// contains filtered or unexported fields
}

Generator is the used to generate the fractal

func NewGenerator

func NewGenerator(width, height int, x, y float64) *Generator

NewGenerator creates a new *Generator it should be used to ensure all fields are filled.

func (*Generator) AntiAliasedColor

func (g *Generator) AntiAliasedColor(x, y, inc float64) color.RGBA

AntiAliasedColor breaks a pixel down into parts and gets the color for each point, then averages them out for the pixel color

func (*Generator) Generate

func (g *Generator) Generate()

Generate does the mandelbrot calculation and stores the fractal into an image

func (*Generator) GetColor

func (g *Generator) GetColor(x, y float64) color.RGBA

GetColor gets the mandelbrot calculation iterations and Uses the defined Colorize function turn into a color

func (*Generator) SetColorize

func (g *Generator) SetColorize(cf ColorFunc)

func (*Generator) SetHeight

func (g *Generator) SetHeight(height int)

func (*Generator) SetLimit

func (g *Generator) SetLimit(limit int)

func (*Generator) SetWidth

func (g *Generator) SetWidth(width int)

func (*Generator) SetX

func (g *Generator) SetX(x float64)

func (*Generator) SetY

func (g *Generator) SetY(y float64)

func (*Generator) SetZoom

func (g *Generator) SetZoom(zoom float64)

func (*Generator) WithAntiAlias

func (g *Generator) WithAntiAlias(aa int) *Generator

Sets the AntiAliasing level

func (*Generator) WithColorizeFunc

func (g *Generator) WithColorizeFunc(f ColorFunc) *Generator

Sets the Colorize function used to generate colorscheme

func (*Generator) WithLimit

func (g *Generator) WithLimit(l int) *Generator

Sets the bailout limit for fractal

func (*Generator) WithZoom

func (g *Generator) WithZoom(z float64) *Generator

Sets the zoom level

func (*Generator) WriteJPG

func (g *Generator) WriteJPG(w io.Writer) error

WriteJPG writes the underlying image to a an io.Writer as a JPG

func (*Generator) WritePNG

func (g *Generator) WritePNG(w io.Writer) error

WritePNG writes the underlying image to a an io.Writer as a PNG

func (*Generator) WriteVideo

func (g *Generator) WriteVideo(file string) error

WriteVideo generates a video file

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL