支持为形状添加标记
This commit is contained in:
parent
d2a6c5d8c7
commit
e026d38b0c
|
@ -4,13 +4,13 @@ import (
|
||||||
"github.com/kercylan98/minotaur/utils/g2d/shape"
|
"github.com/kercylan98/minotaur/utils/g2d/shape"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MatrixShapeSearchResult struct {
|
type MatrixShapeSearchResult[Mark any] struct {
|
||||||
Shape *shape.Shape
|
Shape *shape.Shape[Mark]
|
||||||
Points []shape.Point
|
Points []shape.Point
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatrixShapeSearchWithYX 二维矩阵形状搜索
|
// 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{}
|
record := map[int]map[int]bool{}
|
||||||
width := len(matrix[0])
|
width := len(matrix[0])
|
||||||
height := len(matrix)
|
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 {
|
for _, s := range shapes {
|
||||||
points := s.GetPoints()
|
points := s.GetPoints()
|
||||||
|
@ -48,7 +48,7 @@ func MatrixShapeSearchWithYX[T any](matrix [][]T, shapes []*shape.Shape, checkMa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if count == len(points) {
|
if count == len(points) {
|
||||||
target := MatrixShapeSearchResult{
|
target := MatrixShapeSearchResult[Mark]{
|
||||||
Shape: s,
|
Shape: s,
|
||||||
}
|
}
|
||||||
for _, point := range points {
|
for _, point := range points {
|
||||||
|
@ -72,7 +72,7 @@ func MatrixShapeSearchWithYX[T any](matrix [][]T, shapes []*shape.Shape, checkMa
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatrixShapeSearchWithXY 二维矩阵形状搜索
|
// 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{}
|
record := map[int]map[int]bool{}
|
||||||
width := len(matrix)
|
width := len(matrix)
|
||||||
height := len(matrix[0])
|
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 {
|
for _, s := range shapes {
|
||||||
points := s.GetPoints()
|
points := s.GetPoints()
|
||||||
|
@ -110,7 +110,7 @@ func MatrixShapeSearchWithXY[T any](matrix [][]T, shapes []*shape.Shape, checkMa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if count == len(points) {
|
if count == len(points) {
|
||||||
target := MatrixShapeSearchResult{
|
target := MatrixShapeSearchResult[Mark]{
|
||||||
Shape: s,
|
Shape: s,
|
||||||
}
|
}
|
||||||
for _, point := range points {
|
for _, point := range points {
|
||||||
|
|
|
@ -1,28 +1,31 @@
|
||||||
package shape
|
package shape
|
||||||
|
|
||||||
func NewShape() *Shape {
|
func NewShape[Mark any](mark Mark, points ...Point) *Shape[Mark] {
|
||||||
shape := &Shape{
|
shape := &Shape[Mark]{
|
||||||
maxX: -1,
|
maxX: -1,
|
||||||
maxY: -1,
|
maxY: -1,
|
||||||
points: map[int]map[int]Point{},
|
points: map[int]map[int]Point{},
|
||||||
|
mark: mark,
|
||||||
}
|
}
|
||||||
|
shape.AddPoints(points...)
|
||||||
return shape
|
return shape
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shape 2D形状定义
|
// Shape 2D形状定义
|
||||||
type Shape struct {
|
type Shape[Mark any] struct {
|
||||||
maxX int
|
maxX int
|
||||||
maxY int
|
maxY int
|
||||||
points map[int]map[int]Point
|
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 {
|
for _, point := range points {
|
||||||
slf.AddPoint(point)
|
slf.AddPoint(point)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *Shape) AddPoint(point Point) {
|
func (slf *Shape[Mark]) AddPoint(point Point) {
|
||||||
x, y := point.GetXY()
|
x, y := point.GetXY()
|
||||||
if x < 0 || y < 0 {
|
if x < 0 || y < 0 {
|
||||||
panic("only positive integers are allowed for shape point positions")
|
panic("only positive integers are allowed for shape point positions")
|
||||||
|
@ -41,7 +44,7 @@ func (slf *Shape) AddPoint(point Point) {
|
||||||
ys[y] = point
|
ys[y] = point
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *Shape) GetPoints() []Point {
|
func (slf *Shape[Mark]) GetPoints() []Point {
|
||||||
var points []Point
|
var points []Point
|
||||||
for _, m := range slf.points {
|
for _, m := range slf.points {
|
||||||
for _, point := range m {
|
for _, point := range m {
|
||||||
|
@ -51,19 +54,23 @@ func (slf *Shape) GetPoints() []Point {
|
||||||
return points
|
return points
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *Shape) GetMaxX() int {
|
func (slf *Shape[Mark]) GetMaxX() int {
|
||||||
return slf.maxX
|
return slf.maxX
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *Shape) GetMaxY() int {
|
func (slf *Shape[Mark]) GetMaxY() int {
|
||||||
return slf.maxY
|
return slf.maxY
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *Shape) GetMaxXY() (int, int) {
|
func (slf *Shape[Mark]) GetMaxXY() (int, int) {
|
||||||
return slf.maxX, slf.maxY
|
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
|
var str string
|
||||||
for y := 0; y <= slf.maxY; y++ {
|
for y := 0; y <= slf.maxY; y++ {
|
||||||
for x := 0; x <= slf.maxX; x++ {
|
for x := 0; x <= slf.maxX; x++ {
|
||||||
|
|
|
@ -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))
|
|
||||||
}
|
|
Loading…
Reference in New Issue