From 3a999a6eb38233b6e05110f41526c3a5f9cde968 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Sat, 17 Jun 2023 19:59:33 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=9B=BE=E5=BD=A2=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=94=AF=E6=8C=81=E6=9B=B4=E5=A4=9A=E5=8F=AF=E9=80=89?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/geometry/shape.go | 26 +++++++++++++++++- utils/geometry/shape_search_options.go | 38 +++++++++++++++++++++++--- utils/geometry/shape_test.go | 10 +++++-- 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/utils/geometry/shape.go b/utils/geometry/shape.go index 6511489..b885911 100644 --- a/utils/geometry/shape.go +++ b/utils/geometry/shape.go @@ -49,6 +49,11 @@ func (slf Shape[V]) String() string { // 可通过可选项对搜索结果进行过滤 func (slf Shape[V]) ShapeSearch(options ...ShapeSearchOption) (result []Shape[V]) { opt := &shapeSearchOptions{upperLimit: math.MaxInt} + opt.directionCountUpper = map[Direction]int{} + for _, d := range Directions { + opt.directionCountUpper[d] = math.MaxInt + } + for _, option := range options { option(opt) } @@ -123,6 +128,7 @@ func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result [] var next = -1 var directionPoint = point var links = Shape[V]{point} + var directionCount = map[Direction]int{} for { var direction Direction next, direction = slice.NextLoop(Directions, next) @@ -135,18 +141,36 @@ func (slf Shape[V]) getAllGraphicComposition(opt *shapeSearchOptions) (result [] break } links = append(links, directionPoint) + directionCount[direction]++ pos := directionPoint.GetPos(areaWidth) if _, exist := records[pos]; !exist { result = append(result, Shape[V]{directionPoint}) records[pos] = struct{}{} } + } if direction == DirectionRight { break } directionPoint = point } - result = append(result, links) + + match := true + for _, direction := range Directions { + c := directionCount[direction] + if c < opt.directionCountLower[direction] || c > opt.directionCountUpper[direction] { + match = false + break + } + } + + if match && opt.directionCount > 0 && len(directionCount) != opt.directionCount { + match = false + } + + if match { + result = append(result, links) + } } return result diff --git a/utils/geometry/shape_search_options.go b/utils/geometry/shape_search_options.go index 7d938fd..71911a2 100644 --- a/utils/geometry/shape_search_options.go +++ b/utils/geometry/shape_search_options.go @@ -1,15 +1,45 @@ package geometry type shapeSearchOptions struct { - lowerLimit int - upperLimit int - sort int - deduplication bool + lowerLimit int + upperLimit int + sort int + deduplication bool + directionCountLower map[Direction]int + directionCountUpper map[Direction]int + directionCount int } // ShapeSearchOption 图形搜索可选项,用于 Shape.ShapeSearch 搜索支持 type ShapeSearchOption func(options *shapeSearchOptions) +// WithShapeSearchDirectionCountLowerLimit 通过限制特定方向数量下限的方式搜索 +func WithShapeSearchDirectionCountLowerLimit(direction Direction, count int) ShapeSearchOption { + return func(options *shapeSearchOptions) { + if options.directionCountLower == nil { + options.directionCountLower = map[Direction]int{} + } + options.directionCountLower[direction] = count + } +} + +// WithShapeSearchDirectionCount 通过限制方向数量的方式搜索 +func WithShapeSearchDirectionCount(count int) ShapeSearchOption { + return func(options *shapeSearchOptions) { + if options.directionCountLower == nil { + options.directionCountLower = map[Direction]int{} + } + options.directionCount = count + } +} + +// WithShapeSearchDirectionCountUpperLimit 通过限制特定方向数量上限的方式搜索 +func WithShapeSearchDirectionCountUpperLimit(direction Direction, count int) ShapeSearchOption { + return func(options *shapeSearchOptions) { + options.directionCountUpper[direction] = count + } +} + // WithShapeSearchDeduplication 通过去重的方式进行搜索 // - 去重方式中每个点仅会被使用一次 func WithShapeSearchDeduplication() ShapeSearchOption { diff --git a/utils/geometry/shape_test.go b/utils/geometry/shape_test.go index b9d1008..dba349c 100644 --- a/utils/geometry/shape_test.go +++ b/utils/geometry/shape_test.go @@ -14,12 +14,18 @@ func TestShape_Search(t *testing.T) { shape = append(shape, geometry.NewPoint(1, 2)) shape = append(shape, geometry.NewPoint(2, 2)) + fmt.Println("形状:") fmt.Println(shape) - shapes := shape.ShapeSearch(geometry.WithShapeSearchAsc(), geometry.WithShapeSearchDeduplication()) + shapes := shape.ShapeSearch( + geometry.WithShapeSearchDesc(), + geometry.WithShapeSearchDeduplication(), + geometry.WithShapeSearchPointCountLowerLimit(3), + geometry.WithShapeSearchDirectionCount(1), + ) for _, shape := range shapes { - fmt.Println("图形", shape.Points()) + fmt.Println("搜索", shape.Points()) fmt.Println(shape) } }