1 Star 0 Fork 0

igo/pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
xgeo.go 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
layte.xiao 提交于 2022-10-15 11:20 . init
package xgeo
import (
"errors"
"github.com/golang/geo/r2"
"github.com/spatial-go/geoos/space"
)
type Polygon struct {
r2.Rect
}
func NewPolygon(coords [][]float64) Polygon {
rect := r2.EmptyRect()
if len(coords) > 0 {
points := make([]r2.Point, 0)
for _, v := range coords {
point := r2.Point{
X: v[0],
Y: v[1],
}
points = append(points, point)
}
rect = r2.RectFromPoints(points...)
}
return Polygon{rect}
}
func (p Polygon) Area() (float64, error) {
size := p.Size()
if size.X == size.Y && size.X < 0 {
return 0, errors.New("polygon area err")
}
return size.X * size.Y, nil
}
func (p Polygon) Centroid() space.Point {
point := p.Center()
return space.Point([]float64{point.X, point.Y})
}
func (p Polygon) IsEmpty() bool {
return p.Rect.IsEmpty()
}
func (p Polygon) Intersection(other Polygon) Polygon {
rect := p.Rect.Intersection(other.Rect)
return Polygon{rect}
}
func (p Polygon) Contains(other Polygon) bool {
return p.Rect.Contains(other.Rect)
}
func (p Polygon) InteriorContains(other Polygon) bool {
return p.Rect.InteriorContains(other.Rect)
}
func (p Polygon) Intersects(other Polygon) bool {
return p.Rect.Intersects(other.Rect)
}
func (p Polygon) InteriorIntersects(other Polygon) bool {
return p.Rect.InteriorIntersects(other.Rect)
}
func (p Polygon) ApproxEqual(other Polygon) bool {
return p.Rect.ApproxEqual(other.Rect)
}
func (p Polygon) Union(other Polygon) Polygon {
rect := p.Rect.Union(other.Rect)
return Polygon{rect}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/igolang/pkg.git
git@gitee.com:igolang/pkg.git
igolang
pkg
pkg
v1.18.1

搜索帮助