函数名称更改,x、y坐标将统一成为 Coordinate,[2]int的数组坐标成为 CoordinateArray,顺序坐标称为 Pos
This commit is contained in:
parent
8086e6a813
commit
c3f4a76ad2
|
@ -1,40 +1,40 @@
|
|||
package g2d
|
||||
|
||||
// PositionToArray 将坐标转换为x、y的数组
|
||||
func PositionToArray(x, y int) [2]int {
|
||||
// CoordinateToCoordinateArray 将坐标转换为x、y的坐标数组
|
||||
func CoordinateToCoordinateArray(x, y int) [2]int {
|
||||
return [2]int{x, y}
|
||||
}
|
||||
|
||||
// PositionArrayToXY 将坐标数组转换为x和y坐标
|
||||
func PositionArrayToXY(position [2]int) (x, y int) {
|
||||
// CoordinateArrayToCoordinate 将坐标数组转换为x和y坐标
|
||||
func CoordinateArrayToCoordinate(position [2]int) (x, y int) {
|
||||
return position[0], position[1]
|
||||
}
|
||||
|
||||
// PositionClone 克隆一个坐标数组
|
||||
func PositionClone(position [2]int) [2]int {
|
||||
// CoordinateArrayClone 克隆一个坐标数组
|
||||
func CoordinateArrayClone(position [2]int) [2]int {
|
||||
return [2]int{position[0], position[1]}
|
||||
}
|
||||
|
||||
// GetAdjacentPositions 获取一个矩阵中,特定位置相邻的最多四个方向的位置
|
||||
func GetAdjacentPositions[T any](matrix [][]T, x, y int) (result [][2]int) {
|
||||
// GetAdjacentCoordinates 获取一个矩阵中,特定位置相邻的最多四个方向的坐标
|
||||
func GetAdjacentCoordinates[T any](matrix [][]T, x, y int) (result [][2]int) {
|
||||
width, height := len(matrix), len(matrix[0])
|
||||
if tx := x - 1; tx >= 0 {
|
||||
result = append(result, PositionToArray(tx, y))
|
||||
result = append(result, CoordinateToCoordinateArray(tx, y))
|
||||
}
|
||||
if tx := x + 1; tx < width {
|
||||
result = append(result, PositionToArray(tx, y))
|
||||
result = append(result, CoordinateToCoordinateArray(tx, y))
|
||||
}
|
||||
if ty := y - 1; ty >= 0 {
|
||||
result = append(result, PositionToArray(x, ty))
|
||||
result = append(result, CoordinateToCoordinateArray(x, ty))
|
||||
}
|
||||
if ty := y + 1; ty < height {
|
||||
result = append(result, PositionToArray(x, ty))
|
||||
result = append(result, CoordinateToCoordinateArray(x, ty))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetAdjacentPositionsWithContinuousPosition 获取一个连续位置的矩阵中,特定位置相邻的最多四个方向的位置
|
||||
func GetAdjacentPositionsWithContinuousPosition[T any](matrix []T, width, pos int) (result []int) {
|
||||
// GetAdjacentCoordinatesWithPos 获取一个连续位置的矩阵中,特定位置相邻的最多四个方向的坐标
|
||||
func GetAdjacentCoordinatesWithPos[T any](matrix []T, width, pos int) (result []int) {
|
||||
size := len(matrix)
|
||||
currentRow := pos / width
|
||||
if up := pos - width; up >= 0 {
|
||||
|
@ -52,41 +52,41 @@ func GetAdjacentPositionsWithContinuousPosition[T any](matrix []T, width, pos in
|
|||
return
|
||||
}
|
||||
|
||||
// PositionToInt 将坐标转换为二维数组的顺序位置
|
||||
// CoordinateToPos 将坐标转换为二维数组的顺序位置坐标
|
||||
// - 需要确保x的取值范围必须小于width,或者将会得到不正确的值
|
||||
func PositionToInt(width, x, y int) int {
|
||||
func CoordinateToPos(width, x, y int) int {
|
||||
return y*width + x
|
||||
}
|
||||
|
||||
// PositionToIntWithArray 将坐标转换为二维数组的顺序位置
|
||||
// CoordinateArrayToPos 将坐标转换为二维数组的顺序位置
|
||||
// - 需要确保x的取值范围必须小于width,或者将会得到不正确的值
|
||||
func PositionToIntWithArray(width int, xy [2]int) int {
|
||||
return PositionToInt(width, xy[0], xy[1])
|
||||
func CoordinateArrayToPos(width int, xy [2]int) int {
|
||||
return CoordinateToPos(width, xy[0], xy[1])
|
||||
}
|
||||
|
||||
// PositionsToIntWithArray 将一组坐标转换为二维数组的顺序位置
|
||||
// CoordinateArrayToPosWithMulti 将一组坐标转换为二维数组的顺序位置
|
||||
// - 需要确保x的取值范围必须小于width,或者将会得到不正确的值
|
||||
func PositionsToIntWithArray(width int, xys ...[2]int) []int {
|
||||
func CoordinateArrayToPosWithMulti(width int, xys ...[2]int) []int {
|
||||
var result = make([]int, len(xys), len(xys))
|
||||
for i := 0; i < len(xys); i++ {
|
||||
result[i] = PositionToIntWithArray(width, xys[i])
|
||||
result[i] = CoordinateArrayToPos(width, xys[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// PositionIntToXY 通过宽度将一个二维数组的顺序位置转换为xy坐标
|
||||
func PositionIntToXY(width, pos int) (x, y int) {
|
||||
// PosToCoordinate 通过宽度将一个二维数组的顺序位置转换为xy坐标
|
||||
func PosToCoordinate(width, pos int) (x, y int) {
|
||||
x = pos % width
|
||||
y = pos / width
|
||||
return x, y
|
||||
}
|
||||
|
||||
// PositionIntGetX 通过宽度将一个二维数组的顺序位置转换为X坐标
|
||||
func PositionIntGetX(width, pos int) int {
|
||||
// PosToCoordinateX 通过宽度将一个二维数组的顺序位置转换为X坐标
|
||||
func PosToCoordinateX(width, pos int) int {
|
||||
return pos % width
|
||||
}
|
||||
|
||||
// PositionIntGetY 通过宽度将一个二维数组的顺序位置转换为Y坐标
|
||||
func PositionIntGetY(width, pos int) int {
|
||||
// PosToCoordinateY 通过宽度将一个二维数组的顺序位置转换为Y坐标
|
||||
func PosToCoordinateY(width, pos int) int {
|
||||
return pos / width
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@ import (
|
|||
)
|
||||
|
||||
func TestPositionIntToXY(t *testing.T) {
|
||||
pos := PositionToInt(9, 7, 8)
|
||||
pos := CoordinateToPos(9, 7, 8)
|
||||
fmt.Println(pos)
|
||||
fmt.Println(PositionIntToXY(9, pos))
|
||||
fmt.Println(PosToCoordinate(9, pos))
|
||||
|
||||
fmt.Println(PositionToInt(65000, 61411, 158266))
|
||||
fmt.Println(PositionIntToXY(65000, 10287351411))
|
||||
fmt.Println(CoordinateToPos(65000, 61411, 158266))
|
||||
fmt.Println(PosToCoordinate(65000, 10287351411))
|
||||
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ func (slf *Matrix[T]) GetMatrix() [][]T {
|
|||
for x := 0; x < slf.w; x++ {
|
||||
ys := make([]T, slf.h)
|
||||
for y := 0; y < slf.h; y++ {
|
||||
ys[y] = slf.m[g2d.PositionToInt(slf.w, x, y)]
|
||||
ys[y] = slf.m[g2d.CoordinateToPos(slf.w, x, y)]
|
||||
}
|
||||
result[x] = ys
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ func (slf *Matrix[T]) GetMatrixWithPos() []T {
|
|||
|
||||
// Get 获取特定坐标的内容
|
||||
func (slf *Matrix[T]) Get(x, y int) (value T) {
|
||||
return slf.m[g2d.PositionToInt(slf.w, x, y)]
|
||||
return slf.m[g2d.CoordinateToPos(slf.w, x, y)]
|
||||
}
|
||||
|
||||
// GetWithPos 获取特定坐标的内容
|
||||
|
@ -65,7 +65,7 @@ func (slf *Matrix[T]) GetWithPos(pos int) (value T) {
|
|||
|
||||
// Set 设置特定坐标的内容
|
||||
func (slf *Matrix[T]) Set(x, y int, data T) {
|
||||
slf.m[g2d.PositionToInt(slf.w, x, y)] = data
|
||||
slf.m[g2d.CoordinateToPos(slf.w, x, y)] = data
|
||||
}
|
||||
|
||||
// SetWithPos 设置特定坐标的内容
|
||||
|
@ -76,7 +76,7 @@ func (slf *Matrix[T]) SetWithPos(pos int, data T) {
|
|||
// Swap 交换两个位置的内容
|
||||
func (slf *Matrix[T]) Swap(x1, y1, x2, y2 int) {
|
||||
a, b := slf.Get(x1, y1), slf.Get(x2, y2)
|
||||
slf.m[g2d.PositionToInt(slf.w, x1, y1)], slf.m[g2d.PositionToInt(slf.w, x2, y2)] = b, a
|
||||
slf.m[g2d.CoordinateToPos(slf.w, x1, y1)], slf.m[g2d.CoordinateToPos(slf.w, x2, y2)] = b, a
|
||||
}
|
||||
|
||||
// SwapWithPos 交换两个位置的内容
|
||||
|
@ -87,8 +87,8 @@ func (slf *Matrix[T]) SwapWithPos(pos1, pos2 int) {
|
|||
|
||||
// TrySwap 尝试交换两个位置的内容,交换后不满足表达式时进行撤销
|
||||
func (slf *Matrix[T]) TrySwap(x1, y1, x2, y2 int, expressionHandle func(matrix *Matrix[T]) bool) {
|
||||
pos1 := g2d.PositionToInt(slf.w, x1, y1)
|
||||
pos2 := g2d.PositionToInt(slf.w, x2, y2)
|
||||
pos1 := g2d.CoordinateToPos(slf.w, x1, y1)
|
||||
pos2 := g2d.CoordinateToPos(slf.w, x2, y2)
|
||||
a, b := slf.Get(x1, y1), slf.Get(x2, y2)
|
||||
slf.m[pos1], slf.m[pos2] = b, a
|
||||
if !expressionHandle(slf) {
|
||||
|
@ -109,7 +109,7 @@ func (slf *Matrix[T]) TrySwapWithPos(pos1, pos2 int, expressionHandle func(matri
|
|||
func (slf *Matrix[T]) FillFull(generateHandle func(x, y int) T) {
|
||||
for x := 0; x < slf.w; x++ {
|
||||
for y := 0; y < slf.h; y++ {
|
||||
slf.m[g2d.PositionToInt(slf.w, x, y)] = generateHandle(x, y)
|
||||
slf.m[g2d.CoordinateToPos(slf.w, x, y)] = generateHandle(x, y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ func NewRadiationPattern[ItemType comparable, Item RadiationPatternItem[ItemType
|
|||
if rp.excludes[item.GetType()] {
|
||||
continue
|
||||
}
|
||||
rp.positions[item.GetGuid()] = PositionToArray(x, y)
|
||||
rp.positions[item.GetGuid()] = CoordinateToCoordinateArray(x, y)
|
||||
rp.searchNeighbour(x, y, synchronization.NewMap[int64, bool](), synchronization.NewMap[int64, bool]())
|
||||
}
|
||||
}
|
||||
|
@ -92,25 +92,25 @@ func (slf *RadiationPattern[ItemType, Item]) Refresh(x, y int, item Item) {
|
|||
|
||||
slf.nils[x][y] = false
|
||||
slf.matrix[x][y] = item
|
||||
slf.positions[item.GetGuid()] = PositionToArray(x, y)
|
||||
slf.positions[item.GetGuid()] = CoordinateToCoordinateArray(x, y)
|
||||
slf.searchNeighbour(x, y, synchronization.NewMap[int64, bool](), synchronization.NewMap[int64, bool]())
|
||||
}
|
||||
|
||||
// RefreshBySwap 通过交换的方式刷新两个成员的辐射信息
|
||||
func (slf *RadiationPattern[ItemType, Item]) RefreshBySwap(x1, y1, x2, y2 int, item1, item2 Item) {
|
||||
var xys = [][2]int{PositionToArray(x1, y1), PositionToArray(x2, y2)}
|
||||
var xys = [][2]int{CoordinateToCoordinateArray(x1, y1), CoordinateToCoordinateArray(x2, y2)}
|
||||
for _, xy := range xys {
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
slf.Remove(x, y)
|
||||
}
|
||||
for i, item := range []Item{item1, item2} {
|
||||
if slf.excludes[item.GetType()] {
|
||||
continue
|
||||
}
|
||||
x, y := PositionArrayToXY(xys[i])
|
||||
x, y := CoordinateArrayToCoordinate(xys[i])
|
||||
slf.nils[x][y] = false
|
||||
slf.matrix[x][y] = item
|
||||
slf.positions[item.GetGuid()] = PositionToArray(x, y)
|
||||
slf.positions[item.GetGuid()] = CoordinateToCoordinateArray(x, y)
|
||||
slf.searchNeighbour(x, y, synchronization.NewMap[int64, bool](), synchronization.NewMap[int64, bool]())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,7 +149,7 @@ func SearchNotRepeatCross(xys ...[2]int) (result [][][2]int) {
|
|||
for _, xy := range xys {
|
||||
var points [][2]int
|
||||
var find = map[int]bool{}
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
// 搜索四个方向
|
||||
|
@ -204,7 +204,7 @@ func SearchNotRepeatCross(xys ...[2]int) (result [][][2]int) {
|
|||
for _, points := range result {
|
||||
var match = true
|
||||
for _, point := range points {
|
||||
x, y := PositionArrayToXY(point)
|
||||
x, y := CoordinateArrayToCoordinate(point)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
if record[x][y] {
|
||||
|
@ -235,7 +235,7 @@ func SearchContainCross(xys ...[2]int) bool {
|
|||
for _, xy := range xys {
|
||||
var points [][2]int
|
||||
var find = map[int]bool{}
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
// 搜索四个方向
|
||||
|
@ -303,7 +303,7 @@ func SearchNotRepeatStraightLine(minLength int, xys ...[2]int) (result [][][2]in
|
|||
for _, xy := range xys {
|
||||
var points [][2]int
|
||||
var find = map[int]bool{}
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
// 搜索四个方向
|
||||
|
@ -362,7 +362,7 @@ func SearchNotRepeatStraightLine(minLength int, xys ...[2]int) (result [][][2]in
|
|||
for _, points := range result {
|
||||
var match = true
|
||||
for _, point := range points {
|
||||
x, y := PositionArrayToXY(point)
|
||||
x, y := CoordinateArrayToCoordinate(point)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
if record[x][y] {
|
||||
|
@ -396,7 +396,7 @@ func SearchContainStraightLine(minLength int, xys ...[2]int) bool {
|
|||
for _, xy := range xys {
|
||||
var points [][2]int
|
||||
var find = map[int]bool{}
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
// 搜索四个方向
|
||||
|
@ -467,7 +467,7 @@ func SearchNotRepeatT(minLength int, xys ...[2]int) (result [][][2]int) {
|
|||
for _, xy := range xys {
|
||||
var points [][2]int
|
||||
var find = map[int]bool{}
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
// 搜索四个方向
|
||||
|
@ -513,7 +513,7 @@ func SearchNotRepeatT(minLength int, xys ...[2]int) (result [][][2]int) {
|
|||
for _, points := range result {
|
||||
var match = true
|
||||
for _, point := range points {
|
||||
x, y := PositionArrayToXY(point)
|
||||
x, y := CoordinateArrayToCoordinate(point)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
if record[x][y] {
|
||||
|
@ -547,7 +547,7 @@ func SearchContainT(minLength int, xys ...[2]int) bool {
|
|||
for _, xy := range xys {
|
||||
var points [][2]int
|
||||
var find = map[int]bool{}
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
// 搜索四个方向
|
||||
|
@ -605,7 +605,7 @@ func SearchNotRepeatRightAngle(minLength int, xys ...[2]int) (result [][][2]int)
|
|||
for _, xy := range xys {
|
||||
var points [][2]int
|
||||
var find = map[int]bool{}
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
// 搜索四个方向
|
||||
|
@ -665,7 +665,7 @@ func SearchNotRepeatRightAngle(minLength int, xys ...[2]int) (result [][][2]int)
|
|||
for _, points := range result {
|
||||
var match = true
|
||||
for _, point := range points {
|
||||
x, y := PositionArrayToXY(point)
|
||||
x, y := CoordinateArrayToCoordinate(point)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
if record[x][y] {
|
||||
|
@ -699,7 +699,7 @@ func SearchContainRightAngle(minLength int, xys ...[2]int) bool {
|
|||
for _, xy := range xys {
|
||||
var points [][2]int
|
||||
var find = map[int]bool{}
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
x = x + (0 - left)
|
||||
y = y + (0 - top)
|
||||
// 搜索四个方向
|
||||
|
@ -783,7 +783,7 @@ func SearchNotRepeatFullRectangle(minWidth, minHeight int, xys ...[2]int) (resul
|
|||
points := GetRectangleFullPoints(s[0]+1, s[1]+1)
|
||||
find := 0
|
||||
for _, point := range points {
|
||||
px, py := PositionArrayToXY(point)
|
||||
px, py := CoordinateArrayToCoordinate(point)
|
||||
ox, oy := px+x, py+y
|
||||
if record[ox][oy] || !rectangleShape[ox][oy] {
|
||||
find = 0
|
||||
|
@ -793,7 +793,7 @@ func SearchNotRepeatFullRectangle(minWidth, minHeight int, xys ...[2]int) (resul
|
|||
}
|
||||
if find == len(points) {
|
||||
for _, point := range points {
|
||||
px, py := PositionArrayToXY(point)
|
||||
px, py := CoordinateArrayToCoordinate(point)
|
||||
record[px+x][py+y] = true
|
||||
}
|
||||
result = append(result, [2][2]int{
|
||||
|
@ -834,7 +834,7 @@ func SearchContainFullRectangle(minWidth, minHeight int, xys ...[2]int) bool {
|
|||
points := GetRectangleFullPoints(s[0]+1, s[1]+1)
|
||||
find := 0
|
||||
for _, point := range points {
|
||||
px, py := PositionArrayToXY(point)
|
||||
px, py := CoordinateArrayToCoordinate(point)
|
||||
ox, oy := px+x, py+y
|
||||
if record[ox][oy] || !rectangleShape[ox][oy] {
|
||||
find = 0
|
||||
|
@ -844,7 +844,7 @@ func SearchContainFullRectangle(minWidth, minHeight int, xys ...[2]int) bool {
|
|||
}
|
||||
if find == len(points) {
|
||||
for _, point := range points {
|
||||
px, py := PositionArrayToXY(point)
|
||||
px, py := CoordinateArrayToCoordinate(point)
|
||||
record[px+x][py+y] = true
|
||||
}
|
||||
return true
|
||||
|
@ -931,7 +931,7 @@ func GenerateShape(xys ...[2]int) [][]bool {
|
|||
m[x] = make([]bool, h)
|
||||
}
|
||||
for _, xy := range xys {
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
m[x-(r-right)][y-(b-bottom)] = true
|
||||
}
|
||||
return m
|
||||
|
@ -953,7 +953,7 @@ func CoverageAreaBoundless(l, r, t, b int) (left, right, top, bottom int) {
|
|||
func GetShapeCoverageArea(xys ...[2]int) (left, right, top, bottom int) {
|
||||
left, top = -1, -1
|
||||
for _, xy := range xys {
|
||||
x, y := PositionArrayToXY(xy)
|
||||
x, y := CoordinateArrayToCoordinate(xy)
|
||||
if x < left || left == -1 {
|
||||
left = x
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue