geometry

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AngleBetweenVectors

func AngleBetweenVectors(v1, v2 Vector) float64

AngleBetweenVectors 计算两个向量之间的夹角

func AssertLineSegmentValid

func AssertLineSegmentValid(lineSegments ...LineSegment)

AssertLineSegmentValid 断言检查线段是否有效

func AssertPointValid

func AssertPointValid(points ...Point)

AssertPointValid 断言检查点是否有效

func AssertPolygonValid

func AssertPolygonValid(polygons ...Polygon)

AssertPolygonValid 断言检查多边形是否有效

func AssertVector2Valid

func AssertVector2Valid(vectors ...Vector2)

AssertVector2Valid 断言检查二维向量是否有效

func AssertVector3Valid

func AssertVector3Valid(vectors ...Vector3)

AssertVector3Valid 断言检查三维向量是否有效

func CalcAngleDifference

func CalcAngleDifference[T constraints.Number](a, b T) float64

CalcAngleDifference 计算两个极角之间的最小角度差。 结果在 -180 到 180 度之间,适用于极角、方位角或其他类似场景。

func CalcAngleWithDirection2D

func CalcAngleWithDirection2D(direction Direction2D) float64

CalcAngleWithDirection2D 计算二维方向的角度

func CalcLineSegmentCollinearWithEpsilon

func CalcLineSegmentCollinearWithEpsilon[T constraints.Number](line1, line2 LineSegment, epsilon T) bool

CalcLineSegmentCollinearWithEpsilon 计算两条线段是否共线,允许一定的误差来包容浮点数计算的误差

func CalcLineSegmentDistanceToPoint

func CalcLineSegmentDistanceToPoint(line LineSegment, point Point) float64

CalcLineSegmentDistanceToPoint 计算点到线段的距禂

func CalcPolygonCircumscribedCircleRadius

func CalcPolygonCircumscribedCircleRadius(polygon Polygon) float64

CalcPolygonCircumscribedCircleRadius 计算多边形外接圆的半径

func CalcTriangleAreaTwice

func CalcTriangleAreaTwice(a, b, c Vector2) float64

CalcTriangleAreaTwice 计算三角形面积的两倍

Types

type Circle

type Circle struct {
	Center Vector2
	Radius float64
}

func NewCircle

func NewCircle(center Vector2, radius float64) Circle

NewCircle 创建一个圆

func (Circle) Contains

func (c Circle) Contains(point Vector2) bool

Contains 判断点是否在圆内

func (Circle) GetArea

func (c Circle) GetArea() float64

GetArea 获取面积

func (Circle) GetCenter

func (c Circle) GetCenter() Vector2

GetCenter 获取圆心

func (Circle) GetCenterDistance

func (c Circle) GetCenterDistance(c2 Circle) float64

GetCenterDistance 计算两个圆心之间的距离

func (Circle) GetCircumference

func (c Circle) GetCircumference() float64

GetCircumference 获取周长

func (Circle) GetDiameter

func (c Circle) GetDiameter() float64

GetDiameter 获取直径

func (Circle) GetIntersectionPoints

func (c Circle) GetIntersectionPoints(c2 Circle) (Vector2, Vector2)

GetIntersectionPoints 获取两个圆的交点,如果没有交点则返回两个零向量

func (Circle) GetRadius

func (c Circle) GetRadius() float64

GetRadius 获取半径

func (Circle) Intersect

func (c Circle) Intersect(c2 Circle) bool

Intersect 判断两个圆是否相交

func (Circle) Overlap

func (c Circle) Overlap(c2 Circle) bool

Overlap 计算两个圆是否发生重叠

type Direction2D

type Direction2D = Vector2

Direction2D 二维方向

func CalcDirection2DWithAngle

func CalcDirection2DWithAngle[T constraints.Number](angle T) Direction2D

CalcDirection2DWithAngle 通过角度计算二维方向

func CalcOppositionDirection2D

func CalcOppositionDirection2D(direction Direction2D) Direction2D

CalcOppositionDirection2D 计算二维方向的反方向

func Direction2D4

func Direction2D4() []Direction2D

Direction2D4 上下左右四个方向

func Direction2D8

func Direction2D8() []Direction2D

Direction2D8 上下左右、左上、右上、左下、右下八个方向

func Direction2DDown

func Direction2DDown() Direction2D

Direction2DDown 获取下方向

func Direction2DLeft

func Direction2DLeft() Direction2D

Direction2DLeft 获取左方向

func Direction2DLeftDown

func Direction2DLeftDown() Direction2D

Direction2DLeftDown 获取左下方向

func Direction2DLeftUp

func Direction2DLeftUp() Direction2D

Direction2DLeftUp 获取左上方向

func Direction2DRight

func Direction2DRight() Direction2D

Direction2DRight 获取右方向

func Direction2DRightDown

func Direction2DRightDown() Direction2D

Direction2DRightDown 获取右下方向

func Direction2DRightUp

func Direction2DRightUp() Direction2D

Direction2DRightUp 获取右上方向

func Direction2DUnknown

func Direction2DUnknown() Direction2D

Direction2DUnknown 获取未知方向

func Direction2DUp

func Direction2DUp() Direction2D

Direction2DUp 获取上方向

type FloorPlan

type FloorPlan []string

FloorPlan 平面图

func (FloorPlan) IsFree

func (fp FloorPlan) IsFree(position Vector2) bool

IsFree 检查位置是否为空格

func (FloorPlan) IsInBounds

func (fp FloorPlan) IsInBounds(position Vector2) bool

IsInBounds 检查位置是否在边界内

func (FloorPlan) Put

func (fp FloorPlan) Put(position Vector2, c rune)

Put 设置平面图特定位置的字符

func (FloorPlan) String

func (fp FloorPlan) String() string

String 获取平面图结果

type Line

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

Line 直线是由一个点和斜率定义的

func NewLine

func NewLine(point Point, slope float64) Line

NewLine 创建一条直线

func (Line) IsPointAbove

func (l Line) IsPointAbove(point Point) bool

IsPointAbove 判断点是否在直线上方

func (Line) IsPointBelow

func (l Line) IsPointBelow(point Point) bool

IsPointBelow 判断点是否在直线下方

func (Line) IsPointLeft

func (l Line) IsPointLeft(point Point) bool

IsPointLeft 判断点是否在直线左侧

func (Line) IsPointOn

func (l Line) IsPointOn(point Point) bool

IsPointOn 判断点是否在直线上

func (Line) IsPointOnOrAbove

func (l Line) IsPointOnOrAbove(point Point) bool

IsPointOnOrAbove 判断点是否在直线上或者在直线上方

func (Line) IsPointOnOrBelow

func (l Line) IsPointOnOrBelow(point Point) bool

IsPointOnOrBelow 判断点是否在直线上或者在直线下方

func (Line) IsPointRight

func (l Line) IsPointRight(point Point) bool

IsPointRight 判断点是否在直线右侧

type LineSegment

type LineSegment []Point

LineSegment 由至少两个点组成的线段

func CalcLineSegmentOverlap

func CalcLineSegmentOverlap(line1, line2 LineSegment) (overlap LineSegment, isOverlap bool)

CalcLineSegmentOverlap 通过对点进行排序来检查两条共线线段是否重叠,返回重叠线段

func NewLineSegment

func NewLineSegment(points ...Point) LineSegment

NewLineSegment 创建一个线段

func (LineSegment) ClosestPoint

func (l LineSegment) ClosestPoint(point Point) Point

ClosestPoint 计算一个点到该线段的最近的点

func (LineSegment) GetDirection

func (l LineSegment) GetDirection() Vector2

GetDirection 获取线段的方向

func (LineSegment) GetLength

func (l LineSegment) GetLength() float64

GetLength 获取线段的长度

func (LineSegment) GetMidpoint

func (l LineSegment) GetMidpoint() Point

GetMidpoint 获取线段的中点

func (LineSegment) IsPointOnSegment

func (l LineSegment) IsPointOnSegment(point Point) bool

IsPointOnSegment 判断一个给定的点是否在该线段上,允许一定的误差来包容浮点数计算的误差,但不允许点超出线段的范围

type Point

type Point = Vector2

Point 由二维向量组成的点

func CalcLineSegmentPointProjection

func CalcLineSegmentPointProjection(line LineSegment, point Point) Point

CalcLineSegmentPointProjection 计算点在线段上的投影

func CalcLineSegmentPointProjectionDistance

func CalcLineSegmentPointProjectionDistance(line LineSegment, point Point) (projection Point, distance float64)

CalcLineSegmentPointProjectionDistance 计算点到线段的投影点以及点到投影点的距离

func CalcPolygonCentroid

func CalcPolygonCentroid(polygon Polygon) Point

CalcPolygonCentroid 计算多边形质心

func CalcPolygonCircumscribedCircleCenter

func CalcPolygonCircumscribedCircleCenter(polygon Polygon) Point

CalcPolygonCircumscribedCircleCenter 计算多边形外接圆的圆心

func CalcPolygonPointProjection

func CalcPolygonPointProjection(polygon Polygon, point Point) (projection Point, distance float64)

CalcPolygonPointProjection 给定一个点和一个多边形,计算多边形边界上与该点距离最短的点,并返回投影点和距离

func CalcPolygonVerticesCentroid

func CalcPolygonVerticesCentroid(polygon Polygon) Point

CalcPolygonVerticesCentroid 基于多边形的顶点的平均值计算质心

func CalcRectangleVerticesCentroid

func CalcRectangleVerticesCentroid(rectangle Polygon) Point

CalcRectangleVerticesCentroid 基于矩形的顶点的平均值计算质心

func NewPoint

func NewPoint[V constraints.Number](x, y V) Point

NewPoint 创建一个点

type Polygon

type Polygon []Point

Polygon 由至少三个点组成的多边形

func NewPolygon

func NewPolygon(points ...Point) Polygon

NewPolygon 创建一个多边形

func (Polygon) Centroid

func (p Polygon) Centroid() Point

Centroid 计算多边形质心

func (Polygon) CircumscribedCircleCenter

func (p Polygon) CircumscribedCircleCenter() Point

CircumscribedCircleCenter 计算多边形外接圆的圆心

func (Polygon) CircumscribedCircleRadius

func (p Polygon) CircumscribedCircleRadius() float64

CircumscribedCircleRadius 计算多边形外接圆的半径

func (Polygon) CircumscribedCircleRadiusWithVerticesCentroid

func (p Polygon) CircumscribedCircleRadiusWithVerticesCentroid() float64

CircumscribedCircleRadiusWithVerticesCentroid 基于多边形的顶点平均值计算的质心计算多边形外接圆的半径

func (Polygon) GetEdges

func (p Polygon) GetEdges() []LineSegment

GetEdges 获取多边形的边

func (Polygon) IsPointInside

func (p Polygon) IsPointInside(point Point) bool

IsPointInside 判断点是否在多边形内

func (Polygon) IsPointOnEdge

func (p Polygon) IsPointOnEdge(point Point) bool

IsPointOnEdge 判断点是否在多边形的边上

func (Polygon) VerticesCentroid

func (p Polygon) VerticesCentroid() Point

VerticesCentroid 基于多边形的顶点的平均值计算质心

type Position

type Position = Point

Position 位置,等同于 Point

func NewPosition

func NewPosition[V constraints.Number](x, y V) Position

NewPosition 创建一个位置

type PreviewBoard2D

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

PreviewBoard2D 用于调试等行为的预览画板,该画板生成一个简单的 HTML 页面,该页面包含一个 canvas 元素,可以在该画板上绘制图形。 另外也会根据 EventStream 的事件流来试试更新画板上的图形

func NewPreviewBoard2D

func NewPreviewBoard2D(fps, width, height int) *PreviewBoard2D

NewPreviewBoard2D 创建一个 2D 预览画板

func (*PreviewBoard2D) Start

func (b *PreviewBoard2D) Start(addr string) error

func (*PreviewBoard2D) Stop

func (b *PreviewBoard2D) Stop()

func (*PreviewBoard2D) Update

func (b *PreviewBoard2D) Update(vec2 ...Vector)

type Vector

type Vector []float64

func NewVector

func NewVector[T constraints.Number](v ...T) Vector

NewVector 创建一个向量

func (Vector) Add

func (v Vector) Add(v2 Vector) Vector

Add 向量相加

func (Vector) Angle

func (v Vector) Angle(v2 Vector) float64

Angle 向量夹角

func (Vector) Clone

func (v Vector) Clone() Vector

Clone 复制一个向量

func (Vector) Cross3D

func (v Vector) Cross3D(v2 Vector3) Vector

Cross3D 对三维向量进行叉乘

func (Vector) Div

func (v Vector) Div(scalar float64) Vector

Div 向量除法

func (Vector) Dot

func (v Vector) Dot(v2 Vector) float64

Dot 向量点乘

func (Vector) Equal

func (v Vector) Equal(v2 Vector) bool

Equal 判断两个向量是否相等

func (Vector) IsOrthogonal

func (v Vector) IsOrthogonal(v2 Vector) bool

IsOrthogonal 判断两个向量是否垂直

func (Vector) IsZero

func (v Vector) IsZero() bool

IsZero 判断向量是否为零向量

func (Vector) Key

func (v Vector) Key() string

Key 返回向量的键值

func (Vector) Length

func (v Vector) Length() float64

Length 向量长度

func (Vector) Mul

func (v Vector) Mul(scalar float64) Vector

Mul 向量数乘

func (Vector) Normalize

func (v Vector) Normalize() Vector

Normalize 向量归一化

func (Vector) ProjectTo

func (v Vector) ProjectTo(v2 Vector) Vector

ProjectTo 向量投影

func (Vector) ReflectBy

func (v Vector) ReflectBy(normal Vector) Vector

ReflectBy 向量反射

func (Vector) RejectFrom

func (v Vector) RejectFrom(v2 Vector) Vector

RejectFrom 向量拒投影

func (Vector) Sub

func (v Vector) Sub(v2 Vector) Vector

Sub 向量相减

type Vector2

type Vector2 = Vector

func CalcOffsetInDirection2D

func CalcOffsetInDirection2D[T constraints.Number](vector Vector2, direction Direction2D, offset T) Vector2

CalcOffsetInDirection2D 计算特定方向上按照指定距离偏移后的坐标

func NewVector2

func NewVector2[T constraints.Number](x, y T) Vector2

NewVector2 创建一个二维向量

func Vector2Zero

func Vector2Zero() Vector2

func (Vector2) Cross2D

func (v Vector2) Cross2D(v2 Vector2) float64

Cross2D 对二维向量进行叉乘

func (Vector2) Distance2D

func (v Vector2) Distance2D(v2 Vector2) float64

Distance2D 计算二维向量之间的距离

func (Vector2) DistanceSquared2D

func (v Vector2) DistanceSquared2D(v2 Vector2) float64

DistanceSquared2D 计算二维向量之间的距离的平方

  • 用于比较距离,避免开方运算

func (Vector2) GetX

func (v Vector2) GetX() float64

GetX 获取 x 坐标

func (Vector2) GetXY

func (v Vector2) GetXY() (x, y float64)

GetXY 返回该点的 x、y 坐标

func (Vector2) GetY

func (v Vector2) GetY() float64

GetY 获取 y 坐标

func (Vector2) PolarAngle

func (v Vector2) PolarAngle(v2 Vector2) float64

PolarAngle 极坐标角度,即点在极坐标系中的角度

func (Vector2) Quadrant

func (v Vector2) Quadrant() int

Quadrant 获取向量所在象限

type Vector3

type Vector3 = Vector

func NewVector3

func NewVector3[T constraints.Number](x, y, z T) Vector3

NewVector3 创建一个三维向量

func Vector3Zero

func Vector3Zero() Vector3

func (Vector3) GetXYZ

func (v Vector3) GetXYZ() (x, y, z float64)

GetXYZ 返回该点的 x、y、z 坐标

func (Vector3) GetZ

func (v Vector3) GetZ() float64

GetZ 获取 z 坐标

func (Vector3) IsParallel

func (v Vector3) IsParallel(v2 Vector3) bool

IsParallel 判断两个向量是否平行

type Vector4

type Vector4 = Vector

func Vector4Zero

func Vector4Zero() Vector4

type Vector5

type Vector5 = Vector

func Vector5Zero

func Vector5Zero() Vector5

type Vector6

type Vector6 = Vector

func Vector6Zero

func Vector6Zero() Vector6

type Vector7

type Vector7 = Vector

func Vector7Zero

func Vector7Zero() Vector7

type Vector8

type Vector8 = Vector

func Vector8Zero

func Vector8Zero() Vector8

type Vector9

type Vector9 = Vector

func Vector9Zero

func Vector9Zero() Vector9

Jump to

Keyboard shortcuts

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