Documentation ¶
Overview ¶
Package geometry 旨在提供一组用于处理几何形状和计算几何属性的函数和数据结构。该包旨在简化几何计算的过程,并提供一致的接口和易于使用的功能。 主要特性:
- 几何形状:"geometry"包支持处理各种几何形状,如点、线、多边形和圆等。您可以使用这些形状来表示和操作实际世界中的几何对象。
- 几何计算:该包提供了一系列函数,用于执行常见的几何计算,如计算两点之间的距离、计算线段的长度、计算多边形的面积等。这些函数旨在提供高效和准确的计算结果。
- 坐标转换:"geometry"包还提供了一些函数,用于在不同坐标系之间进行转换。您可以将点从笛卡尔坐标系转换为极坐标系,或者从二维坐标系转换为三维坐标系等。
- 简化接口:该包的设计目标之一是提供简化的接口,使几何计算变得更加直观和易于使用。您可以轻松地创建和操作几何对象,而无需处理繁琐的底层细节。
Index ¶
- Constants
- Variables
- func CalcAngle[V generic.SignedNumber](x1, y1, x2, y2 V) V
- func CalcAngleDifference[V generic.SignedNumber](angleA, angleB V) V
- func CalcBoundingRadius[V generic.SignedNumber](shape Shape[V]) V
- func CalcBoundingRadiusWithCentroid[V generic.SignedNumber](shape Shape[V], centroid Point[V]) V
- func CalcCircleCentroidDistance[V generic.SignedNumber](circle1, circle2 Circle[V]) V
- func CalcDistanceSquared[V generic.SignedNumber](x1, y1, x2, y2 V) V
- func CalcDistanceWithCoordinate[V generic.SignedNumber](x1, y1, x2, y2 V) V
- func CalcDistanceWithPoint[V generic.SignedNumber](point1, point2 Point[V]) V
- func CalcLineSegmentIntercept[V generic.SignedNumber](line LineSegment[V]) V
- func CalcLineSegmentIsCollinear[V generic.SignedNumber](line1, line2 LineSegment[V], tolerance V) bool
- func CalcLineSegmentIsIntersect[V generic.SignedNumber](line1, line2 LineSegment[V]) bool
- func CalcLineSegmentSlope[V generic.SignedNumber](line LineSegment[V]) V
- func CalcNewCoordinate[V generic.SignedNumber](x, y, angle, distance V) (newX, newY V)
- func CalcRadianWithAngle[V generic.SignedNumber](angle V) V
- func CalcRayIsIntersect[V generic.SignedNumber](x, y, angle V, shape Shape[V]) bool
- func CalcTriangleTwiceArea[V generic.SignedNumber](a, b, c Point[V]) V
- func CoordinateMatrixToPosMatrix[V any](matrix [][]V) (width int, posMatrix []V)
- func CoordinateToPos[V generic.SignedNumber](width, x, y V) V
- func CoverageAreaBoundless[V generic.SignedNumber](l, r, t, b V) (left, right, top, bottom V)
- func DoublePointToCoordinate[V generic.SignedNumber](point1, point2 Point[V]) (x1, y1, x2, y2 V)
- func GenerateShapeOnRectangleWithCoordinate[V generic.SignedNumber](points ...Point[V]) (result [][]bool)
- func GetAdjacentDiagonalsPos[T any, P generic.SignedNumber](matrix []T, width, pos P) (result []P)
- func GetAdjacentPos[T any, P generic.SignedNumber](matrix []T, width, pos P) (result []P)
- func GetAdjacentTranslatePos[T any, P generic.SignedNumber](matrix []T, width, pos P) (result []P)
- func GetDirectionNextWithCoordinate[V generic.SignedNumber](direction Direction, x, y V) (nx, ny V)
- func GetDirectionNextWithPos[V generic.SignedNumber](direction Direction, width, pos V) V
- func GetRectangleFullPos[V generic.SignedNumber](width, height V) (result []V)
- func GetShapeCoverageAreaWithPoint[V generic.SignedNumber](points ...Point[V]) (left, right, top, bottom V)
- func GetShapeCoverageAreaWithPos[V generic.SignedNumber](width V, positions ...V) (left, right, top, bottom V)
- func IsPointOnEdge[V generic.SignedNumber](edges []LineSegment[V], point Point[V]) bool
- func PointOnLineSegmentWithCoordinate[V generic.SignedNumber](x1, y1, x2, y2, x, y V) bool
- func PointOnLineSegmentWithCoordinateInBounds[V generic.SignedNumber](x1, y1, x2, y2, x, y V) bool
- func PointOnLineSegmentWithPoint[V generic.SignedNumber](point1, point2, point Point[V]) bool
- func PointOnLineSegmentWithPointInBounds[V generic.SignedNumber](point1, point2, point Point[V]) bool
- func PointOnLineSegmentWithPos[V generic.SignedNumber](width, pos1, pos2, pos V) bool
- func PointOnLineSegmentWithPosInBounds[V generic.SignedNumber](width, pos1, pos2, pos V) bool
- func PointToCoordinate[V generic.SignedNumber](position Point[V]) (x, y V)
- func PointToPos[V generic.SignedNumber](width V, xy Point[V]) V
- func PointToPosWithMulti[V generic.SignedNumber](width V, points ...Point[V]) []V
- func PosSameRow[V generic.SignedNumber](width, pos1, pos2 V) bool
- func PosToCoordinate[V generic.SignedNumber](width, pos V) (x, y V)
- func PosToCoordinateX[V generic.SignedNumber](width, pos V) V
- func PosToCoordinateY[V generic.SignedNumber](width, pos V) V
- func SetShapeStringHasBorder()
- func SetShapeStringNotHasBorder()
- type Circle
- type Direction
- type FloorPlan
- type LineSegment
- func CalcLineSegmentIsOverlap[V generic.SignedNumber](line1, line2 LineSegment[V]) (line LineSegment[V], overlap bool)
- func ConvertLineSegmentGeneric[V generic.SignedNumber, TO generic.SignedNumber](line LineSegment[V]) LineSegment[TO]
- func NewLineSegment[V generic.SignedNumber](start, end Point[V]) LineSegment[V]
- type LineSegmentCap
- type Point
- func CalcProjectionPoint[V generic.SignedNumber](line LineSegment[V], point Point[V]) Point[V]
- func CalcRectangleCentroid[V generic.SignedNumber](shape Shape[V]) Point[V]
- func CoordinateToPoint[V generic.SignedNumber](x, y V) Point[V]
- func GetAdjacentCoordinateXY[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
- func GetAdjacentCoordinateYX[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
- func GetAdjacentDiagonalsCoordinateXY[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
- func GetAdjacentDiagonalsCoordinateYX[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
- func GetAdjacentTranslateCoordinateXY[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
- func GetAdjacentTranslateCoordinateYX[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
- func GetDirectionNextWithPoint[V generic.SignedNumber](direction Direction, point Point[V]) Point[V]
- func GetExpressibleRectangle[V generic.SignedNumber](width, height V) (result []Point[V])
- func GetExpressibleRectangleBySize[V generic.SignedNumber](width, height, minWidth, minHeight V) (result []Point[V])
- func GetRectangleFullPoints[V generic.SignedNumber](width, height V) (result []Point[V])
- func GetRectangleFullPointsByXY[V generic.SignedNumber](startX, startY, endX, endY V) (result []Point[V])
- func NewPoint[V generic.SignedNumber](x, y V) Point[V]
- func PointCopy[V generic.SignedNumber](point Point[V]) Point[V]
- func PosToPoint[V generic.SignedNumber](width, pos V) Point[V]
- func PosToPointWithMulti[V generic.SignedNumber](width V, positions ...V) []Point[V]
- func ProjectionPointToShape[V generic.SignedNumber](point Point[V], shape Shape[V]) (Point[V], V)
- func (slf Point[V]) Abs() Point[V]
- func (slf Point[V]) Add(point Point[V]) Point[V]
- func (slf Point[V]) Copy() Point[V]
- func (slf Point[V]) Div(point Point[V]) Point[V]
- func (slf Point[V]) Equal(point Point[V]) bool
- func (slf Point[V]) GetOffset(x, y V) Point[V]
- func (slf Point[V]) GetPos(width V) V
- func (slf Point[V]) GetX() V
- func (slf Point[V]) GetXY() (x, y V)
- func (slf Point[V]) GetY() V
- func (slf Point[V]) Max(point Point[V]) Point[V]
- func (slf Point[V]) Min(point Point[V]) Point[V]
- func (slf Point[V]) Mul(point Point[V]) Point[V]
- func (slf Point[V]) Negative() bool
- func (slf Point[V]) OutOf(minWidth, minHeight, maxWidth, maxHeight V) bool
- func (slf Point[V]) Sub(point Point[V]) Point[V]
- type PointCap
- func GenerateShapeOnRectangle[V generic.SignedNumber](points ...Point[V]) (result []PointCap[V, bool])
- func NewPointCap[V generic.SignedNumber, D any](x, y V) PointCap[V, D]
- func NewPointCapWithData[V generic.SignedNumber, D any](x, y V, data D) PointCap[V, D]
- func NewPointCapWithPoint[V generic.SignedNumber, D any](point Point[V], data D) PointCap[V, D]
- type Shape
- func (slf Shape[V]) Contains(point Point[V]) bool
- func (slf Shape[V]) Edges() (edges []LineSegment[V])
- func (slf Shape[V]) IsPointOnEdge(point Point[V]) bool
- func (slf Shape[V]) PointCount() int
- func (slf Shape[V]) Points() []Point[V]
- func (slf Shape[V]) ShapeSearch(options ...ShapeSearchOption) (result []Shape[V])
- func (slf Shape[V]) String() string
- func (slf Shape[V]) ToCircle() Circle[V]
- type ShapeSearchOption
- func WithShapeSearchAsc() ShapeSearchOption
- func WithShapeSearchDeduplication() ShapeSearchOption
- func WithShapeSearchDesc() ShapeSearchOption
- func WithShapeSearchDirectionCount(count int) ShapeSearchOption
- func WithShapeSearchDirectionCountLowerLimit(direction Direction, count int) ShapeSearchOption
- func WithShapeSearchDirectionCountUpperLimit(direction Direction, count int) ShapeSearchOption
- func WithShapeSearchOppositionDirection(direction Direction) ShapeSearchOption
- func WithShapeSearchPointCountLowerLimit(lowerLimit int) ShapeSearchOption
- func WithShapeSearchPointCountUpperLimit(upperLimit int) ShapeSearchOption
- func WithShapeSearchRectangleLowerLimit(minWidth, minHeight int) ShapeSearchOption
- func WithShapeSearchRectangleUpperLimit(maxWidth, maxHeight int) ShapeSearchOption
- func WithShapeSearchRightAngle() ShapeSearchOption
Examples ¶
Constants ¶
const ( DirectionUnknown = Direction(iota) // 未知 DirectionUp // 上方 DirectionDown // 下方 DirectionLeft // 左方 DirectionRight // 右方 )
Variables ¶
var ( DirectionUDLR = []Direction{DirectionUp, DirectionDown, DirectionLeft, DirectionRight} // 上下左右四个方向的数组 DirectionLRUD = []Direction{DirectionLeft, DirectionRight, DirectionUp, DirectionDown} // 左右上下四个方向的数组 )
Functions ¶
func CalcAngleDifference ¶
func CalcAngleDifference[V generic.SignedNumber](angleA, angleB V) V
CalcAngleDifference 计算两个角度之间的最小角度差
func CalcBoundingRadius ¶
func CalcBoundingRadius[V generic.SignedNumber](shape Shape[V]) V
CalcBoundingRadius 计算多边形转换为圆的半径
func CalcBoundingRadiusWithCentroid ¶
func CalcBoundingRadiusWithCentroid[V generic.SignedNumber](shape Shape[V], centroid Point[V]) V
CalcBoundingRadiusWithCentroid 计算多边形在特定质心下圆的半径
func CalcCircleCentroidDistance ¶
func CalcCircleCentroidDistance[V generic.SignedNumber](circle1, circle2 Circle[V]) V
CalcCircleCentroidDistance 计算两个圆质心距离
func CalcDistanceSquared ¶
func CalcDistanceSquared[V generic.SignedNumber](x1, y1, x2, y2 V) V
CalcDistanceSquared 计算两点之间的平方距离
- 这个函数的主要用途是在需要计算两点之间距离的情况下,但不需要得到实际的距离值,而只需要比较距离大小。因为平方根运算相对较为耗时,所以在只需要比较大小的情况下,通常会使用平方距离。
func CalcDistanceWithCoordinate ¶
func CalcDistanceWithCoordinate[V generic.SignedNumber](x1, y1, x2, y2 V) V
CalcDistanceWithCoordinate 计算两点之间的距离
func CalcDistanceWithPoint ¶
func CalcDistanceWithPoint[V generic.SignedNumber](point1, point2 Point[V]) V
CalcDistanceWithPoint 计算两点之间的距离
func CalcLineSegmentIntercept ¶
func CalcLineSegmentIntercept[V generic.SignedNumber](line LineSegment[V]) V
CalcLineSegmentIntercept 计算线段的截距
func CalcLineSegmentIsCollinear ¶
func CalcLineSegmentIsCollinear[V generic.SignedNumber](line1, line2 LineSegment[V], tolerance V) bool
CalcLineSegmentIsCollinear 检查两条线段在一个误差内是否共线
- 共线是指两条线段在同一直线上,即它们的延长线可以重合
func CalcLineSegmentIsIntersect ¶
func CalcLineSegmentIsIntersect[V generic.SignedNumber](line1, line2 LineSegment[V]) bool
CalcLineSegmentIsIntersect 计算两条线段是否相交
func CalcLineSegmentSlope ¶
func CalcLineSegmentSlope[V generic.SignedNumber](line LineSegment[V]) V
CalcLineSegmentSlope 计算线段的斜率
func CalcNewCoordinate ¶
func CalcNewCoordinate[V generic.SignedNumber](x, y, angle, distance V) (newX, newY V)
CalcNewCoordinate 根据给定的x、y坐标、角度和距离计算新的坐标
func CalcRadianWithAngle ¶
func CalcRadianWithAngle[V generic.SignedNumber](angle V) V
CalcRadianWithAngle 根据角度 angle 计算弧度
func CalcRayIsIntersect ¶
func CalcRayIsIntersect[V generic.SignedNumber](x, y, angle V, shape Shape[V]) bool
CalcRayIsIntersect 根据给定的位置和角度生成射线,检测射线是否与多边形发生碰撞
func CalcTriangleTwiceArea ¶
func CalcTriangleTwiceArea[V generic.SignedNumber](a, b, c Point[V]) V
CalcTriangleTwiceArea 计算由 a、b、c 三个点组成的三角形的面积的两倍
func CoordinateMatrixToPosMatrix ¶
CoordinateMatrixToPosMatrix 将二维矩阵转换为顺序的二维矩阵
func CoordinateToPos ¶
func CoordinateToPos[V generic.SignedNumber](width, x, y V) V
CoordinateToPos 将坐标转换为二维数组的顺序位置坐标
- 需要确保x的取值范围必须小于width,或者将会得到不正确的值
func CoverageAreaBoundless ¶
func CoverageAreaBoundless[V generic.SignedNumber](l, r, t, b V) (left, right, top, bottom V)
CoverageAreaBoundless 将一个图形覆盖矩形范围设置为无边的
- 无边化表示会将多余的部分进行裁剪,例如图形左边从 2 开始的时候,那么左边将会被裁剪到从 0 开始
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { // # # # // # X # // # X X // ↓ // X # // X X left, right, top, bottom := geometry.CoverageAreaBoundless(1, 2, 1, 2) fmt.Println(fmt.Sprintf("left: %v, right: %v, top: %v, bottom: %v", left, right, top, bottom)) // left: 0, right: 1, top: 0, bottom: 1 }
Output:
func DoublePointToCoordinate ¶
func DoublePointToCoordinate[V generic.SignedNumber](point1, point2 Point[V]) (x1, y1, x2, y2 V)
DoublePointToCoordinate 将两个位置转换为 x1, y1, x2, y2 的坐标进行返回
func GenerateShapeOnRectangleWithCoordinate ¶
func GenerateShapeOnRectangleWithCoordinate[V generic.SignedNumber](points ...Point[V]) (result [][]bool)
GenerateShapeOnRectangleWithCoordinate 生成一组二维坐标的形状
- 这个形状将被在一个刚好能容纳形状的矩形中表示
- 为 true 的位置表示了形状的每一个点
func GetAdjacentDiagonalsPos ¶
func GetAdjacentDiagonalsPos[T any, P generic.SignedNumber](matrix []T, width, pos P) (result []P)
GetAdjacentDiagonalsPos 获取一个连续位置的矩阵中,特定位置相邻的对角线最多四个方向的位置
func GetAdjacentPos ¶
func GetAdjacentPos[T any, P generic.SignedNumber](matrix []T, width, pos P) (result []P)
GetAdjacentPos 获取一个连续位置的矩阵中,特定位置相邻的最多八个方向的位置
func GetAdjacentTranslatePos ¶
func GetAdjacentTranslatePos[T any, P generic.SignedNumber](matrix []T, width, pos P) (result []P)
GetAdjacentTranslatePos 获取一个连续位置的矩阵中,特定位置相邻的最多四个平移方向(上下左右)的位置
func GetDirectionNextWithCoordinate ¶
func GetDirectionNextWithCoordinate[V generic.SignedNumber](direction Direction, x, y V) (nx, ny V)
GetDirectionNextWithCoordinate 获取特定方向上的下一个坐标
func GetDirectionNextWithPos ¶
func GetDirectionNextWithPos[V generic.SignedNumber](direction Direction, width, pos V) V
GetDirectionNextWithPos 获取位置在特定宽度和特定方向上的下一个位置
- 需要注意的是,在左右方向时,当下一个位置不在矩形区域内时,将会返回上一行的末位置或下一行的首位置
func GetRectangleFullPos ¶
func GetRectangleFullPos[V generic.SignedNumber](width, height V) (result []V)
GetRectangleFullPos 获取一个矩形填充满后包含的所有位置
func GetShapeCoverageAreaWithPoint ¶
func GetShapeCoverageAreaWithPoint[V generic.SignedNumber](points ...Point[V]) (left, right, top, bottom V)
GetShapeCoverageAreaWithPoint 通过传入的一组坐标 points 计算一个图形覆盖的矩形范围
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { // # # # // # X # // # X X var points []geometry.Point[int] points = append(points, geometry.NewPoint(1, 1)) points = append(points, geometry.NewPoint(2, 1)) points = append(points, geometry.NewPoint(2, 2)) left, right, top, bottom := geometry.GetShapeCoverageAreaWithPoint(points...) fmt.Println(fmt.Sprintf("left: %v, right: %v, top: %v, bottom: %v", left, right, top, bottom)) // left: 1, right: 2, top: 1, bottom: 2 }
Output:
func GetShapeCoverageAreaWithPos ¶
func GetShapeCoverageAreaWithPos[V generic.SignedNumber](width V, positions ...V) (left, right, top, bottom V)
GetShapeCoverageAreaWithPos 通过传入的一组坐标 positions 计算一个图形覆盖的矩形范围
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { // # # # 0 1 2 // # X # 3 4 5 // # X X 6 7 8 left, right, top, bottom := geometry.GetShapeCoverageAreaWithPos(3, 4, 7, 8) fmt.Println(fmt.Sprintf("left: %v, right: %v, top: %v, bottom: %v", left, right, top, bottom)) // left: 1, right: 2, top: 1, bottom: 2 }
Output:
func IsPointOnEdge ¶
func IsPointOnEdge[V generic.SignedNumber](edges []LineSegment[V], point Point[V]) bool
IsPointOnEdge 检查点是否在 edges 的任意一条边上
func PointOnLineSegmentWithCoordinate ¶
func PointOnLineSegmentWithCoordinate[V generic.SignedNumber](x1, y1, x2, y2, x, y V) bool
PointOnLineSegmentWithCoordinate 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
func PointOnLineSegmentWithCoordinateInBounds ¶
func PointOnLineSegmentWithCoordinateInBounds[V generic.SignedNumber](x1, y1, x2, y2, x, y V) bool
PointOnLineSegmentWithCoordinateInBounds 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
- 与 PointOnLineSegmentWithCoordinate 不同的是, PointOnLineSegmentWithCoordinateInBounds 中会判断线段及点的位置是否正确
func PointOnLineSegmentWithPoint ¶
func PointOnLineSegmentWithPoint[V generic.SignedNumber](point1, point2, point Point[V]) bool
PointOnLineSegmentWithPoint 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
func PointOnLineSegmentWithPointInBounds ¶
func PointOnLineSegmentWithPointInBounds[V generic.SignedNumber](point1, point2, point Point[V]) bool
PointOnLineSegmentWithPointInBounds 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
- 与 PointOnLineSegmentWithPoint 不同的是, PointOnLineSegmentWithPointInBounds 中会判断线段及点的位置是否正确
func PointOnLineSegmentWithPos ¶
func PointOnLineSegmentWithPos[V generic.SignedNumber](width, pos1, pos2, pos V) bool
PointOnLineSegmentWithPos 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
func PointOnLineSegmentWithPosInBounds ¶
func PointOnLineSegmentWithPosInBounds[V generic.SignedNumber](width, pos1, pos2, pos V) bool
PointOnLineSegmentWithPosInBounds 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
- 与 PointOnLineSegmentWithPos 不同的是, PointOnLineSegmentWithPosInBounds 中会判断线段及点的位置是否正确
func PointToCoordinate ¶
func PointToCoordinate[V generic.SignedNumber](position Point[V]) (x, y V)
PointToCoordinate 将坐标数组转换为x和y坐标
func PointToPos ¶
func PointToPos[V generic.SignedNumber](width V, xy Point[V]) V
PointToPos 将坐标转换为二维数组的顺序位置
- 需要确保x的取值范围必须小于width,或者将会得到不正确的值
func PointToPosWithMulti ¶
func PointToPosWithMulti[V generic.SignedNumber](width V, points ...Point[V]) []V
PointToPosWithMulti 将一组坐标转换为二维数组的顺序位置
- 需要确保x的取值范围必须小于width,或者将会得到不正确的值
func PosSameRow ¶
func PosSameRow[V generic.SignedNumber](width, pos1, pos2 V) bool
PosSameRow 返回两个顺序位置在同一宽度是否位于同一行
func PosToCoordinate ¶
func PosToCoordinate[V generic.SignedNumber](width, pos V) (x, y V)
PosToCoordinate 通过宽度将一个二维数组的顺序位置转换为xy坐标
func PosToCoordinateX ¶
func PosToCoordinateX[V generic.SignedNumber](width, pos V) V
PosToCoordinateX 通过宽度将一个二维数组的顺序位置转换为X坐标
func PosToCoordinateY ¶
func PosToCoordinateY[V generic.SignedNumber](width, pos V) V
PosToCoordinateY 通过宽度将一个二维数组的顺序位置转换为Y坐标
func SetShapeStringHasBorder ¶
func SetShapeStringHasBorder()
SetShapeStringHasBorder 设置 Shape.String 是拥有边界的
func SetShapeStringNotHasBorder ¶
func SetShapeStringNotHasBorder()
SetShapeStringNotHasBorder 设置 Shape.String 是没有边界的
Types ¶
type Circle ¶
type Circle[V generic.SignedNumber] struct { Shape[V] }
Circle 圆形
func NewCircle ¶
func NewCircle[V generic.SignedNumber](radius V, points int) Circle[V]
NewCircle 通过传入圆的半径和需要的点数量,生成一个圆
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { fmt.Println(geometry.NewCircle[float64](7, 12)) }
Output: [[0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [7 0] [6.062177826491071 3.4999999999999996] [3.500000000000001 6.06217782649107] [4.28626379701573e-16 7] [-3.4999999999999982 6.062177826491071] [-6.0621778264910695 3.500000000000002] [-7 8.57252759403146e-16] [-6.062177826491071 -3.4999999999999982] [-3.500000000000003 -6.062177826491069] [-1.285879139104719e-15 -7] [3.499999999999995 -6.062177826491074] [6.062177826491069 -3.500000000000003]] # # # # # # # X # # # # # # # # # # # X # # # # # X # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # X # # # # # # # # # # # X # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # X # # # # # # X # # # # # # X # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # X # # # # # # # # # # # X # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # X # # # # # X # # # # # # # # # # # X # # # # # # #
func (Circle[V]) CentroidDistance ¶
CentroidDistance 计算与另一个圆的质心距离
type Direction ¶
type Direction uint8
Direction 方向
func CalcDirection ¶
func CalcDirection[V generic.SignedNumber](x1, y1, x2, y2 V) Direction
CalcDirection 计算点2位于点1的方向
func GetOppositionDirection ¶
GetOppositionDirection 获取特定方向的对立方向
type FloorPlan ¶
type FloorPlan []string
FloorPlan 平面图
func (FloorPlan) IsInBounds ¶
IsInBounds 检查位置是否在边界内
type LineSegment ¶
type LineSegment[V generic.SignedNumber] [2]Point[V]
LineSegment 通过两个点表示一根线段
func CalcLineSegmentIsOverlap ¶
func CalcLineSegmentIsOverlap[V generic.SignedNumber](line1, line2 LineSegment[V]) (line LineSegment[V], overlap bool)
CalcLineSegmentIsOverlap 通过对点进行排序来检查两条共线线段是否重叠,返回重叠线段
func ConvertLineSegmentGeneric ¶
func ConvertLineSegmentGeneric[V generic.SignedNumber, TO generic.SignedNumber](line LineSegment[V]) LineSegment[TO]
ConvertLineSegmentGeneric 转换线段的泛型类型为特定类型
func NewLineSegment ¶
func NewLineSegment[V generic.SignedNumber](start, end Point[V]) LineSegment[V]
NewLineSegment 创建一根线段
func (LineSegment[V]) GetPoints ¶
func (slf LineSegment[V]) GetPoints() [2]Point[V]
GetPoints 获取该线段的两个点
type LineSegmentCap ¶
type LineSegmentCap[V generic.SignedNumber, Data any] struct { LineSegment[V] Data Data }
LineSegmentCap 可以包含一份额外数据的线段
func NewLineSegmentCap ¶
func NewLineSegmentCap[V generic.SignedNumber, Data any](start, end Point[V], data Data) LineSegmentCap[V, Data]
NewLineSegmentCap 创建一根包含数据的线段
func NewLineSegmentCapWithLine ¶
func NewLineSegmentCapWithLine[V generic.SignedNumber, Data any](line LineSegment[V], data Data) LineSegmentCap[V, Data]
NewLineSegmentCapWithLine 通过已有线段创建一根包含数据的线段
func (*LineSegmentCap[V, Data]) GetData ¶
func (slf *LineSegmentCap[V, Data]) GetData() Data
type Point ¶
type Point[V generic.SignedNumber] [2]V
Point 表示了一个由 x、y 坐标组成的点
func CalcProjectionPoint ¶
func CalcProjectionPoint[V generic.SignedNumber](line LineSegment[V], point Point[V]) Point[V]
CalcProjectionPoint 计算一个点到一条线段的最近点(即投影点)的。这个函数接收一个点和一条线段作为输入,线段由两个端点组成。
- 该函数的主要用于需要计算一个点到一条线段的最近点的情况下
func CalcRectangleCentroid ¶
func CalcRectangleCentroid[V generic.SignedNumber](shape Shape[V]) Point[V]
CalcRectangleCentroid 计算矩形质心
- 非多边形质心计算,仅为顶点的平均值 - 该区域中多边形因子的适当质心
func CoordinateToPoint ¶
func CoordinateToPoint[V generic.SignedNumber](x, y V) Point[V]
CoordinateToPoint 将坐标转换为x、y的坐标数组
func GetAdjacentCoordinateXY ¶
func GetAdjacentCoordinateXY[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
GetAdjacentCoordinateXY 获取一个基于 x、y 的二维矩阵中,特定位置相邻的最多八个方向的位置
func GetAdjacentCoordinateYX ¶
func GetAdjacentCoordinateYX[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
GetAdjacentCoordinateYX 获取一个基于 yx 的二维矩阵中,特定位置相邻的最多八个方向的位置
func GetAdjacentDiagonalsCoordinateXY ¶
func GetAdjacentDiagonalsCoordinateXY[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
GetAdjacentDiagonalsCoordinateXY 获取一个基于 x、y 的二维矩阵中,特定位置相邻的对角线最多四个方向的位置
func GetAdjacentDiagonalsCoordinateYX ¶
func GetAdjacentDiagonalsCoordinateYX[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
GetAdjacentDiagonalsCoordinateYX 获取一个基于 tx 的二维矩阵中,特定位置相邻的对角线最多四个方向的位置
func GetAdjacentTranslateCoordinateXY ¶
func GetAdjacentTranslateCoordinateXY[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
GetAdjacentTranslateCoordinateXY 获取一个基于 x、y 的二维矩阵中,特定位置相邻的最多四个平移方向(上下左右)的位置
func GetAdjacentTranslateCoordinateYX ¶
func GetAdjacentTranslateCoordinateYX[T any, P generic.SignedNumber](matrix [][]T, x, y P) (result []Point[P])
GetAdjacentTranslateCoordinateYX 获取一个基于 y、x 的二维矩阵中,特定位置相邻的最多四个平移方向(上下左右)的位置
func GetDirectionNextWithPoint ¶
func GetDirectionNextWithPoint[V generic.SignedNumber](direction Direction, point Point[V]) Point[V]
GetDirectionNextWithPoint 获取特定方向上的下一个坐标
func GetExpressibleRectangle ¶
func GetExpressibleRectangle[V generic.SignedNumber](width, height V) (result []Point[V])
GetExpressibleRectangle 获取一个宽高可表达的所有矩形形状
- 返回值表示了每一个矩形右下角的x,y位置(左上角始终为0, 0)
- 矩形尺寸由大到小
func GetExpressibleRectangleBySize ¶
func GetExpressibleRectangleBySize[V generic.SignedNumber](width, height, minWidth, minHeight V) (result []Point[V])
GetExpressibleRectangleBySize 获取一个宽高可表达的所有特定尺寸以上的矩形形状
- 返回值表示了每一个矩形右下角的x,y位置(左上角始终为0, 0)
- 矩形尺寸由大到小
func GetRectangleFullPoints ¶
func GetRectangleFullPoints[V generic.SignedNumber](width, height V) (result []Point[V])
GetRectangleFullPoints 获取一个矩形填充满后包含的所有点
func GetRectangleFullPointsByXY ¶
func GetRectangleFullPointsByXY[V generic.SignedNumber](startX, startY, endX, endY V) (result []Point[V])
GetRectangleFullPointsByXY 通过开始结束坐标获取一个矩形包含的所有点
- 例如 1,1 到 2,2 的矩形结果为 1,1 2,1 1,2 2,2
func PosToPoint ¶
func PosToPoint[V generic.SignedNumber](width, pos V) Point[V]
PosToPoint 通过宽度将一个二维数组的顺序位置转换为x、y的坐标数组
func PosToPointWithMulti ¶
func PosToPointWithMulti[V generic.SignedNumber](width V, positions ...V) []Point[V]
PosToPointWithMulti 将一组二维数组的顺序位置转换为一组数组坐标
func ProjectionPointToShape ¶
func ProjectionPointToShape[V generic.SignedNumber](point Point[V], shape Shape[V]) (Point[V], V)
ProjectionPointToShape 将一个点投影到一个多边形上,找到离该点最近的投影点,并返回投影点和距离
type PointCap ¶
type PointCap[V generic.SignedNumber, D any] struct { Point[V] Data D }
PointCap 表示了一个由 x、y 坐标组成的点,这个点具有一个数据容量
func GenerateShapeOnRectangle ¶
func GenerateShapeOnRectangle[V generic.SignedNumber](points ...Point[V]) (result []PointCap[V, bool])
GenerateShapeOnRectangle 生成一组二维坐标的形状
- 这个形状将被在一个刚好能容纳形状的矩形中表示
- 为 true 的位置表示了形状的每一个点
func NewPointCap ¶
func NewPointCap[V generic.SignedNumber, D any](x, y V) PointCap[V, D]
NewPointCap 创建一个由 x、y 坐标组成的点,这个点具有一个数据容量
func NewPointCapWithData ¶
func NewPointCapWithData[V generic.SignedNumber, D any](x, y V, data D) PointCap[V, D]
NewPointCapWithData 通过设置数据的方式创建一个由 x、y 坐标组成的点,这个点具有一个数据容量
func NewPointCapWithPoint ¶
func NewPointCapWithPoint[V generic.SignedNumber, D any](point Point[V], data D) PointCap[V, D]
NewPointCapWithPoint 通过设置数据的方式创建一个由已有坐标组成的点,这个点具有一个数据容量
type Shape ¶
type Shape[V generic.SignedNumber] []Point[V]
Shape 通过多个点表示了一个形状
func NewShape ¶
func NewShape[V generic.SignedNumber](points ...Point[V]) Shape[V]
NewShape 通过多个点生成一个形状进行返回
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { shape := geometry.NewShape[int]( geometry.NewPoint(3, 0), geometry.NewPoint(3, 1), geometry.NewPoint(3, 2), geometry.NewPoint(3, 3), geometry.NewPoint(4, 3), ) fmt.Println(shape) }
Output: [[3 0] [3 1] [3 2] [3 3] [4 3]] X # X # X # X X
func NewShapeWithString ¶
func NewShapeWithString[V generic.SignedNumber](rows []string, point rune) (shape Shape[V])
NewShapeWithString 通过字符串将指定 rune 转换为点位置生成形状进行返回
- 每个点的顺序从上到下,从左到右
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { shape := geometry.NewShapeWithString[int]([]string{ "###X###", "###X###", "###X###", "###XX##", }, 'X') fmt.Println(shape) }
Output: [[3 0] [3 1] [3 2] [3 3] [4 3]] X # X # X # X X
func (Shape[V]) Edges ¶
func (slf Shape[V]) Edges() (edges []LineSegment[V])
Edges 获取该形状每一条边
- 该形状需要最少由3个点组成,否则将不会返回任意一边
func (Shape[V]) IsPointOnEdge ¶
IsPointOnEdge 检查点是否在该形状的一条边上
func (Shape[V]) PointCount ¶
PointCount 获取这个形状的点数量
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { shape := geometry.NewShapeWithString[int]([]string{ "###X###", "##XXX##", }, 'X') fmt.Println(shape.PointCount()) }
Output: 4
func (Shape[V]) Points ¶
Points 获取这个形状的所有点
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { shape := geometry.NewShapeWithString[int]([]string{ "###X###", "##XXX##", }, 'X') points := shape.Points() fmt.Println(points) }
Output: [[3 0] [2 1] [3 1] [4 1]]
func (Shape[V]) ShapeSearch ¶
func (slf Shape[V]) ShapeSearch(options ...ShapeSearchOption) (result []Shape[V])
ShapeSearch 获取该形状中包含的所有图形组合及其位置
- 需要注意的是,即便图形最终表示为相同的,但是只要位置组合顺序不同,那么也将被认定为一种图形组合
- [[1 0] [1 1] [1 2]] 和 [[1 1] [1 0] [1 2]] 可以被视为两个图形组合
- 返回的坐标为原始形状的坐标
可通过可选项对搜索结果进行过滤
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { shape := geometry.NewShapeWithString[int]([]string{ "###X###", "##XXX##", "###X###", }, 'X') shapes := shape.ShapeSearch( geometry.WithShapeSearchDeduplication(), geometry.WithShapeSearchDesc(), ) for _, shape := range shapes { fmt.Println(shape) } }
Output: [[3 0] [3 2] [2 1] [4 1] [3 1]] # X # X X X # X #
func (Shape[V]) String ¶
String 将该形状转换为可视化的字符串进行返回
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/geometry" ) func main() { shape := geometry.NewShapeWithString[int]([]string{ "###X###", "##XXX##", }, 'X') fmt.Println(shape) }
Output: [[3 0] [2 1] [3 1] [4 1]] # X # X X X
type ShapeSearchOption ¶
type ShapeSearchOption func(options *shapeSearchOptions)
ShapeSearchOption 图形搜索可选项,用于 Shape.ShapeSearch 搜索支持
func WithShapeSearchAsc ¶
func WithShapeSearchAsc() ShapeSearchOption
WithShapeSearchAsc 通过升序的方式进行搜索
func WithShapeSearchDeduplication ¶
func WithShapeSearchDeduplication() ShapeSearchOption
WithShapeSearchDeduplication 通过去重的方式进行搜索
- 去重方式中每个点仅会被使用一次
func WithShapeSearchDesc ¶
func WithShapeSearchDesc() ShapeSearchOption
WithShapeSearchDesc 通过降序的方式进行搜索
func WithShapeSearchDirectionCount ¶
func WithShapeSearchDirectionCount(count int) ShapeSearchOption
WithShapeSearchDirectionCount 通过限制方向数量的方式搜索
func WithShapeSearchDirectionCountLowerLimit ¶
func WithShapeSearchDirectionCountLowerLimit(direction Direction, count int) ShapeSearchOption
WithShapeSearchDirectionCountLowerLimit 通过限制特定方向数量下限的方式搜索
func WithShapeSearchDirectionCountUpperLimit ¶
func WithShapeSearchDirectionCountUpperLimit(direction Direction, count int) ShapeSearchOption
WithShapeSearchDirectionCountUpperLimit 通过限制特定方向数量上限的方式搜索
func WithShapeSearchOppositionDirection ¶
func WithShapeSearchOppositionDirection(direction Direction) ShapeSearchOption
WithShapeSearchOppositionDirection 通过限制对立方向的方式搜索
- 对立方向例如上不能与下共存
func WithShapeSearchPointCountLowerLimit ¶
func WithShapeSearchPointCountLowerLimit(lowerLimit int) ShapeSearchOption
WithShapeSearchPointCountLowerLimit 通过限制图形构成的最小点数进行搜索
- 当搜索到的图形的点数量低于 lowerLimit 时,将被忽略
func WithShapeSearchPointCountUpperLimit ¶
func WithShapeSearchPointCountUpperLimit(upperLimit int) ShapeSearchOption
WithShapeSearchPointCountUpperLimit 通过限制图形构成的最大点数进行搜索
- 当搜索到的图形的点数量大于 upperLimit 时,将被忽略
func WithShapeSearchRectangleLowerLimit ¶
func WithShapeSearchRectangleLowerLimit(minWidth, minHeight int) ShapeSearchOption
WithShapeSearchRectangleLowerLimit 通过矩形宽高下限的方式搜索
func WithShapeSearchRectangleUpperLimit ¶
func WithShapeSearchRectangleUpperLimit(maxWidth, maxHeight int) ShapeSearchOption
WithShapeSearchRectangleUpperLimit 通过矩形宽高上限的方式搜索
func WithShapeSearchRightAngle ¶
func WithShapeSearchRightAngle() ShapeSearchOption
WithShapeSearchRightAngle 通过直角的方式进行搜索
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package astar 提供用于实现 A* 算法的函数和数据结构。
|
Package astar 提供用于实现 A* 算法的函数和数据结构。 |
Package dp (DistributionPattern) 提供用于在二维数组中根据不同的特征标记为数组成员建立分布链接的函数和数据结构。
|
Package dp (DistributionPattern) 提供用于在二维数组中根据不同的特征标记为数组成员建立分布链接的函数和数据结构。 |
Package matrix 提供了一个简单的二维数组的实现
|
Package matrix 提供了一个简单的二维数组的实现 |
Package navmesh 提供了用于导航网格处理的函数和数据结构。
|
Package navmesh 提供了用于导航网格处理的函数和数据结构。 |