imggg

package module
v0.0.0-...-3eac991 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: 0BSD Imports: 5 Imported by: 0

README

Go Reference Go Report Card

imggg - go's image generic geometrics, reimplements image.Point and image.Rectangle to work with any number type you want: int, int8, int16, int32, int64, float32, float64, uint, uint8, uint16, uint32, uint64, uintptr, instead of just int.

Features:

  • All the same methods and functions as image, the code is modified directly from https://go.dev/src/image/geom.go.
  • Cast them back to the standard library equivalent with the Std() method.
  • Get X and Y of a point as two variables quickly with the XY() method.

Library made for the egriden game framework. .

[!NOTE] Using points and rectangles with floats changes their logic from the intentions of the image library, read more on The Go Blog about the package.

[!WARNING] Operations on unsigned integers is not tested, just try not to underflow things.

Usage

package main

import (
    "github.com/greenthepear/imggg"
    "image"
)

func main(){
    // Be implicit
    floatPoint1 := imggg.Pt[float64](0.2,0.1)
    // Let the compiler figure it out
    floatPoint2 := imggg.Pt(1.6,2.2)
    // Use the classic methods
    rec := imggg.Rectangle[float64]{
		floatPoint1.Mul(10),
		floatPoint2.Div(0.1),
	} // (2,1)-(16,22)

    // Work with the standard library
    img := image.NewRGNA(
        rec.Std()
    )
    ...
}

License

Do what you want with this, to be fancy I chose the 0BSD license.

Uses Go source code, Copyright 2010 The Go Authors. All rights reserved: LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

Number interface by combining the exp/contraints' Integer and Float

type Point

type Point[V Number] struct {
	X, Y V
}

A Point is an X, Y coordinate pair. The axes increase right and down.

func Pt

func Pt[V Number](X, Y V) Point[V]

Pt is shorthand for Point{X, Y}.

func (Point[V]) Add

func (p Point[V]) Add(q Point[V]) Point[V]

Add returns the vector p+q.

func (Point[V]) Div

func (p Point[V]) Div(k V) Point[V]

Div returns the vector p/k.

func (Point[V]) Eq

func (p Point[V]) Eq(q Point[V]) bool

Eq reports whether p and q are equal.

func (Point[V]) In

func (p Point[V]) In(r Rectangle[V]) bool

In reports whether p is in r.

func (Point[V]) Mod

func (p Point[V]) Mod(r Rectangle[V]) Point[V]

Mod returns the point q in r such that p.X-q.X is a multiple of r's width and p.Y-q.Y is a multiple of r's height.

func (Point[V]) Mul

func (p Point[V]) Mul(k V) Point[V]

Mul returns the vector p*k.

func (Point[V]) Std

func (p Point[V]) Std() image.Point

Std returns the point as the standard package's image.Point type by a simple cast of X and Y to an int.

func (Point[V]) String

func (p Point[V]) String() string

String returns a string representation of p like "(3,4)".

func (Point[V]) Sub

func (p Point[V]) Sub(q Point[V]) Point[V]

Sub returns the vector p-q.

func (Point[V]) XY

func (p Point[V]) XY() (V, V)

XY returns p.X and p.Y as two variables

type Rectangle

type Rectangle[V Number] struct {
	Min, Max Point[V]
}

A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y. It is well-formed if Min.X <= Max.X and likewise for Y. Points are always well-formed. A rectangle's methods always return well-formed outputs for well-formed inputs.

A Rectangle is also an [Image] whose bounds are the rectangle itself. At returns color.Opaque for points in the rectangle and color.Transparent otherwise.

func Rect

func Rect[V Number](x0, y0, x1, y1 V) Rectangle[V]

Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}. The returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.

func (Rectangle[V]) Add

func (r Rectangle[V]) Add(p Point[V]) Rectangle[V]

Add returns the rectangle r translated by p.

func (Rectangle[V]) At

func (r Rectangle[V]) At(x, y V) color.Color

At implements the [Image] interface.

func (Rectangle[V]) Bounds

func (r Rectangle[V]) Bounds() Rectangle[V]

Bounds implements the [Image] interface.

func (Rectangle[V]) Canon

func (r Rectangle[V]) Canon() Rectangle[V]

Canon returns the canonical version of r. The returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.

func (Rectangle[V]) ColorModel

func (r Rectangle[V]) ColorModel() color.Model

ColorModel implements the [Image] interface.

func (Rectangle[V]) Dx

func (r Rectangle[V]) Dx() V

Dx returns r's width.

func (Rectangle[V]) Dy

func (r Rectangle[V]) Dy() V

Dy returns r's height.

func (Rectangle[V]) Empty

func (r Rectangle[V]) Empty() bool

Empty reports whether the rectangle contains no points.

func (Rectangle[V]) Eq

func (r Rectangle[V]) Eq(s Rectangle[V]) bool

Eq reports whether r and s contain the same set of points. All empty rectangles are considered equal.

func (Rectangle[V]) In

func (r Rectangle[V]) In(s Rectangle[V]) bool

In reports whether every point in r is in s.

func (Rectangle[V]) Inset

func (r Rectangle[V]) Inset(n V) Rectangle[V]

Inset returns the rectangle r inset by n, which may be negative. If either of r's dimensions is less than 2*n then an empty rectangle near the center of r will be returned.

func (Rectangle[V]) Intersect

func (r Rectangle[V]) Intersect(s Rectangle[V]) Rectangle[V]

Intersect returns the largest rectangle contained by both r and s. If the two rectangles do not overlap then the zero rectangle will be returned.

func (Rectangle[V]) Overlaps

func (r Rectangle[V]) Overlaps(s Rectangle[V]) bool

Overlaps reports whether r and s have a non-empty intersection.

func (Rectangle[V]) RGBA64At

func (r Rectangle[V]) RGBA64At(x, y V) color.RGBA64

RGBA64At implements the [RGBA64Image] interface.

func (Rectangle[V]) Size

func (r Rectangle[V]) Size() Point[V]

Size returns r's width and height.

func (Rectangle[V]) Std

func (p Rectangle[V]) Std() image.Rectangle

Std returns the rectangle as the standard package's image.Rectangle type by a simple cast of the two points' X and Y to ints.

func (Rectangle[V]) String

func (r Rectangle[V]) String() string

String returns a string representation of r like "(3,4)-(6,5)".

func (Rectangle[V]) Sub

func (r Rectangle[V]) Sub(p Point[V]) Rectangle[V]

Sub returns the rectangle r translated by -p.

func (Rectangle[V]) Union

func (r Rectangle[V]) Union(s Rectangle[V]) Rectangle[V]

Union returns the smallest rectangle that contains both r and s.

Jump to

Keyboard shortcuts

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