geo

package
v0.0.0-...-92253b2 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

README

geo

A collection of geometry primitives.

Why would you use this? You wouldn't. There are so many good math libraries. This is not one of them.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DegreesToRadians

func DegreesToRadians(degrees float64) float64

func DistanceFromPointToSegment

func DistanceFromPointToSegment[T Number](p Point[T], s Segment[T]) float64

DistanceFromPointToSegment calculates the distance from a point to a line segment Ugh, what a mess. I guess this is obsoleted by DistPointToSegment, although I don't see why this couldn't answer return the point. And then I suppose I should see if there are differenecs in result and speed to decide which to keep. Although looking at the implementation, it sorta looks like it's just a distance to the closest endpoint? Which seems completely wrong?

func FloatsEqual

func FloatsEqual(a, b float64) bool

func IsCollinear

func IsCollinear[T Number](p1, p2, p3 Point[T]) bool

IsCollinear checks if three points are collinear

func Max

func Max[T Number](a, b T) T

func Min

func Min[T Number](a, b T) T

func NullHitTest

func NullHitTest(PointF64) bool

func OnSegment

func OnSegment[T Number](p, start, end Point[T]) bool

OnSegment checks if a point lies on a line segment

func Orientation

func Orientation[T Number](p0, p1, p2 Point[T]) int

Orientation checks the orientation of three points

func RadiansToDegrees

func RadiansToDegrees(radians float64) float64

func Ratio

func Ratio(a, b float64) float64

Ratio answers a 0-1 value based on a's contributing factor to the final value.

func Sqr

func Sqr[T Number](x T) T

func XAtY

func XAtY(s SegmentF64, y float64) (float64, bool)

XAtY answers the X value for this segment at the given Y value, or false if the line does not intersect y.

func XYToIndex

func XYToIndex[T constraints.Integer](width, height T, pt Point[T]) (T, error)

XYToIndex converts an X, Y to a flat index.

Types

type CircleHitTest

type CircleHitTest struct {
	// contains filtered or unexported fields
}

func (*CircleHitTest) Hit

func (t *CircleHitTest) Hit(pt PointF64) bool

func (*CircleHitTest) Set

func (t *CircleHitTest) Set(center PointF64, radius float64)

func (*CircleHitTest) String

func (t *CircleHitTest) String() string

type Float

type Float interface {
	constraints.Float
}

type HitTest

type HitTest func(PointF64) bool

HitTest answers true if the point is a hit.

type Number

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

type Point

type Point[T Number] struct {
	X T
	Y T
}

func Centroid

func Centroid[T Number](pts []Point[T]) Point[T]

func ConvertPoint

func ConvertPoint[A Number, B Number](a Point[A]) Point[B]

func FindIntersectionBAD

func FindIntersectionBAD[T Number](s1, s2 Segment[T]) (Point[T], bool)

Actually, the answers everyone have given you so far are not optimal. They are imprecise, and so are not guaranteed to work on integer coordinates. Also, they are way too complicated.

Taken from Victor Lecomte's fabulous handbook, and modified for simplicity, this C++ function properInter returns whether there is an intersection between segments AB and CD:

struct pt { int x, int y };

int cross(pt a, pt b) {
    return a.x*b.y - a.y*b.x;
}
int orient(pt a, pt b, pt c) {
    return cross(b-a, c-a);
}
bool properInter(pt a, pt b, pt c, pt d) {
    int oa = orient(c,d,a),
        ob = orient(c,d,b),
        oc = orient(a,b,c),
        od = orient(a,b,d);
    // Proper intersection exists iff opposite signs
    return (oa*ob < 0 && oc*od < 0);
}

FindIntersection finds the intersection point of two line segments

func IndexToXY

func IndexToXY[T constraints.Integer](width, height, index T) (Point[T], error)

IndexToXY converts a flat index into an array into an XY position.

func PerpendicularIntersection

func PerpendicularIntersection[T Float](seg Segment[T], pt Point[T]) (Point[T], bool)

PerpendicularIntersection finds the intersection of point to the line segment by drawing a perpendicular line from point to segment. Note: this is from Gemini. Seems to work but not heavily tested.

func Pt

func Pt[T Number](x, y T) Point[T]

Pt is shorthand for creating a point from two values.

func (Point[T]) Add

func (a Point[T]) Add(b Point[T]) Point[T]

func (Point[T]) Area

func (p Point[T]) Area() T

func (Point[T]) Degrees

func (a Point[T]) Degrees(b Point[T]) float64

Degrees finds the angle of the segment with this point as the origin. Degrees will be 0-360, with 0/360 on the right, proceeding clockwise.

func (Point[T]) Dist

func (a Point[T]) Dist(b Point[T]) float64

func (Point[T]) DistSquared

func (a Point[T]) DistSquared(b Point[T]) T

Dist2 is the distance without the square root.

func (Point[T]) Inside

func (a Point[T]) Inside(r RectT[T]) bool

func (Point[T]) Magnitude

func (a Point[T]) Magnitude() float64

func (Point[T]) Mult

func (a Point[T]) Mult(b Point[T]) Point[T]

func (Point[T]) Normalize

func (a Point[T]) Normalize() Point[T]

func (Point[T]) Project

func (a Point[T]) Project(m, dist float64) (Point[T], Point[T])

Given slope m and distance, project the positive and negative points on the line. https://stackoverflow.com/questions/1250419/finding-points-on-a-line-with-a-given-distance

func (Point[T]) ProjectDegree

func (a Point[T]) ProjectDegree(deg, dist float64) Point[T]

ProjectDegree takes a degree and distance and projects a new point.

func (Point[T]) Radians

func (a Point[T]) Radians() float64

func (Point[T]) Sub

func (a Point[T]) Sub(b Point[T]) Point[T]

func (Point[T]) ToIndex

func (p Point[T]) ToIndex(xy Point[T]) T

ToIndex converts the xy point into an index into a flat array as represented by this point.

type Point3d

type Point3d[T Number] struct {
	X T
	Y T
}

func (Point3d[T]) Area

func (p Point3d[T]) Area() T

type Point3dF64

type Point3dF64 = Point3d[float64]

type Point3dI

type Point3dI = Point3d[int]

type Point3dI64

type Point3dI64 = Point3d[int64]

type Point3dUI64

type Point3dUI64 = Point3d[uint64]

type PointF32

type PointF32 = Point[float32]

type PointF64

type PointF64 = Point[float64]

func DistPointToSegment

func DistPointToSegment(seg SegmentF64, p PointF64) (float64, PointF64)

DistPointToSegment answers the distance from the point to the segment, as well as the point found on the segment. From https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment

func DistSquared

func DistSquared(seg SegmentF64, p PointF64) (float64, PointF64)

DistSquared answers the squared distance from the point to the segment, as well as the point found on the segment. From https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment

type PointI

type PointI = Point[int]

type PointI64

type PointI64 = Point[int64]

type PointUI64

type PointUI64 = Point[uint64]

type Poly

type Poly[T Number] struct {
	Pts []Point[T]
}

type PolyF64

type PolyF64 = Poly[float64]

type PolyI

type PolyI = Poly[int]

type PolyI64

type PolyI64 = Poly[int64]

type PolyUI64

type PolyUI64 = Poly[uint64]

type PolygonBB

type PolygonBB[T Number] struct {
	Pts []Point[T]
	BB  RectT[T]
}

PolygonBB is a closed polygon that includes the computed bounding box.

func PolyBB

func PolyBB[T Float](pts []Point[T]) PolygonBB[T]

type PolygonBBF64

type PolygonBBF64 = PolygonBB[float64]

type ProcessPtF64Func

type ProcessPtF64Func func(pt PointF64) PointF64

type Range

type Range[T Number] struct {
	Min T
	Max T
}

func Rng

func Rng[T Number](min, max T) Range[T]

func (Range[T]) Clip

func (p Range[T]) Clip(value T) T

Clip returns the value clipped to my range.

func (Range[T]) MapNormal

func (p Range[T]) MapNormal(normal T) T

MapNormal takes a normalized (0-1) value and maps it to my range.

func (Range[T]) Normalize

func (p Range[T]) Normalize(value T) float64

Normalize returns the value clipped to my range and normalized to 0-1.

type RangeF64

type RangeF64 = Range[float64]

type RangeI

type RangeI = Range[int]

type RangeI64

type RangeI64 = Range[int64]

type RectF32

type RectF32 = RectT[float32]

type RectF64

type RectF64 = RectT[float64]

type RectI

type RectI = RectT[int]

type RectI64

type RectI64 = RectT[int64]

type RectT

type RectT[T Number] struct {
	L, T, R, B T
}

func ConvertRect

func ConvertRect[A Number, B Number](a RectT[A]) RectT[B]

func PolygonBounds

func PolygonBounds[T Float](pts []Point[T]) RectT[T]

func Rect

func Rect[T Number](left, top, right, bottom T) RectT[T]

func (RectT[T]) Add

func (r1 RectT[T]) Add(l, t, r, b T) RectT[T]

func (RectT[T]) Area

func (r RectT[T]) Area() T

func (RectT[T]) LT

func (r RectT[T]) LT() Point[T]

func (RectT[T]) RB

func (r RectT[T]) RB() Point[T]

func (RectT[T]) Size

func (r RectT[T]) Size() Point[T]

func (RectT[T]) Translate

func (r RectT[T]) Translate(pt Point[T]) RectT[T]

func (RectT[T]) Union

func (r1 RectT[T]) Union(r2 RectT[T]) RectT[T]

func (RectT[T]) WithExpand

func (r RectT[T]) WithExpand(v T) RectT[T]

Expand adds the value to all edges.

func (RectT[T]) WithSize

func (r RectT[T]) WithSize(pt Point[T]) RectT[T]

type RectUI64

type RectUI64 = RectT[uint64]

type Segment

type Segment[T Number] struct {
	A Point[T]
	B Point[T]
}

Segment represents a line segment with start and end points

func ConvertSegment

func ConvertSegment[A Number, B Number](seg Segment[A]) Segment[B]

func Seg

func Seg[T Number](ax, ay, bx, by T) Segment[T]

Seg is shorthand for creating a segment from two points.

func (Segment[T]) Degrees

func (s Segment[T]) Degrees() float64

Degrees finds the angle of the segment with A as the origin. Degrees will be 0-360, with 0/360 on the right, proceeding clockwise.

func (Segment[T]) Dir

func (s Segment[T]) Dir() Point[T]

Dir answers the direction vector of this segment.

func (Segment[T]) Midpoint

func (s Segment[T]) Midpoint() Point[T]

func (Segment[T]) PerpendicularSlope

func (s Segment[T]) PerpendicularSlope() float64

PerpendicularSlope answers the perpendicular of Slope.

func (Segment[T]) Slope

func (s Segment[T]) Slope() float64

Slope answers the slope of this line segment. Vertical lines are slope math.MaxFloat64, horizontal lines are slope 0.

type SegmentF64

type SegmentF64 = Segment[float64]

type SegmentI

type SegmentI = Segment[int]

type SegmentI64

type SegmentI64 = Segment[int64]

type SegmentUI64

type SegmentUI64 = Segment[uint64]

Jump to

Keyboard shortcuts

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