💥 CoordinateArray 更名为 Point

This commit is contained in:
kercylan98 2023-06-20 15:56:16 +08:00
parent d5884db63e
commit 25f0a068d3
9 changed files with 54 additions and 54 deletions

View File

@ -13,7 +13,7 @@ type Graph struct {
func (slf Graph) Neighbours(point geometry.Point[int]) []geometry.Point[int] { func (slf Graph) Neighbours(point geometry.Point[int]) []geometry.Point[int] {
neighbours := make([]geometry.Point[int], 0, 4) neighbours := make([]geometry.Point[int], 0, 4)
for _, direction := range geometry.DirectionUDLR { for _, direction := range geometry.DirectionUDLR {
np := geometry.GetDirectionNextWithCoordinateArray(direction, point) np := geometry.GetDirectionNextWithPoint(direction, point)
if slf.IsFree(np) { if slf.IsFree(np) {
neighbours = append(neighbours, np) neighbours = append(neighbours, np)
} }

View File

@ -53,8 +53,8 @@ func GetDirectionNextWithCoordinate[V generic.SignedNumber](direction Direction,
return return
} }
// GetDirectionNextWithCoordinateArray 获取特定方向上的下一个坐标 // GetDirectionNextWithPoint 获取特定方向上的下一个坐标
func GetDirectionNextWithCoordinateArray[V generic.SignedNumber](direction Direction, point Point[V]) Point[V] { func GetDirectionNextWithPoint[V generic.SignedNumber](direction Direction, point Point[V]) Point[V] {
x, y := point.GetXY() x, y := point.GetXY()
switch direction { switch direction {
case DirectionUp: case DirectionUp:

View File

@ -70,8 +70,8 @@ func PointOnLineWithPos[V generic.SignedNumber](width, pos1, pos2, pos V) bool {
return PointOnLineWithCoordinate(x1, y1, x2, y2, x, y) return PointOnLineWithCoordinate(x1, y1, x2, y2, x, y)
} }
// PointOnLineWithCoordinateArray 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上 // PointOnLineWithPoint 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
func PointOnLineWithCoordinateArray[V generic.SignedNumber](point1, point2, point Point[V]) bool { func PointOnLineWithPoint[V generic.SignedNumber](point1, point2, point Point[V]) bool {
x1, y1 := point1.GetXY() x1, y1 := point1.GetXY()
x2, y2 := point2.GetXY() x2, y2 := point2.GetXY()
x, y := point.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) return x >= x1 && x <= x2 && y >= y1 && y <= y2 && PointOnLineWithCoordinate(x1, y1, x2, y2, x, y)
} }
// PointOnSegmentWithCoordinateArray 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上 // PointOnSegmentWithPoint 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上
// - 与 PointOnLineWithCoordinateArray 不同的是, PointOnSegmentWithCoordinateArray 中会判断线段及点的位置是否正确 // - 与 PointOnLineWithPoint 不同的是, PointOnSegmentWithPoint 中会判断线段及点的位置是否正确
func PointOnSegmentWithCoordinateArray[V generic.SignedNumber](point1, point2, point Point[V]) bool { func PointOnSegmentWithPoint[V generic.SignedNumber](point1, point2, point Point[V]) bool {
x1, y1 := point1.GetXY() x1, y1 := point1.GetXY()
x2, y2 := point2.GetXY() x2, y2 := point2.GetXY()
x, y := point.GetXY() x, y := point.GetXY()

View File

@ -31,7 +31,7 @@ func (slf Point[V]) GetXY() (x, y V) {
// GetPos 返回该点位于特定宽度的二维数组的顺序位置 // GetPos 返回该点位于特定宽度的二维数组的顺序位置
func (slf Point[V]) GetPos(width V) V { func (slf Point[V]) GetPos(width V) V {
return CoordinateArrayToPos(width, slf) return PointToPos(width, slf)
} }
// GetOffset 获取偏移后的新坐标 // GetOffset 获取偏移后的新坐标
@ -44,7 +44,7 @@ func (slf Point[V]) Negative() bool {
return slf.GetX() < V(0) || slf.GetY() < V(0) return slf.GetX() < V(0) || slf.GetY() < V(0)
} }
// OutOf 返回该点在特定宽高下是否越界 // OutOf 返回该点在特定宽高下是否越界f
func (slf Point[V]) OutOf(minWidth, minHeight, maxWidth, maxHeight V) bool { func (slf Point[V]) OutOf(minWidth, minHeight, maxWidth, maxHeight V) bool {
return slf.GetX() < minWidth || slf.GetY() < minHeight || slf.GetX() >= maxWidth || slf.GetY() >= maxHeight 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 复制一个点位置 // Copy 复制一个点位置
func (slf Point[V]) Copy() Point[V] { func (slf Point[V]) Copy() Point[V] {
return CoordinateArrayCopy(slf) return PointCopy(slf)
} }
// NewPointCap 创建一个由 x、y 坐标组成的点,这个点具有一个数据容量 // NewPointCap 创建一个由 x、y 坐标组成的点,这个点具有一个数据容量
@ -93,8 +93,8 @@ func (slf PointCap[V, D]) GetData() D {
return slf.Data return slf.Data
} }
// CoordinateToCoordinateArray 将坐标转换为x、y的坐标数组 // CoordinateToPoint 将坐标转换为x、y的坐标数组
func CoordinateToCoordinateArray[V generic.SignedNumber](x, y V) Point[V] { func CoordinateToPoint[V generic.SignedNumber](x, y V) Point[V] {
return [2]V{x, y} return [2]V{x, y}
} }
@ -104,14 +104,14 @@ func CoordinateToPos[V generic.SignedNumber](width, x, y V) V {
return y*width + x return y*width + x
} }
// CoordinateArrayToCoordinate 将坐标数组转换为x和y坐标 // PointToCoordinate 将坐标数组转换为x和y坐标
func CoordinateArrayToCoordinate[V generic.SignedNumber](position Point[V]) (x, y V) { func PointToCoordinate[V generic.SignedNumber](position Point[V]) (x, y V) {
return position[0], position[1] return position[0], position[1]
} }
// CoordinateArrayToPos 将坐标转换为二维数组的顺序位置 // PointToPos 将坐标转换为二维数组的顺序位置
// - 需要确保x的取值范围必须小于width或者将会得到不正确的值 // - 需要确保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]) 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 return x, y
} }
// PosToCoordinateArray 通过宽度将一个二维数组的顺序位置转换为x、y的坐标数组 // PosToPoint 通过宽度将一个二维数组的顺序位置转换为x、y的坐标数组
func PosToCoordinateArray[V generic.SignedNumber](width, pos V) Point[V] { func PosToPoint[V generic.SignedNumber](width, pos V) Point[V] {
return [2]V{V(math.Mod(float64(pos), float64(width))), pos / width} 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 return pos / width
} }
// CoordinateArrayCopy 复制一个坐标数组 // PointCopy 复制一个坐标数组
func CoordinateArrayCopy[V generic.SignedNumber](position Point[V]) Point[V] { func PointCopy[V generic.SignedNumber](point Point[V]) Point[V] {
return NewPoint(position[0], position[1]) return point.Copy()
} }
// CoordinateArrayToPosWithMulti 将一组坐标转换为二维数组的顺序位置 // PointToPosWithMulti 将一组坐标转换为二维数组的顺序位置
// - 需要确保x的取值范围必须小于width或者将会得到不正确的值 // - 需要确保x的取值范围必须小于width或者将会得到不正确的值
func CoordinateArrayToPosWithMulti[V generic.SignedNumber](width V, xys ...Point[V]) []V { func PointToPosWithMulti[V generic.SignedNumber](width V, points ...Point[V]) []V {
var result = make([]V, len(xys), len(xys)) var result = make([]V, len(points), len(points))
for i := 0; i < len(xys); i++ { for i := 0; i < len(points); i++ {
result[i] = CoordinateArrayToPos(width, xys[i]) result[i] = PointToPos(width, points[i])
} }
return result return result
} }
// PosToCoordinateArrayWithMulti 将一组二维数组的顺序位置转换为一组数组坐标 // PosToPointWithMulti 将一组二维数组的顺序位置转换为一组数组坐标
func PosToCoordinateArrayWithMulti[V generic.SignedNumber](width V, positions ...V) []Point[V] { func PosToPointWithMulti[V generic.SignedNumber](width V, positions ...V) []Point[V] {
var result = make([]Point[V], len(positions)) var result = make([]Point[V], len(positions))
for i := 0; i < len(positions); i++ { for i := 0; i < len(positions); i++ {
result[i] = PosToCoordinateArray(width, positions[i]) result[i] = PosToPoint(width, positions[i])
} }
return result return result
} }

View File

@ -7,5 +7,5 @@ import (
func TestNewPoint(t *testing.T) { func TestNewPoint(t *testing.T) {
p := [2]int{1, 1} p := [2]int{1, 1}
fmt.Println(CoordinateArrayToPos(9, p)) fmt.Println(PointToPos(9, p))
} }

View File

@ -144,11 +144,11 @@ func CoordinateMatrixToPosMatrix[V any](matrix [][]V) (width int, posMatrix []V)
return return
} }
// GetShapeCoverageAreaWithCoordinateArray 通过传入的一组坐标 xys 计算一个图形覆盖的矩形范围 // GetShapeCoverageAreaWithPoint 通过传入的一组坐标 points 计算一个图形覆盖的矩形范围
func GetShapeCoverageAreaWithCoordinateArray[V generic.SignedNumber](xys ...Point[V]) (left, right, top, bottom V) { func GetShapeCoverageAreaWithPoint[V generic.SignedNumber](points ...Point[V]) (left, right, top, bottom V) {
hasLeft, hasTop := false, false hasLeft, hasTop := false, false
for _, xy := range xys { for _, xy := range points {
x, y := CoordinateArrayToCoordinate(xy) x, y := PointToCoordinate(xy)
if x < left || !hasLeft { if x < left || !hasLeft {
hasLeft = true hasLeft = true
left = x left = x
@ -167,7 +167,7 @@ func GetShapeCoverageAreaWithCoordinateArray[V generic.SignedNumber](xys ...Poin
return return
} }
// GetShapeCoverageAreaWithPos 通过传入的一组坐标 xys 计算一个图形覆盖的矩形范围 // GetShapeCoverageAreaWithPos 通过传入的一组坐标 positions 计算一个图形覆盖的矩形范围
func GetShapeCoverageAreaWithPos[V generic.SignedNumber](width V, positions ...V) (left, right, top, bottom V) { func GetShapeCoverageAreaWithPos[V generic.SignedNumber](width V, positions ...V) (left, right, top, bottom V) {
hasLeft, hasTop := false, false hasLeft, hasTop := false, false
for _, pos := range positions { for _, pos := range positions {
@ -205,12 +205,12 @@ func CoverageAreaBoundless[V generic.SignedNumber](l, r, t, b V) (left, right, t
// GenerateShapeOnRectangle 生成一组二维坐标的形状 // GenerateShapeOnRectangle 生成一组二维坐标的形状
// - 这个形状将被在一个刚好能容纳形状的矩形中表示 // - 这个形状将被在一个刚好能容纳形状的矩形中表示
// - 为 true 的位置表示了形状的每一个点 // - 为 true 的位置表示了形状的每一个点
func GenerateShapeOnRectangle[V generic.SignedNumber](xys ...Point[V]) (result []PointCap[V, bool]) { func GenerateShapeOnRectangle[V generic.SignedNumber](points ...Point[V]) (result []PointCap[V, bool]) {
left, r, top, b := GetShapeCoverageAreaWithCoordinateArray(xys...) left, r, top, b := GetShapeCoverageAreaWithPoint(points...)
_, right, _, bottom := CoverageAreaBoundless(left, r, top, b) _, right, _, bottom := CoverageAreaBoundless(left, r, top, b)
w, h := right+1, bottom+1 w, h := right+1, bottom+1
result = make([]PointCap[V, bool], int(w*h)) result = make([]PointCap[V, bool], int(w*h))
for _, xy := range xys { for _, xy := range points {
x, y := xy.GetXY() x, y := xy.GetXY()
sx := x - (r - right) sx := x - (r - right)
sy := y - (b - bottom) sy := y - (b - bottom)
@ -234,15 +234,15 @@ func GenerateShapeOnRectangle[V generic.SignedNumber](xys ...Point[V]) (result [
// GenerateShapeOnRectangleWithCoordinate 生成一组二维坐标的形状 // GenerateShapeOnRectangleWithCoordinate 生成一组二维坐标的形状
// - 这个形状将被在一个刚好能容纳形状的矩形中表示 // - 这个形状将被在一个刚好能容纳形状的矩形中表示
// - 为 true 的位置表示了形状的每一个点 // - 为 true 的位置表示了形状的每一个点
func GenerateShapeOnRectangleWithCoordinate[V generic.SignedNumber](xys ...Point[V]) (result [][]bool) { func GenerateShapeOnRectangleWithCoordinate[V generic.SignedNumber](points ...Point[V]) (result [][]bool) {
left, r, top, b := GetShapeCoverageAreaWithCoordinateArray(xys...) left, r, top, b := GetShapeCoverageAreaWithPoint(points...)
_, right, _, bottom := CoverageAreaBoundless(left, r, top, b) _, right, _, bottom := CoverageAreaBoundless(left, r, top, b)
w, h := right+1, bottom+1 w, h := right+1, bottom+1
result = make([][]bool, int(w)) result = make([][]bool, int(w))
for x := V(0); x < w; x++ { for x := V(0); x < w; x++ {
result[int(x)] = make([]bool, int(h)) result[int(x)] = make([]bool, int(h))
} }
for _, xy := range xys { for _, xy := range points {
x, y := xy.GetXY() x, y := xy.GetXY()
sx := x - (r - right) sx := x - (r - right)
sy := y - (b - bottom) sy := y - (b - bottom)

View File

@ -5,7 +5,7 @@ import (
"github.com/kercylan98/minotaur/utils/geometry" "github.com/kercylan98/minotaur/utils/geometry"
) )
func ExampleGetShapeCoverageAreaWithCoordinateArray() { func ExampleGetShapeCoverageAreaWithPoint() {
// # # # // # # #
// # X # // # X #
// # X X // # X X
@ -15,7 +15,7 @@ func ExampleGetShapeCoverageAreaWithCoordinateArray() {
points = append(points, geometry.NewPoint(2, 1)) points = append(points, geometry.NewPoint(2, 1))
points = append(points, geometry.NewPoint(2, 2)) 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)) fmt.Println(fmt.Sprintf("left: %v, right: %v, top: %v, bottom: %v", left, right, top, bottom))
// left: 1, right: 2, top: 1, bottom: 2 // left: 1, right: 2, top: 1, bottom: 2

View File

@ -7,14 +7,14 @@ import (
"testing" "testing"
) )
func TestGetShapeCoverageAreaWithCoordinateArray(t *testing.T) { func TestGetShapeCoverageAreaWithPoint(t *testing.T) {
Convey("TestGetShapeCoverageAreaWithCoordinateArray", t, func() { Convey("TestGetShapeCoverageAreaWithPoint", t, func() {
var points []geometry.Point[int] var points []geometry.Point[int]
points = append(points, geometry.NewPoint(1, 1)) points = append(points, geometry.NewPoint(1, 1))
points = append(points, geometry.NewPoint(2, 1)) points = append(points, geometry.NewPoint(2, 1))
points = append(points, geometry.NewPoint(2, 2)) 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(left, ShouldEqual, 1)
So(right, ShouldEqual, 2) So(right, ShouldEqual, 2)

View File

@ -87,7 +87,7 @@ func (slf Shape[V]) Contains(point Point[V]) bool {
// String 将该形状转换为可视化的字符串进行返回 // String 将该形状转换为可视化的字符串进行返回
func (slf Shape[V]) String() string { func (slf Shape[V]) String() string {
var result string var result string
left, right, top, bottom := GetShapeCoverageAreaWithCoordinateArray(slf.Points()...) left, right, top, bottom := GetShapeCoverageAreaWithPoint(slf.Points()...)
width := right - left + 1 width := right - left + 1
height := bottom - top + 1 height := bottom - top + 1
if !shapeStringHasBorder { 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]] 可以被视为两个图形组合 // - [[1 0] [1 1] [1 2]] 和 [[1 1] [1 0] [1 2]] 可以被视为两个图形组合
// - 返回的坐标为原始形状的坐标 // - 返回的坐标为原始形状的坐标
func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result []Shape[V]) { 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 width := right - left + 1
height := bottom - top + 1 height := bottom - top + 1
areaWidth := width + left areaWidth := width + left
@ -262,7 +262,7 @@ func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result []
} }
if opt.rectangle { if opt.rectangle {
l, r, t, b := GetShapeCoverageAreaWithCoordinateArray(slf.Points()...) l, r, t, b := GetShapeCoverageAreaWithPoint(slf.Points()...)
rs := GenerateShapeOnRectangleWithCoordinate(slf.Points()...) rs := GenerateShapeOnRectangleWithCoordinate(slf.Points()...)
w := r - l + 1 w := r - l + 1
h := b - t + 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) points := GetRectangleFullPoints(s[0]+1, s[1]+1)
find := 0 find := 0
for _, point := range points { for _, point := range points {
px, py := CoordinateArrayToCoordinate(point) px, py := PointToCoordinate(point)
ox, oy := px+V(x), py+V(y) ox, oy := px+V(x), py+V(y)
if !rs[int(ox)][int(oy)] { if !rs[int(ox)][int(oy)] {
find = 0 find = 0
@ -313,7 +313,7 @@ func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result []
for { for {
next, direction = slice.NextLoop(directions, next) next, direction = slice.NextLoop(directions, next)
for { for {
directionPoint = GetDirectionNextWithCoordinateArray(direction, directionPoint) directionPoint = GetDirectionNextWithPoint(direction, directionPoint)
if px, py := directionPoint.GetXY(); px < 0 || px >= areaWidth || py < 0 || py >= areaHeight { if px, py := directionPoint.GetXY(); px < 0 || px >= areaWidth || py < 0 || py >= areaHeight {
break break
} }
@ -378,7 +378,7 @@ func (slf Shape[V]) Edges() (edges []Line[V]) {
// IsPointOnEdge 检查点是否在该形状的一条边上 // IsPointOnEdge 检查点是否在该形状的一条边上
func (slf Shape[V]) IsPointOnEdge(point Point[V]) bool { func (slf Shape[V]) IsPointOnEdge(point Point[V]) bool {
for _, edge := range slf.Edges() { for _, edge := range slf.Edges() {
if PointOnSegmentWithCoordinateArray(edge.GetStart(), edge.GetEnd(), point) { if PointOnSegmentWithPoint(edge.GetStart(), edge.GetEnd(), point) {
return true return true
} }
} }
@ -444,7 +444,7 @@ func CalcTriangleTwiceArea[V generic.SignedNumber](a, b, c Point[V]) V {
// IsPointOnEdge 检查点是否在 edges 的任意一条边上 // IsPointOnEdge 检查点是否在 edges 的任意一条边上
func IsPointOnEdge[V generic.SignedNumber](edges []Line[V], point Point[V]) bool { func IsPointOnEdge[V generic.SignedNumber](edges []Line[V], point Point[V]) bool {
for _, edge := range edges { for _, edge := range edges {
if PointOnSegmentWithCoordinateArray(edge.GetStart(), edge.GetEnd(), point) { if PointOnSegmentWithPoint(edge.GetStart(), edge.GetEnd(), point) {
return true return true
} }
} }