ng

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: MIT Imports: 0 Imported by: 1

README

ng

Package ng provides generic numeric types and interfaces. These facilitate interoperability between data structures that are structurally compatible, regardless of their original definition.

Example

The following demonstrates how functions can use these interfaces to work with different, but structurally similar, types (e.g., image.Point and a custom struct).

package ng_test

import (
	"image"
	"github.com/eihigh/ng"
)

// CirclesHit demonstrates interoperability using ng.Vec2like for vector inputs
// and ng.Scalar for generic scalar type S (used for components and radii).
func CirclesHit[P ng.Vec2like[S], S ng.Scalar](a, b P, rA, rB S) bool {
	va := ng.Vec2[S](a)
	vb := ng.Vec2[S](b)
	dx := va.X - vb.X
	dy := va.Y - vb.Y
	dsq := dx*dx + dy*dy
	return dsq <= (rA+rB)*(rA+rB)
}

type myVec2f struct{ X, Y float32 }

func Example_interoperability() {
	// Example call with image.Point
	var p1, p2 image.Point
	_ = CirclesHit(p1, p2, 1, 2)

	// Example call with a custom type myVec2f
	var f1, f2 myVec2f
	_ = CirclesHit(f1, f2, 1.0, 2.0)
}

For API details, see pkg.go.dev/github.com/eihigh/ng.

Documentation

Overview

Package ng provides generic numeric types and interfaces for interoperability between structurally compatible types.

Example
package main

import (
	"image"

	"github.com/eihigh/ng"
)

// CirclesHit demonstrates interoperability using ng.Vec2like for vector inputs
// and ng.Scalar for generic scalar type S (used for components and radii).
func CirclesHit[P ng.Vec2like[S], S ng.Scalar](a, b P, rA, rB S) bool {
	// ng.Vec2like enables accepting various 2D vector types.
	// ng.Scalar allows S to be any supported int or float type.
	va := ng.Vec2[S](a)
	vb := ng.Vec2[S](b)
	dx := va.X - vb.X
	dy := va.Y - vb.Y
	dsq := dx*dx + dy*dy
	return dsq <= (rA+rB)*(rA+rB)
}

type myVec2f struct{ X, Y float32 }

func main() {
	// Example call with image.Point
	var p1, p2 image.Point
	_ = CirclesHit(p1, p2, 1, 2)

	// Example call with a custom type myVec2f
	var f1, f2 myVec2f
	_ = CirclesHit(f1, f2, 1.0, 2.0)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Float

type Float interface {
	~float32 | ~float64
}

type Int

type Int interface {
	SignedInt | UnsignedInt
}

type Scalar

type Scalar interface {
	Int | Float
}

type SignedInt

type SignedInt interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type UnsignedInt

type UnsignedInt interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type Vec2

type Vec2[S Scalar] struct{ X, Y S }

type Vec2like

type Vec2like[S Scalar] interface{ ~struct{ X, Y S } }

type Vec3

type Vec3[S Scalar] struct{ X, Y, Z S }

type Vec3like

type Vec3like[S Scalar] interface{ ~struct{ X, Y, Z S } }

type Vec4

type Vec4[S Scalar] struct{ X, Y, Z, W S }

type Vec4like

type Vec4like[S Scalar] interface{ ~struct{ X, Y, Z, W S } }

Jump to

Keyboard shortcuts

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