🔥 移除设计不合理的形状 shape 及 shape/point
This commit is contained in:
parent
03f5a4fbdb
commit
41e0f404b5
|
@ -1,17 +0,0 @@
|
|||
package g2d
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/kercylan98/minotaur/utils/geometry"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPositionIntToXY(t *testing.T) {
|
||||
pos := geometry.CoordinateToPos(9, 7, 8)
|
||||
fmt.Println(pos)
|
||||
fmt.Println(geometry.PosToCoordinate(9, pos))
|
||||
|
||||
fmt.Println(geometry.CoordinateToPos(65000, 61411, 158266))
|
||||
fmt.Println(geometry.PosToCoordinate(65000, 10287351411))
|
||||
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package shape
|
||||
|
||||
func NewPoint(x, y int) Point {
|
||||
return Point{x, y}
|
||||
}
|
||||
|
||||
func NewPointWithArray(arr [2]int) Point {
|
||||
return Point{arr[0], arr[1]}
|
||||
}
|
||||
|
||||
func NewPointWithArrays(arrays ...[2]int) []Point {
|
||||
var points = make([]Point, len(arrays), len(arrays))
|
||||
for i, arr := range arrays {
|
||||
points[i] = NewPointWithArray(arr)
|
||||
}
|
||||
return points
|
||||
}
|
||||
|
||||
type Point [2]int
|
||||
|
||||
func (slf Point) GetX() int {
|
||||
return slf[0]
|
||||
}
|
||||
|
||||
func (slf Point) GetY() int {
|
||||
return slf[1]
|
||||
}
|
||||
|
||||
func (slf Point) GetXY() (int, int) {
|
||||
return slf[0], slf[1]
|
||||
}
|
|
@ -1,86 +0,0 @@
|
|||
package 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[Mark any] struct {
|
||||
maxX int
|
||||
maxY int
|
||||
points map[int]map[int]Point
|
||||
mark Mark
|
||||
}
|
||||
|
||||
func (slf *Shape[Mark]) AddPoints(points ...Point) {
|
||||
for _, point := range points {
|
||||
slf.AddPoint(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")
|
||||
}
|
||||
if x > slf.maxX {
|
||||
slf.maxX = x
|
||||
}
|
||||
if y > slf.maxY {
|
||||
slf.maxY = y
|
||||
}
|
||||
ys, exist := slf.points[x]
|
||||
if !exist {
|
||||
ys = map[int]Point{}
|
||||
slf.points[x] = ys
|
||||
}
|
||||
ys[y] = point
|
||||
}
|
||||
|
||||
func (slf *Shape[Mark]) GetPoints() []Point {
|
||||
var points []Point
|
||||
for _, m := range slf.points {
|
||||
for _, point := range m {
|
||||
points = append(points, point)
|
||||
}
|
||||
}
|
||||
return points
|
||||
}
|
||||
|
||||
func (slf *Shape[Mark]) GetMaxX() int {
|
||||
return slf.maxX
|
||||
}
|
||||
|
||||
func (slf *Shape[Mark]) GetMaxY() int {
|
||||
return slf.maxY
|
||||
}
|
||||
|
||||
func (slf *Shape[Mark]) GetMaxXY() (int, int) {
|
||||
return slf.maxX, slf.maxY
|
||||
}
|
||||
|
||||
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++ {
|
||||
if _, exist := slf.points[x][y]; exist {
|
||||
str += "1"
|
||||
} else {
|
||||
str += "0"
|
||||
}
|
||||
}
|
||||
str += "\r\n"
|
||||
}
|
||||
return str
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package g2d
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetShapeCoverageArea(t *testing.T) {
|
||||
for _, xy := range GetExpressibleRectangleBySize(2, 3, 2, 2) {
|
||||
for y := 0; y < xy[1]+1; y++ {
|
||||
for x := 0; x < xy[0]+1; x++ {
|
||||
fmt.Print("0", " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetExpressibleRectangleBySize(t *testing.T) {
|
||||
for _, xy := range GetExpressibleRectangleBySize(3, 3, 2, 2) {
|
||||
for y := 0; y < xy[1]+1; y++ {
|
||||
for x := 0; x < xy[0]+1; x++ {
|
||||
fmt.Print("0", " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue