From e026d38b0c427756547f6f160b4e59fc27d17dfd Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Wed, 31 May 2023 14:00:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=BA=E5=BD=A2=E7=8A=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A0=87=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/g2d/shape.go | 16 ++++++++-------- utils/g2d/shape/shape.go | 27 +++++++++++++++++---------- utils/g2d/shape_test.go | 28 ---------------------------- 3 files changed, 25 insertions(+), 46 deletions(-) delete mode 100644 utils/g2d/shape_test.go diff --git a/utils/g2d/shape.go b/utils/g2d/shape.go index 78a7bc6..85c8ee5 100644 --- a/utils/g2d/shape.go +++ b/utils/g2d/shape.go @@ -4,13 +4,13 @@ import ( "github.com/kercylan98/minotaur/utils/g2d/shape" ) -type MatrixShapeSearchResult struct { - Shape *shape.Shape +type MatrixShapeSearchResult[Mark any] struct { + Shape *shape.Shape[Mark] Points []shape.Point } // MatrixShapeSearchWithYX 二维矩阵形状搜索 -func MatrixShapeSearchWithYX[T any](matrix [][]T, shapes []*shape.Shape, checkMatchHandle func(val T) bool) []MatrixShapeSearchResult { +func MatrixShapeSearchWithYX[T any, Mark any](matrix [][]T, shapes []*shape.Shape[Mark], checkMatchHandle func(val T) bool) []MatrixShapeSearchResult[Mark] { record := map[int]map[int]bool{} width := len(matrix[0]) height := len(matrix) @@ -20,7 +20,7 @@ func MatrixShapeSearchWithYX[T any](matrix [][]T, shapes []*shape.Shape, checkMa } } - var result []MatrixShapeSearchResult + var result []MatrixShapeSearchResult[Mark] for _, s := range shapes { points := s.GetPoints() @@ -48,7 +48,7 @@ func MatrixShapeSearchWithYX[T any](matrix [][]T, shapes []*shape.Shape, checkMa } } if count == len(points) { - target := MatrixShapeSearchResult{ + target := MatrixShapeSearchResult[Mark]{ Shape: s, } for _, point := range points { @@ -72,7 +72,7 @@ func MatrixShapeSearchWithYX[T any](matrix [][]T, shapes []*shape.Shape, checkMa } // MatrixShapeSearchWithXY 二维矩阵形状搜索 -func MatrixShapeSearchWithXY[T any](matrix [][]T, shapes []*shape.Shape, checkMatchHandle func(val T) bool) []MatrixShapeSearchResult { +func MatrixShapeSearchWithXY[T any, Mark any](matrix [][]T, shapes []*shape.Shape[Mark], checkMatchHandle func(val T) bool) []MatrixShapeSearchResult[Mark] { record := map[int]map[int]bool{} width := len(matrix) height := len(matrix[0]) @@ -82,7 +82,7 @@ func MatrixShapeSearchWithXY[T any](matrix [][]T, shapes []*shape.Shape, checkMa } } - var result []MatrixShapeSearchResult + var result []MatrixShapeSearchResult[Mark] for _, s := range shapes { points := s.GetPoints() @@ -110,7 +110,7 @@ func MatrixShapeSearchWithXY[T any](matrix [][]T, shapes []*shape.Shape, checkMa } } if count == len(points) { - target := MatrixShapeSearchResult{ + target := MatrixShapeSearchResult[Mark]{ Shape: s, } for _, point := range points { diff --git a/utils/g2d/shape/shape.go b/utils/g2d/shape/shape.go index 095d891..788ecc3 100644 --- a/utils/g2d/shape/shape.go +++ b/utils/g2d/shape/shape.go @@ -1,28 +1,31 @@ package shape -func NewShape() *Shape { - shape := &Shape{ +func NewShape[Mark any](mark Mark, points ...Point) *Shape[Mark] { + shape := &Shape[Mark]{ maxX: -1, maxY: -1, points: map[int]map[int]Point{}, + mark: mark, } + shape.AddPoints(points...) return shape } // Shape 2D形状定义 -type Shape struct { +type Shape[Mark any] struct { maxX int maxY int points map[int]map[int]Point + mark Mark } -func (slf *Shape) AddPoints(points ...Point) { +func (slf *Shape[Mark]) AddPoints(points ...Point) { for _, point := range points { slf.AddPoint(point) } } -func (slf *Shape) AddPoint(point Point) { +func (slf *Shape[Mark]) AddPoint(point Point) { x, y := point.GetXY() if x < 0 || y < 0 { panic("only positive integers are allowed for shape point positions") @@ -41,7 +44,7 @@ func (slf *Shape) AddPoint(point Point) { ys[y] = point } -func (slf *Shape) GetPoints() []Point { +func (slf *Shape[Mark]) GetPoints() []Point { var points []Point for _, m := range slf.points { for _, point := range m { @@ -51,19 +54,23 @@ func (slf *Shape) GetPoints() []Point { return points } -func (slf *Shape) GetMaxX() int { +func (slf *Shape[Mark]) GetMaxX() int { return slf.maxX } -func (slf *Shape) GetMaxY() int { +func (slf *Shape[Mark]) GetMaxY() int { return slf.maxY } -func (slf *Shape) GetMaxXY() (int, int) { +func (slf *Shape[Mark]) GetMaxXY() (int, int) { return slf.maxX, slf.maxY } -func (slf *Shape) String() string { +func (slf *Shape[Mark]) GetMark() Mark { + return slf.mark +} + +func (slf *Shape[Mark]) String() string { var str string for y := 0; y <= slf.maxY; y++ { for x := 0; x <= slf.maxX; x++ { diff --git a/utils/g2d/shape_test.go b/utils/g2d/shape_test.go deleted file mode 100644 index f6f3e10..0000000 --- a/utils/g2d/shape_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package g2d - -import ( - "fmt" - "github.com/kercylan98/minotaur/utils/g2d/shape" - "testing" -) - -func TestMatrixShape(t *testing.T) { - var m = [][]int{ - {1, 0, 0, 0, 0, 0, 0, 0, 0}, - {1, 1, 1, 0, 0, 0, 0, 0, 0}, - {0, 0, 1, 0, 0, 0, 0, 0, 0}, - {0, 0, 1, 1, 1, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 1, 0, 0, 0, 0, 0, 0, 0}, - {0, 1, 1, 1, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0}, - } - s := shape.NewShape() - s.AddPoints(shape.NewPointWithArrays([][2]int{{0, 0}, {0, 1}, {1, 1}, {2, 1}}...)...) - fmt.Println(s) - result := MatrixShapeSearchWithYX(m, []*shape.Shape{s}, func(val int) bool { - return val == 1 - }) - fmt.Println(len(result)) -}