custom

package
v0.1.0-rc1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package custom provides a noise generator using the user-defined function. Which implements github.com/KEINOS/go-noise/noise interface.

You need to define the function before calling `Eval32` or `Eval64`.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {

	// Seed holds the seed value for the noise.
	Seed int64
	// contains filtered or unexported fields
}

Generator holds parameters of user-defined noise generator.

func New

func New(seed int64) *Generator

New returns a seeded Perlin noise instance.

func (*Generator) Eval32

func (n *Generator) Eval32(dim ...float32) float32

Eval32 returns a float32 noise value using the user-defined function for the given coordinates.

func (*Generator) Eval64

func (n *Generator) Eval64(dim ...float64) float64

Eval64 returns a float64 noise value using the user-defined function for the given coordinates.

func (*Generator) SetEval32

func (n *Generator) SetEval32(f func(seed int64, dim ...float32) float32) error

SetEval32 sets the user-defined noise generator function for float32.

Example

Example to use the custom noise generator for float32.

This example uses the `custom.New` function to instantiate a new noise generator. Then the `custom.SetEval32` function is used to set the user- defined function.

package main

import (
	"fmt"
	"log"
	"math/rand"

	"github.com/KEINOS/go-noise/pkg/custom"
)

func main() {
	const seed = int64(12345)

	var rnd *rand.Rand

	// Instantiate a new noise generator.
	genNoise := custom.New(seed)

	// User custom function.
	// It generates pseudo-random numbers between -1 and 1.
	myCustomFunc := func(seed int64, dim ...float32) float32 {
		if rnd == nil {
			//nolint:gosec // Use of weak random number generation is intended for simple examples.
			rnd = rand.New(rand.NewSource(seed))
		}

		for i := 0; i < len(dim); i++ {
			max := int(dim[i])

			for ii := 0; ii < max; ii++ {
				_ = rnd.Float32()
			}
		}

		// Generate a pseudo-random number.
		v := rnd.Float32()

		return v*2 - 1 // Convert [0.0,1.0] to [-1.0,1.0]
	}

	// Set the user-defined function.
	// The function must return a value between -1 and 1. Same seed values should
	// generate the same pseudo-random sequence.
	if err := genNoise.SetEval32(myCustomFunc); err != nil {
		log.Fatal(err)
	}

	out := genNoise.Eval32(1.0, 2.0, 3.0)

	fmt.Printf("%f\n", out)

}
Output:

-0.969880

func (*Generator) SetEval64

func (n *Generator) SetEval64(f func(seed int64, dim ...float64) float64) error

SetEval64 sets the user-defined noise generator function for float64.

Example

Example to use the custom noise generator for float64.

This example uses the `custom.New` function to instantiate a new noise generator. Then the `custom.SetEval642` function is used to set the user- defined function.

package main

import (
	"fmt"
	"log"
	"math/rand"

	"github.com/KEINOS/go-noise/pkg/custom"
)

func main() {
	const seed = int64(12345)

	var rnd *rand.Rand

	// Instantiate a new noise generator.
	genNoise := custom.New(seed)

	// User custom function.
	myCustomFunc := func(seed int64, dim ...float64) float64 {
		if rnd == nil {
			//nolint:gosec // Use of weak random number generation is intended for simple examples.
			rnd = rand.New(rand.NewSource(seed))
		}

		for i := 0; i < len(dim); i++ {
			max := int(dim[i])

			for ii := 0; ii < max; ii++ {
				_ = rnd.Float64()
			}
		}

		// Generate a pseudo-random number.
		v := rnd.Float64()

		return v*2 - 1 // Convert [0.0,1.0] to [-1.0,1.0]
	}

	// Set the user-defined function.
	// The function must return a value between -1 and 1. Same seed values should
	// generate the same pseudo-random sequence.
	if err := genNoise.SetEval64(myCustomFunc); err != nil {
		log.Fatal(err)
	}

	out := genNoise.Eval64(1.0, 2.0, 3.0)

	fmt.Printf("%f\n", out)

}
Output:

-0.969880

Jump to

Keyboard shortcuts

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