1 Star 0 Fork 0

exlimit / tegola

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
points.go 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
package points
import (
"math"
"github.com/go-spatial/tegola/maths"
)
func SinArea(pts []maths.Pt) (a float64) {
if len(pts) < 3 {
return a
}
for i := range pts[:len(pts)-1] {
a += (pts[i].X * pts[i+1].Y) - (pts[i+1].X * pts[i].Y)
}
return a / 2
}
func Area(pts []maths.Pt) (a float64) {
return math.Abs(SinArea(pts))
}
func Centroid(pts []maths.Pt) (center maths.Pt) {
if len(pts) == 0 {
return center
}
if len(pts) == 1 {
return pts[0]
}
// TODO: Optimize for small rings and lines.
var a, aa, cx, cy float64
for i := range pts[:len(pts)-1] {
pt, npt := pts[i], pts[i+1]
aa = (pt.X * npt.Y) - (npt.X * pt.Y)
a += aa
cx += (pt.X + npt.X) * aa
cy += (pt.Y + npt.Y) * aa
}
cx = cx / (3 * a)
cy = cy / (3 * a)
return maths.Pt{cx, cy}
}
func SlopeIntercept(pt1, pt2 maths.Pt) (m, b float64, defined bool) {
dx := pt2.X - pt1.X
dy := pt2.Y - pt1.Y
if dx == 0 || dy == 0 {
// if dx == 0 then m == 0; and the intercept is y.
// However if the lines are verticle then the slope is not defined.
return 0, pt1.Y, dx != 0
}
m = dy / dx
// b = y - mx
b = pt1.Y - (m * pt1.X)
return m, b, true
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/exlimit/tegola.git
git@gitee.com:exlimit/tegola.git
exlimit
tegola
tegola
v0.10.3

搜索帮助

Bbcd6f05 5694891 0cc6727d 5694891