💥 CoordinateArray 更名为 Point
This commit is contained in:
parent
d5884db63e
commit
25f0a068d3
|
@ -13,7 +13,7 @@ type Graph struct {
|
|||
func (slf Graph) Neighbours(point geometry.Point[int]) []geometry.Point[int] {
|
||||
neighbours := make([]geometry.Point[int], 0, 4)
|
||||
for _, direction := range geometry.DirectionUDLR {
|
||||
np := geometry.GetDirectionNextWithCoordinateArray(direction, point)
|
||||
np := geometry.GetDirectionNextWithPoint(direction, point)
|
||||
if slf.IsFree(np) {
|
||||
neighbours = append(neighbours, np)
|
||||
}
|
||||
|
|
|
@ -53,8 +53,8 @@ func GetDirectionNextWithCoordinate[V generic.SignedNumber](direction Direction,
|
|||
return
|
||||
}
|
||||
|
||||
// GetDirectionNextWithCoordinateArray 获取特定方向上的下一个坐标
|
||||
func GetDirectionNextWithCoordinateArray[V generic.SignedNumber](direction Direction, point Point[V]) Point[V] {
|
||||
// GetDirectionNextWithPoint 获取特定方向上的下一个坐标
|
||||
func GetDirectionNextWithPoint[V generic.SignedNumber](direction Direction, point Point[V]) Point[V] {
|
||||
x, y := point.GetXY()
|
||||
switch direction {
|
||||
case DirectionUp:
|
||||
|
|
|
@ -70,8 +70,8 @@ func PointOnLineWithPos[V generic.SignedNumber](width, pos1, pos2, pos V) bool {
|
|||
return PointOnLineWithCoordinate(x1, y1, x2, y2, x, y)
|
||||
}
|
||||
|
||||
// PointOnLineWithCoordinateArray 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
|
||||
func PointOnLineWithCoordinateArray[V generic.SignedNumber](point1, point2, point Point[V]) bool {
|
||||
// PointOnLineWithPoint 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
|
||||
func PointOnLineWithPoint[V generic.SignedNumber](point1, point2, point Point[V]) bool {
|
||||
x1, y1 := point1.GetXY()
|
||||
x2, y2 := point2.GetXY()
|
||||
x, y := point.GetXY()
|
||||
|
@ -93,9 +93,9 @@ func PointOnSegmentWithPos[V generic.SignedNumber](width, pos1, pos2, pos V) boo
|
|||
return x >= x1 && x <= x2 && y >= y1 && y <= y2 && PointOnLineWithCoordinate(x1, y1, x2, y2, x, y)
|
||||
}
|
||||
|
||||
// PointOnSegmentWithCoordinateArray 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
|
||||
// - 与 PointOnLineWithCoordinateArray 不同的是, PointOnSegmentWithCoordinateArray 中会判断线段及点的位置是否正确
|
||||
func PointOnSegmentWithCoordinateArray[V generic.SignedNumber](point1, point2, point Point[V]) bool {
|
||||
// PointOnSegmentWithPoint 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
|
||||
// - 与 PointOnLineWithPoint 不同的是, PointOnSegmentWithPoint 中会判断线段及点的位置是否正确
|
||||
func PointOnSegmentWithPoint[V generic.SignedNumber](point1, point2, point Point[V]) bool {
|
||||
x1, y1 := point1.GetXY()
|
||||
x2, y2 := point2.GetXY()
|
||||
x, y := point.GetXY()
|
||||
|
|
|
@ -31,7 +31,7 @@ func (slf Point[V]) GetXY() (x, y V) {
|
|||
|
||||
// GetPos 返回该点位于特定宽度的二维数组的顺序位置
|
||||
func (slf Point[V]) GetPos(width V) V {
|
||||
return CoordinateArrayToPos(width, slf)
|
||||
return PointToPos(width, slf)
|
||||
}
|
||||
|
||||
// GetOffset 获取偏移后的新坐标
|
||||
|
@ -44,7 +44,7 @@ func (slf Point[V]) Negative() bool {
|
|||
return slf.GetX() < V(0) || slf.GetY() < V(0)
|
||||
}
|
||||
|
||||
// OutOf 返回该点在特定宽高下是否越界
|
||||
// OutOf 返回该点在特定宽高下是否越界f
|
||||
func (slf Point[V]) OutOf(minWidth, minHeight, maxWidth, maxHeight V) bool {
|
||||
return slf.GetX() < minWidth || slf.GetY() < minHeight || slf.GetX() >= maxWidth || slf.GetY() >= maxHeight
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ func (slf Point[V]) Equal(point Point[V]) bool {
|
|||
|
||||
// Copy 复制一个点位置
|
||||
func (slf Point[V]) Copy() Point[V] {
|
||||
return CoordinateArrayCopy(slf)
|
||||
return PointCopy(slf)
|
||||
}
|
||||
|
||||
// NewPointCap 创建一个由 x、y 坐标组成的点,这个点具有一个数据容量
|
||||
|
@ -93,8 +93,8 @@ func (slf PointCap[V, D]) GetData() D {
|
|||
return slf.Data
|
||||
}
|
||||
|
||||
// CoordinateToCoordinateArray 将坐标转换为x、y的坐标数组
|
||||
func CoordinateToCoordinateArray[V generic.SignedNumber](x, y V) Point[V] {
|
||||
// CoordinateToPoint 将坐标转换为x、y的坐标数组
|
||||
func CoordinateToPoint[V generic.SignedNumber](x, y V) Point[V] {
|
||||
return [2]V{x, y}
|
||||
}
|
||||
|
||||
|
@ -104,14 +104,14 @@ func CoordinateToPos[V generic.SignedNumber](width, x, y V) V {
|
|||
return y*width + x
|
||||
}
|
||||
|
||||
// CoordinateArrayToCoordinate 将坐标数组转换为x和y坐标
|
||||
func CoordinateArrayToCoordinate[V generic.SignedNumber](position Point[V]) (x, y V) {
|
||||
// PointToCoordinate 将坐标数组转换为x和y坐标
|
||||
func PointToCoordinate[V generic.SignedNumber](position Point[V]) (x, y V) {
|
||||
return position[0], position[1]
|
||||
}
|
||||
|
||||
// CoordinateArrayToPos 将坐标转换为二维数组的顺序位置
|
||||
// PointToPos 将坐标转换为二维数组的顺序位置
|
||||
// - 需要确保x的取值范围必须小于width,或者将会得到不正确的值
|
||||
func CoordinateArrayToPos[V generic.SignedNumber](width V, xy Point[V]) V {
|
||||
func PointToPos[V generic.SignedNumber](width V, xy Point[V]) V {
|
||||
return CoordinateToPos(width, xy[0], xy[1])
|
||||
}
|
||||
|
||||
|
@ -123,8 +123,8 @@ func PosToCoordinate[V generic.SignedNumber](width, pos V) (x, y V) {
|
|||
return x, y
|
||||
}
|
||||
|
||||
// PosToCoordinateArray 通过宽度将一个二维数组的顺序位置转换为x、y的坐标数组
|
||||
func PosToCoordinateArray[V generic.SignedNumber](width, pos V) Point[V] {
|
||||
// PosToPoint 通过宽度将一个二维数组的顺序位置转换为x、y的坐标数组
|
||||
func PosToPoint[V generic.SignedNumber](width, pos V) Point[V] {
|
||||
return [2]V{V(math.Mod(float64(pos), float64(width))), pos / width}
|
||||
}
|
||||
|
||||
|
@ -138,26 +138,26 @@ func PosToCoordinateY[V generic.SignedNumber](width, pos V) V {
|
|||
return pos / width
|
||||
}
|
||||
|
||||
// CoordinateArrayCopy 复制一个坐标数组
|
||||
func CoordinateArrayCopy[V generic.SignedNumber](position Point[V]) Point[V] {
|
||||
return NewPoint(position[0], position[1])
|
||||
// PointCopy 复制一个坐标数组
|
||||
func PointCopy[V generic.SignedNumber](point Point[V]) Point[V] {
|
||||
return point.Copy()
|
||||
}
|
||||
|
||||
// CoordinateArrayToPosWithMulti 将一组坐标转换为二维数组的顺序位置
|
||||
// PointToPosWithMulti 将一组坐标转换为二维数组的顺序位置
|
||||
// - 需要确保x的取值范围必须小于width,或者将会得到不正确的值
|
||||
func CoordinateArrayToPosWithMulti[V generic.SignedNumber](width V, xys ...Point[V]) []V {
|
||||
var result = make([]V, len(xys), len(xys))
|
||||
for i := 0; i < len(xys); i++ {
|
||||
result[i] = CoordinateArrayToPos(width, xys[i])
|
||||
func PointToPosWithMulti[V generic.SignedNumber](width V, points ...Point[V]) []V {
|
||||
var result = make([]V, len(points), len(points))
|
||||
for i := 0; i < len(points); i++ {
|
||||
result[i] = PointToPos(width, points[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// PosToCoordinateArrayWithMulti 将一组二维数组的顺序位置转换为一组数组坐标
|
||||
func PosToCoordinateArrayWithMulti[V generic.SignedNumber](width V, positions ...V) []Point[V] {
|
||||
// PosToPointWithMulti 将一组二维数组的顺序位置转换为一组数组坐标
|
||||
func PosToPointWithMulti[V generic.SignedNumber](width V, positions ...V) []Point[V] {
|
||||
var result = make([]Point[V], len(positions))
|
||||
for i := 0; i < len(positions); i++ {
|
||||
result[i] = PosToCoordinateArray(width, positions[i])
|
||||
result[i] = PosToPoint(width, positions[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -7,5 +7,5 @@ import (
|
|||
|
||||
func TestNewPoint(t *testing.T) {
|
||||
p := [2]int{1, 1}
|
||||
fmt.Println(CoordinateArrayToPos(9, p))
|
||||
fmt.Println(PointToPos(9, p))
|
||||
}
|
||||
|
|
|
@ -144,11 +144,11 @@ func CoordinateMatrixToPosMatrix[V any](matrix [][]V) (width int, posMatrix []V)
|
|||
return
|
||||
}
|
||||
|
||||
// GetShapeCoverageAreaWithCoordinateArray 通过传入的一组坐标 xys 计算一个图形覆盖的矩形范围
|
||||
func GetShapeCoverageAreaWithCoordinateArray[V generic.SignedNumber](xys ...Point[V]) (left, right, top, bottom V) {
|
||||
// GetShapeCoverageAreaWithPoint 通过传入的一组坐标 points 计算一个图形覆盖的矩形范围
|
||||
func GetShapeCoverageAreaWithPoint[V generic.SignedNumber](points ...Point[V]) (left, right, top, bottom V) {
|
||||
hasLeft, hasTop := false, false
|
||||
for _, xy := range xys {
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
for _, xy := range points {
|
||||
x, y := PointToCoordinate(xy)
|
||||
if x < left || !hasLeft {
|
||||
hasLeft = true
|
||||
left = x
|
||||
|
@ -167,7 +167,7 @@ func GetShapeCoverageAreaWithCoordinateArray[V generic.SignedNumber](xys ...Poin
|
|||
return
|
||||
}
|
||||
|
||||
// GetShapeCoverageAreaWithPos 通过传入的一组坐标 xys 计算一个图形覆盖的矩形范围
|
||||
// GetShapeCoverageAreaWithPos 通过传入的一组坐标 positions 计算一个图形覆盖的矩形范围
|
||||
func GetShapeCoverageAreaWithPos[V generic.SignedNumber](width V, positions ...V) (left, right, top, bottom V) {
|
||||
hasLeft, hasTop := false, false
|
||||
for _, pos := range positions {
|
||||
|
@ -205,12 +205,12 @@ func CoverageAreaBoundless[V generic.SignedNumber](l, r, t, b V) (left, right, t
|
|||
// GenerateShapeOnRectangle 生成一组二维坐标的形状
|
||||
// - 这个形状将被在一个刚好能容纳形状的矩形中表示
|
||||
// - 为 true 的位置表示了形状的每一个点
|
||||
func GenerateShapeOnRectangle[V generic.SignedNumber](xys ...Point[V]) (result []PointCap[V, bool]) {
|
||||
left, r, top, b := GetShapeCoverageAreaWithCoordinateArray(xys...)
|
||||
func GenerateShapeOnRectangle[V generic.SignedNumber](points ...Point[V]) (result []PointCap[V, bool]) {
|
||||
left, r, top, b := GetShapeCoverageAreaWithPoint(points...)
|
||||
_, right, _, bottom := CoverageAreaBoundless(left, r, top, b)
|
||||
w, h := right+1, bottom+1
|
||||
result = make([]PointCap[V, bool], int(w*h))
|
||||
for _, xy := range xys {
|
||||
for _, xy := range points {
|
||||
x, y := xy.GetXY()
|
||||
sx := x - (r - right)
|
||||
sy := y - (b - bottom)
|
||||
|
@ -234,15 +234,15 @@ func GenerateShapeOnRectangle[V generic.SignedNumber](xys ...Point[V]) (result [
|
|||
// GenerateShapeOnRectangleWithCoordinate 生成一组二维坐标的形状
|
||||
// - 这个形状将被在一个刚好能容纳形状的矩形中表示
|
||||
// - 为 true 的位置表示了形状的每一个点
|
||||
func GenerateShapeOnRectangleWithCoordinate[V generic.SignedNumber](xys ...Point[V]) (result [][]bool) {
|
||||
left, r, top, b := GetShapeCoverageAreaWithCoordinateArray(xys...)
|
||||
func GenerateShapeOnRectangleWithCoordinate[V generic.SignedNumber](points ...Point[V]) (result [][]bool) {
|
||||
left, r, top, b := GetShapeCoverageAreaWithPoint(points...)
|
||||
_, right, _, bottom := CoverageAreaBoundless(left, r, top, b)
|
||||
w, h := right+1, bottom+1
|
||||
result = make([][]bool, int(w))
|
||||
for x := V(0); x < w; x++ {
|
||||
result[int(x)] = make([]bool, int(h))
|
||||
}
|
||||
for _, xy := range xys {
|
||||
for _, xy := range points {
|
||||
x, y := xy.GetXY()
|
||||
sx := x - (r - right)
|
||||
sy := y - (b - bottom)
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"github.com/kercylan98/minotaur/utils/geometry"
|
||||
)
|
||||
|
||||
func ExampleGetShapeCoverageAreaWithCoordinateArray() {
|
||||
func ExampleGetShapeCoverageAreaWithPoint() {
|
||||
// # # #
|
||||
// # X #
|
||||
// # X X
|
||||
|
@ -15,7 +15,7 @@ func ExampleGetShapeCoverageAreaWithCoordinateArray() {
|
|||
points = append(points, geometry.NewPoint(2, 1))
|
||||
points = append(points, geometry.NewPoint(2, 2))
|
||||
|
||||
left, right, top, bottom := geometry.GetShapeCoverageAreaWithCoordinateArray(points...)
|
||||
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
|
||||
|
|
|
@ -7,14 +7,14 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestGetShapeCoverageAreaWithCoordinateArray(t *testing.T) {
|
||||
Convey("TestGetShapeCoverageAreaWithCoordinateArray", t, func() {
|
||||
func TestGetShapeCoverageAreaWithPoint(t *testing.T) {
|
||||
Convey("TestGetShapeCoverageAreaWithPoint", t, func() {
|
||||
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.GetShapeCoverageAreaWithCoordinateArray(points...)
|
||||
left, right, top, bottom := geometry.GetShapeCoverageAreaWithPoint(points...)
|
||||
|
||||
So(left, ShouldEqual, 1)
|
||||
So(right, ShouldEqual, 2)
|
||||
|
|
|
@ -87,7 +87,7 @@ func (slf Shape[V]) Contains(point Point[V]) bool {
|
|||
// String 将该形状转换为可视化的字符串进行返回
|
||||
func (slf Shape[V]) String() string {
|
||||
var result string
|
||||
left, right, top, bottom := GetShapeCoverageAreaWithCoordinateArray(slf.Points()...)
|
||||
left, right, top, bottom := GetShapeCoverageAreaWithPoint(slf.Points()...)
|
||||
width := right - left + 1
|
||||
height := bottom - top + 1
|
||||
if !shapeStringHasBorder {
|
||||
|
@ -213,7 +213,7 @@ func (slf Shape[V]) ShapeSearch(options ...ShapeSearchOption) (result []Shape[V]
|
|||
// - [[1 0] [1 1] [1 2]] 和 [[1 1] [1 0] [1 2]] 可以被视为两个图形组合
|
||||
// - 返回的坐标为原始形状的坐标
|
||||
func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result []Shape[V]) {
|
||||
left, right, top, bottom := GetShapeCoverageAreaWithCoordinateArray(slf.Points()...)
|
||||
left, right, top, bottom := GetShapeCoverageAreaWithPoint(slf.Points()...)
|
||||
width := right - left + 1
|
||||
height := bottom - top + 1
|
||||
areaWidth := width + left
|
||||
|
@ -262,7 +262,7 @@ func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result []
|
|||
}
|
||||
|
||||
if opt.rectangle {
|
||||
l, r, t, b := GetShapeCoverageAreaWithCoordinateArray(slf.Points()...)
|
||||
l, r, t, b := GetShapeCoverageAreaWithPoint(slf.Points()...)
|
||||
rs := GenerateShapeOnRectangleWithCoordinate(slf.Points()...)
|
||||
w := r - l + 1
|
||||
h := b - t + 1
|
||||
|
@ -280,7 +280,7 @@ func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result []
|
|||
points := GetRectangleFullPoints(s[0]+1, s[1]+1)
|
||||
find := 0
|
||||
for _, point := range points {
|
||||
px, py := CoordinateArrayToCoordinate(point)
|
||||
px, py := PointToCoordinate(point)
|
||||
ox, oy := px+V(x), py+V(y)
|
||||
if !rs[int(ox)][int(oy)] {
|
||||
find = 0
|
||||
|
@ -313,7 +313,7 @@ func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result []
|
|||
for {
|
||||
next, direction = slice.NextLoop(directions, next)
|
||||
for {
|
||||
directionPoint = GetDirectionNextWithCoordinateArray(direction, directionPoint)
|
||||
directionPoint = GetDirectionNextWithPoint(direction, directionPoint)
|
||||
if px, py := directionPoint.GetXY(); px < 0 || px >= areaWidth || py < 0 || py >= areaHeight {
|
||||
break
|
||||
}
|
||||
|
@ -378,7 +378,7 @@ func (slf Shape[V]) Edges() (edges []Line[V]) {
|
|||
// IsPointOnEdge 检查点是否在该形状的一条边上
|
||||
func (slf Shape[V]) IsPointOnEdge(point Point[V]) bool {
|
||||
for _, edge := range slf.Edges() {
|
||||
if PointOnSegmentWithCoordinateArray(edge.GetStart(), edge.GetEnd(), point) {
|
||||
if PointOnSegmentWithPoint(edge.GetStart(), edge.GetEnd(), point) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -444,7 +444,7 @@ func CalcTriangleTwiceArea[V generic.SignedNumber](a, b, c Point[V]) V {
|
|||
// IsPointOnEdge 检查点是否在 edges 的任意一条边上
|
||||
func IsPointOnEdge[V generic.SignedNumber](edges []Line[V], point Point[V]) bool {
|
||||
for _, edge := range edges {
|
||||
if PointOnSegmentWithCoordinateArray(edge.GetStart(), edge.GetEnd(), point) {
|
||||
if PointOnSegmentWithPoint(edge.GetStart(), edge.GetEnd(), point) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue