Documentation
¶
Overview ¶
定义了几何图形质心计算相关函数
Package xy 包含了低维平面(xy)相关的函数。 数据可以是任意维度,但是每个坐标的前两个坐标必须是x,y坐标。所有其他坐标都将被忽略。
Index ¶
- func Angle(p0, p1 geom.Coord) float64
- func AngleBetween(tip1, tail, tip2 geom.Coord) float64
- func AngleBetweenOriented(tip1, tail, tip2 geom.Coord) float64
- func AngleFromOrigin(p geom.Coord) float64
- func AngleOrientation(ang1, ang2 float64) orientation.Type
- func Centroid(geometry geom.T) (centroid geom.Coord, err error)
- func Diff(ang1, ang2 float64) float64
- func Distance(c1, c2 geom.Coord) float64
- func DistanceFromLineToLine(line1Start, line1End, line2Start, line2End geom.Coord) float64
- func DistanceFromPointToLine(p, lineStart, lineEnd geom.Coord) float64
- func DistanceFromPointToLineString(layout geom.Layout, p geom.Coord, line []float64) float64
- func DoLinesOverlap(line1End1, line1End2, line2End1, line2End2 geom.Coord) bool
- func Equal(coords1 []float64, start1 int, coords2 []float64, start2 int) bool
- func InteriorAngle(p0, p1, p2 geom.Coord) float64
- func IsAcute(endpoint1, base, endpoint2 geom.Coord) bool
- func IsObtuse(endpoint1, base, endpoint2 geom.Coord) bool
- func IsOnLine(layout geom.Layout, point geom.Coord, lineSegmentCoordinates []float64) bool
- func IsPointWithinLineBounds(p, lineEndpoint1, lineEndpoint2 geom.Coord) bool
- func IsRingCounterClockwise(layout geom.Layout, ring []float64) bool
- func LinearRingsCentroid(line *geom.LinearRing, extraLines ...*geom.LinearRing) (centroid geom.Coord)
- func LinesCentroid(line *geom.LineString, extraLines ...*geom.LineString) (centroid geom.Coord)
- func MultiLineCentroid(line *geom.MultiLineString) (centroid geom.Coord)
- func MultiPointCentroid(point *geom.MultiPoint) geom.Coord
- func MultiPolygonCentroid(polygon *geom.MultiPolygon) (centroid geom.Coord)
- func NewRadialSorting(layout geom.Layout, coordData []float64, focalPoint geom.Coord) sort.Interface
- func Normalize(angle float64) float64
- func NormalizePositive(angle float64) float64
- func OrientationIndex(vectorOrigin, vectorEnd, point geom.Coord) orientation.Type
- func PerpendicularDistanceFromPointToLine(p, lineStart, lineEnd geom.Coord) float64
- func PointIndexOnLine(ls wkb.LineString, coord geom.Coord) int
- func PointsCentroid(point *geom.Point, extra ...*geom.Point) geom.Coord
- func PointsCentroidFlat(layout geom.Layout, pointData []float64) geom.Coord
- func PolygonsCentroid(polygon *geom.Polygon, extraPolys ...*geom.Polygon) (centroid geom.Coord)
- func SignedArea(layout geom.Layout, ring []float64) float64
- type AreaCentroidCalculator
- type LineCentroidCalculator
- func (calc *LineCentroidCalculator) AddLine(line *geom.LineString) *LineCentroidCalculator
- func (calc *LineCentroidCalculator) AddLinearRing(line *geom.LinearRing) *LineCentroidCalculator
- func (calc *LineCentroidCalculator) AddPolygon(polygon *geom.Polygon) *LineCentroidCalculator
- func (calc *LineCentroidCalculator) GetCentroid() geom.Coord
- type PointCentroidCalculator
Examples ¶
- Angle
- AngleBetween
- AngleBetweenOriented
- AngleFromOrigin
- AngleOrientation
- AreaCentroidCalculator.AddPolygon
- Diff
- Distance
- DistanceFromLineToLine
- DistanceFromPointToLine
- DistanceFromPointToLineString
- DoLinesOverlap
- Equal
- InteriorAngle
- IsAcute
- IsObtuse
- IsOnLine
- IsPointWithinLineBounds
- IsRingCounterClockwise
- LineCentroidCalculator.AddPolygon
- LinearRingsCentroid
- LinesCentroid
- MultiLineCentroid
- MultiPointCentroid
- NewLineCentroidCalculator
- NewPointCentroidCalculator
- NewRadialSorting
- Normalize
- NormalizePositive
- OrientationIndex
- PerpendicularDistanceFromPointToLine
- PointsCentroid
- PointsCentroidFlat
- PolygonsCentroid
- SignedArea
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Angle ¶
func Angle(p0, p1 geom.Coord) float64
Angle函数 计算向量从po到p1的角度,此角度相当于x轴正方向,角度范围为[-180,180]
Example ¶
p1 := geom.Coord{-4.007890598483777e8, 7.149034067497588e8, -4.122305737303918e7} p2 := geom.Coord{6.452880325856061e8, -7.013452035812421e7, 6.060122721006607e8} angle := xy.Angle(p1, p2) fmt.Println(angle)
Output: -0.6437947786359727
func AngleBetween ¶
func AngleBetween(tip1, tail, tip2 geom.Coord) float64
AngleBetween函数 计算向量间的最小夹角 计算的角度范围在(0,180]之间
Param tip1 - 向量的顶点 param tail - 每一个向量的尾部 param tip2 - 每一个向量的顶点
Example ¶
p1 := geom.Coord{-8.6092078831365e7, -1.2832262246888882e8, -5.39892066777803e8} p2 := geom.Coord{-4.125610572401442e7, 3.097372706101881e8, 1.5483271373430803e8} p3 := geom.Coord{1.641532856745057e8, 3.949735922042323e7, 1.9570089185263705e8} angle := xy.AngleBetween(p1, p2, p3) fmt.Println(angle)
Output: 0.7519299818333081
func AngleBetweenOriented ¶
func AngleBetweenOriented(tip1, tail, tip2 geom.Coord) float64
AngleBetweenOriented函数 计算计算两向量间的最小夹角(有两种结果). 计算的结果范围为(-180,180] 一个正数结果对应了从v1向量到v2向量逆时针旋转 负数结果对应了从v1到v2顺时针旋转所成的角 0 表示两向量间不存在夹角(方向一致)
Example ¶
p1 := geom.Coord{-1.3799002832563987e9, 5.999590771085212e8, -4.693581090182036e8} p2 := geom.Coord{6.826007948791102e7, -8.657386626766933e8, -1.493830309099963e9} p3 := geom.Coord{-6.183224805123262e8, 2.4666014745222422e8, 7271369.117346094} angle := xy.AngleBetweenOriented(p1, p2, p3) fmt.Println(angle)
Output: -0.22640245255136904
func AngleFromOrigin ¶
func AngleFromOrigin(p geom.Coord) float64
AngleFromOrigin函数 计算向量从(0,0)到p点的角度。此角度相对于x轴正方向,角度范围为(-180,180]
Example ¶
p1 := geom.Coord{-643891.5406414514, 6.214131154131615e8, -9.241166163738243e7} angle := xy.AngleFromOrigin(p1) fmt.Println(angle)
Output: 1.571832499502282
func AngleOrientation ¶
func AngleOrientation(ang1, ang2 float64) orientation.Type
AngleOrientation函数 一个角度是否必须顺时针或逆时针旋转另一个角度
Example ¶
package main import ( "fmt" "github.com/chengxiaoer/geomGo/xy" ) func main() { p1 := 1.5973282539123574e8 p2 := 1.0509666695558771e9 orient := xy.AngleOrientation(p1, p2) fmt.Println(orient) }
Output: Clockwise
func Centroid ¶
func Centroid(geometry geom.T) (centroid geom.Coord, err error)
Centroid函数 计算几何体的质心。、 根据几何学的拓扑结构,质心可能在几何之外。
func Diff ¶
Diff函数 计算非定向的两个向量的最小角度。 假设角被归一化到范围[-π,π]。 结果将在[0,π]之间 Param ang1 - the angle of one vector (in [-Pi, Pi] ) Param ang2 - the angle of the other vector (in range [-Pi, Pi] )
Example ¶
package main import ( "fmt" "github.com/chengxiaoer/geomGo/xy" ) func main() { p1 := -5.976261773911254e7 p2 := 1.5847324519716722e8 diff := xy.Diff(p1, p2) fmt.Println(diff) }
Output: -2.1823585665309447e+08
func Distance ¶
func Distance(c1, c2 geom.Coord) float64
Distance函数 计算两点间的距离
Example ¶
coords := []float64{10, 10, 10, -10} distance := xy.Distance(geom.Coord(coords[0:2]), geom.Coord(coords[2:4])) fmt.Println(distance)
Output: 20
func DistanceFromLineToLine ¶
func DistanceFromLineToLine(line1Start, line1End, line2Start, line2End geom.Coord) float64
DistanceFromLineToLine函数 计算两个线段间的距离
Note: 不稳健
param line1Start - 线段1的起点 param line1End - 线段1的终点(不能与起点相同) param line2Start - 线段2的起点 param line2End - 线段2的终点(不能与起点相同)
Example ¶
line1 := geom.NewLineStringFlat(geom.XY, []float64{0, 0, 10, 10}) line2 := geom.NewLineStringFlat(geom.XY, []float64{-10, -10, 0, -10}) distance := xy.DistanceFromLineToLine(line1.Coord(0), line1.Coord(1), line2.Coord(0), line2.Coord(1)) fmt.Println(distance)
Output: 10
func DistanceFromPointToLine ¶
func DistanceFromPointToLine(p, lineStart, lineEnd geom.Coord) float64
DistanceFromPointToLine 计算一个点到一个线段的距离
Note: 非强健算法
Example ¶
p := geom.Coord{0, 0} lineStart := geom.Coord{10, -10} lineEnd := geom.Coord{10, 10} distance := xy.DistanceFromPointToLine(p, lineStart, lineEnd) fmt.Println(distance)
Output: 10
func DistanceFromPointToLineString ¶
DistanceFromPointToLineString函数 计算点到线段序列的距离
Param p - 一个点 Param line - 由顶点定义的连续线段。
Example ¶
p := geom.Coord{50, 50} lineString := geom.NewLineStringFlat(geom.XY, []float64{0, 0, 10, 10, 10, 20, 10, 100}) distance := xy.DistanceFromPointToLineString(lineString.Layout(), p, lineString.FlatCoords()) fmt.Println(distance)
Output: 40
func DoLinesOverlap ¶
func DoLinesOverlap(line1End1, line1End2, line2End1, line2End2 geom.Coord) bool
DoLinesOverlap函数 计算的线段的边界是否重叠
Example ¶
line1Start := geom.Coord{0, 0} line1End := geom.Coord{10, 10} line2Start := geom.Coord{0, -10} line2End := geom.Coord{10, 5} overlaps := xy.DoLinesOverlap(line1Start, line1End, line2Start, line2End) fmt.Println(overlaps)
Output: true
func Equal ¶
Equal函数 检查点start1在坐标数组1中是否与点start2以坐标数组2构成的向量相等. 只有x和y坐标进行比较,x被假定为第一坐标和y作为第二坐标, 这是一种实用方法,只在性能很重要时使用,因为它降低了可读性。
Example ¶
package main import ( "fmt" "github.com/chengxiaoer/geomGo/xy" ) func main() { coords := []float64{10, 30, 30, 10} isEqual := xy.Equal(coords, 0, coords, 1) fmt.Println(isEqual) }
Output: false
func InteriorAngle ¶
func InteriorAngle(p0, p1, p2 geom.Coord) float64
InteriorAngle函数 计算环的两个部分之间的内角。 以顺时针为正向,计算结果的范围为 [0, 2Pi]
Example ¶
p1 := geom.Coord{9.339625086270301e7, 9.494327011462314e8, -8.832231914445356e8} p2 := geom.Coord{-8.685036396637098e7, -9827198.1341636, -5.130707858094123e8} p3 := geom.Coord{5.48739535964397e8, 8.532792391532723e8, 2.8251807396930236e8} angle := xy.InteriorAngle(p1, p2, p3) fmt.Println(angle)
Output: 0.44900284899855447
func IsAcute ¶
func IsAcute(endpoint1, base, endpoint2 geom.Coord) bool
IsAcute函数 测试一个角度是否为锐角 Note: 对于非常接近90度的角度来说,不太精确。
Example ¶
p1 := geom.Coord{-2.9746056181996536e8, 1.283116247239797e9, 3.0124856147872955e8} p2 := geom.Coord{2.9337112870686615e8, -1.0822405666887188e9, 9.613329966907622e7} p3 := geom.Coord{-3.402935182393674e7, -8.477260955562395e8, 2.4474783489619292e7} isAcute := xy.IsAcute(p1, p2, p3) fmt.Println(isAcute)
Output: true
func IsObtuse ¶
func IsObtuse(endpoint1, base, endpoint2 geom.Coord) bool
IsObtuse函数 测试一个角度是否为钝角 Note: 当角度非常接近90度时,不太准确
Example ¶
p1 := geom.Coord{-6.581881182734076e8, -5.1226495000032324e8, 4.942792920863176e8} p2 := geom.Coord{-2.8760338491412956e8, -2.7637897930097174e7, -1.3120283887929991e8} p3 := geom.Coord{-7.253118635362322e8, 2.854840728999085e8, -3.3865131338040566e8} isObtuse := xy.IsObtuse(p1, p2, p3) fmt.Println(isObtuse)
Output: false
func IsOnLine ¶
IsOnLine函数 检测一个点是否在由坐标数组构成的线段上,如果这个点是线段端点或者在线段上返回true
Example ¶
line := geom.NewLineString(geom.XY) line.MustSetCoords([]geom.Coord{ {0, 0}, {10, 0}, {10, 20}, }) onLine := xy.IsOnLine(line.Layout(), geom.Coord{5, 0}, line.FlatCoords()) fmt.Println(onLine)
Output: true
func IsPointWithinLineBounds ¶
func IsPointWithinLineBounds(p, lineEndpoint1, lineEndpoint2 geom.Coord) bool
IsPointWithinLineBounds函数 计算点是否在以lineEndpoint1、lineEndpoint2为线段边界外面
Example ¶
point := geom.Coord{0, 0} line := geom.NewLineStringFlat(geom.XY, []float64{-10, -10, 0, -10}) isWithinLineBounds := xy.IsPointWithinLineBounds(point, line.Coord(0), line.Coord(1)) fmt.Println(isWithinLineBounds)
Output: false
func IsRingCounterClockwise ¶
IsRingCounterClockwise函数 判断是否入的坐标是否能组成一个逆时针的线环。
- 点的列表被假定为第一个和最后一个点相等。 - 这将会处理坐标列表里面重复的点 此算法要求是能构成一个有效的环,无效的环可能导致结果错误.
Param ring - 形成环的坐标数组。 Returns 如果这个环是逆时针方向排列的,返回true Panics 如果传入的点数目小于3,会报错
Example ¶
ring := geom.NewLinearRingFlat(geom.XY, []float64{10, 10, 20, 10, 30, 30, 10, 30, 10, 10}) clockwise := xy.IsRingCounterClockwise(ring.Layout(), ring.FlatCoords()) fmt.Println(clockwise)
Output: true
func LinearRingsCentroid ¶
func LinearRingsCentroid(line *geom.LinearRing, extraLines ...*geom.LinearRing) (centroid geom.Coord)
LinearRingsCentroid函数 计算参数传入的所有线环的质心
Algorithm: 计算各线段的中点段长度加权平均。
Example ¶
line1 := geom.NewLinearRingFlat(geom.XY, []float64{0, 0, 1, 1, 3, 3, 0, 0}) line2 := geom.NewLinearRingFlat(geom.XY, []float64{10, 10, 11, 11, 13, 13, 10, 10}) centroid := xy.LinearRingsCentroid(line1, line2) fmt.Println(centroid)
Output: [6.5 6.5]
func LinesCentroid ¶
func LinesCentroid(line *geom.LineString, extraLines ...*geom.LineString) (centroid geom.Coord)
LinesCentroid函数 计算参数传入的所有线状要素的质心
Algorithm: 计算各线段的中点段长度加权平均。
Example ¶
line1 := geom.NewLineStringFlat(geom.XY, []float64{0, 0, 1, 1, 3, 3}) line2 := geom.NewLineStringFlat(geom.XY, []float64{10, 10, 11, 11, 13, 13}) centroid := xy.LinesCentroid(line1, line2) fmt.Println(centroid)
Output: [6.5 6.5]
func MultiLineCentroid ¶
func MultiLineCentroid(line *geom.MultiLineString) (centroid geom.Coord)
MultiLineCentroid函数 计算MultiLineString的质心
Algorithm: 计算线段长度加权的所有线段的平均值。
Example ¶
line := geom.NewMultiLineStringFlat(geom.XY, []float64{0, 0, 1, 1, 3, 3, 10, 10, 11, 11, 13, 13}, []int{6, 12}) centroid := xy.MultiLineCentroid(line) fmt.Println(centroid)
Output: [6.5 6.5]
func MultiPointCentroid ¶
func MultiPointCentroid(point *geom.MultiPoint) geom.Coord
MultiPointCentroid函数 计算的点的集合的质心
算法实现:集合中所有点的平均值
Example ¶
multiPoint := geom.NewMultiPointFlat(geom.XY, []float64{ 0, 0, 2, 0, 2, 2, 0, 2}) centroid := xy.MultiPointCentroid(multiPoint) fmt.Println(centroid)
Output: [1 1]
func MultiPolygonCentroid ¶
func MultiPolygonCentroid(polygon *geom.MultiPolygon) (centroid geom.Coord)
MultiPolygonCentroid 计算区域几何(多边形集合)的形心。(multipolygon)
算法: 基于通常的算法计算的质心作为一个区域的质心加权分解成三角形(可能有重叠)。
该算法已扩展到处理多个多边形的孔。
See http://www.faqs.org/faqs/graphics/algorithms-faq/ for 具体细节关于这一基本算法.
该代码还扩展到处理退化(零面积)多边形。
在这种情况下,将返回多边形中线段的质心。
func NewRadialSorting ¶
func NewRadialSorting(layout geom.Layout, coordData []float64, focalPoint geom.Coord) sort.Interface
NewRadialSorting 创建一个实现的排序.Interface which will sort the wrapped coordinate array radially around the focal point. The comparison is based on the angle and distance from the focal point. First the angle is checked. Counter clockwise indicates a greater value and clockwise indicates a lesser value If co-linear then the coordinate nearer to the focalPoint is considered less.
Example ¶
coords := []float64{10, 10, 20, 20, 20, 0, 30, 10, 0, 0, 1, 1} sorting := xy.NewRadialSorting(geom.XY, coords, geom.Coord{10, 10}) sort.Sort(sorting) fmt.Println(coords)
Output: [10 10 20 20 30 10 20 0 1 1 0 0]
func Normalize ¶
Normalize函数 计算一个角的归一化值,它是在(-180,180]范围内的等效角
Example ¶
package main import ( "fmt" "github.com/chengxiaoer/geomGo/xy" ) func main() { p1 := 7.089301226008829e8 normalized := xy.Normalize(p1) fmt.Println(normalized) }
Output: 0.7579033437162295
func NormalizePositive ¶
NormalizePositive函数 计算一个角的归一化值,它是在[0,360]范围内的等效角 E.g.: * normalizePositive(0.0) = 0.0 * normalizePositive(-PI) = PI * normalizePositive(-2PI) = 0.0 * normalizePositive(-3PI) = PI * normalizePositive(-4PI) = 0 * normalizePositive(PI) = PI * normalizePositive(2PI) = 0.0 * normalizePositive(3PI) = PI * normalizePositive(4PI) = 0.0
Example ¶
package main import ( "fmt" "github.com/chengxiaoer/geomGo/xy" ) func main() { p1 := -2.269415841413788e8 normalized := xy.NormalizePositive(p1) fmt.Println(normalized) }
Output: 0.4870605702066726
func OrientationIndex ¶
func OrientationIndex(vectorOrigin, vectorEnd, point geom.Coord) orientation.Type
OrientationIndex函数 返回一个点关于一个特殊的向量指向的方向的索引
vectorOrigin - 向量的起点 vectorEnd - 向量的终点 point - 计算方向的点
Example ¶
vectorOrigin := geom.Coord{10.0, 10.0} vectorEnd := geom.Coord{20.0, 20.0} target := geom.Coord{10.0, 20.0} orientation := xy.OrientationIndex(vectorOrigin, vectorEnd, target) fmt.Println(orientation)
Output: CounterClockwise
func PerpendicularDistanceFromPointToLine ¶
func PerpendicularDistanceFromPointToLine(p, lineStart, lineEnd geom.Coord) float64
PerpendicularDistanceFromPointToLine函数 计算从点P到直线的垂直距离。 containing the points lineStart/lineEnd
Example ¶
p := geom.Coord{0, 0} lineStart := geom.Coord{10, 5} lineEnd := geom.Coord{10, 10} distance := xy.PerpendicularDistanceFromPointToLine(p, lineStart, lineEnd) fmt.Println(distance)
Output: 10
func PointIndexOnLine ¶
func PointIndexOnLine(ls wkb.LineString, coord geom.Coord) int
获取线的控制点中距离某点最近的点的索引 FIXME 第一个参数可以是geom.LineString
func PointsCentroid ¶
func PointsCentroid(point *geom.Point, extra ...*geom.Point) geom.Coord
PointsCentroid函数 计算参数传入的点的质心
算法实现:所有点的平均值
Example ¶
centroid := xy.PointsCentroid( geom.NewPointFlat(geom.XY, []float64{0, 0}), geom.NewPointFlat(geom.XY, []float64{2, 0}), geom.NewPointFlat(geom.XY, []float64{2, 2}), geom.NewPointFlat(geom.XY, []float64{0, 2})) fmt.Println(centroid)
Output: [1 1]
func PointsCentroidFlat ¶
func PointsCentroidFlat(layout geom.Layout, pointData []float64) geom.Coord
PointsCentroidFlat函数 计算点数组中的点的质心 布局仅用于确定如何查找每个坐标,x-y坐标每个点必须的参数 算法实现: 所有点的平均值
Example ¶
multiPoint := geom.NewMultiPointFlat(geom.XY, []float64{0, 0, 2, 0, 2, 2, 0, 2}) centroid := xy.PointsCentroidFlat(multiPoint.Layout(), multiPoint.FlatCoords()) fmt.Println(centroid)
Output: [1 1]
func PolygonsCentroid ¶
func PolygonsCentroid(polygon *geom.Polygon, extraPolys ...*geom.Polygon) (centroid geom.Coord)
PolygonsCentroid函数 计算一个多边形的几何中心
算法: 基于通常的算法计算的质心作为一个区域的质心加权分解成三角形(可能有重叠)。
该算法已扩展到处理多个多边形的孔。
See http://www.faqs.org/faqs/graphics/algorithms-faq/ for 具体细节关于这一基本算法.
该代码还扩展到处理退化(零面积)多边形
在这种情况下,将返回多边形中线段的质心。
Example ¶
poly1 := geom.NewPolygonFlat(geom.XY, []float64{0, 0, -10, 0, -10, -10, 0, -10, 0, 0}, []int{10}) poly2 := geom.NewPolygonFlat(geom.XY, []float64{0, 0, 10, 0, 10, 10, 0, 10, 0, 0}, []int{10}) centroid := xy.PolygonsCentroid(poly1, poly2) fmt.Println(centroid)
Output: [0 0]
func SignedArea ¶
SignedArea函数 计算一个线环的面积。 computes the signed area for a ring. The signed area is positive if the 如果线环是顺时针旋转的则结果为正数,如果逆时针旋转结果为负数。 如果环是退化的或平坦的,结果为0
Example ¶
ring := geom.NewLinearRingFlat(geom.XY, []float64{10, 10, 20, 10, 30, 30, 10, 30, 10, 10}) singedArea := xy.SignedArea(ring.Layout(), ring.FlatCoords()) fmt.Println(singedArea)
Output: -300
Types ¶
type AreaCentroidCalculator ¶
type AreaCentroidCalculator struct {
// contains filtered or unexported fields
}
AreaCentroidCalculator 是质心计算数据的数据结构。这类型无法使用其0的价值, 它必须使用newareacentroid函数来创建
func NewAreaCentroidCalculator ¶
func NewAreaCentroidCalculator(layout geom.Layout) *AreaCentroidCalculator
NewAreaCentroidCalculator函数 创建计算的新实例。 创建计算器后,可以添加多边形。 GetCentroid方法 在任何点都可以得到当前的质心,每次添加多边形时,质心都会自然变化。
func (*AreaCentroidCalculator) AddPolygon ¶
func (calc *AreaCentroidCalculator) AddPolygon(polygon *geom.Polygon)
AddPolygon方法 向计算器中添加多边形
Example ¶
polygons := []*geom.Polygon{ geom.NewPolygonFlat(geom.XY, []float64{0, 0, -10, 0, -10, -10, 0, -10, 0, 0}, []int{10}), geom.NewPolygonFlat(geom.XY, []float64{0, 0, 10, 0, 10, 10, 0, 10, 0, 0}, []int{10}), } calculator := xy.NewAreaCentroidCalculator(geom.XY) for _, p := range polygons { calculator.AddPolygon(p) } fmt.Println(calculator.GetCentroid())
Output: [0 0]
func (*AreaCentroidCalculator) GetCentroid ¶
func (calc *AreaCentroidCalculator) GetCentroid() geom.Coord
* *------------------------------ * AreaCentroidCalculator(质心计算器)相关的方法 *---------------------------------
GetCentroid方法 获得当前计算的质心。返回一个0,如果没有几何已添加
type LineCentroidCalculator ¶
type LineCentroidCalculator struct {
// contains filtered or unexported fields
}
LineCentroidCalculator结构 是质心计算的数据结构
该结构没有默认零值,必须使用NewLineCentroid函数创建
func NewLineCentroidCalculator ¶
func NewLineCentroidCalculator(layout geom.Layout) *LineCentroidCalculator
NewLineCentroidCalculator 创建计算器的新实例。 计算器创建后多边形、线要素或线性环可以添加 GetCentroid方法 可以用于在任何点获得当前质心。 每次添加几何体时,质心都会发生自然变化。
Example ¶
calculator := xy.NewLineCentroidCalculator(geom.XY) calculator.AddLine(geom.NewLineStringFlat(geom.XY, []float64{0, 0, 1, 1, 3, 3})) calculator.AddLine(geom.NewLineStringFlat(geom.XY, []float64{10, 10, 11, 11, 13, 13})) centroid := calculator.GetCentroid() fmt.Println(centroid)
Output: [6.5 6.5]
func (*LineCentroidCalculator) AddLine ¶
func (calc *LineCentroidCalculator) AddLine(line *geom.LineString) *LineCentroidCalculator
AddLine方法 向计算器中添加线段
func (*LineCentroidCalculator) AddLinearRing ¶
func (calc *LineCentroidCalculator) AddLinearRing(line *geom.LinearRing) *LineCentroidCalculator
AddLinearRing方法 向计算器中添加线环
func (*LineCentroidCalculator) AddPolygon ¶
func (calc *LineCentroidCalculator) AddPolygon(polygon *geom.Polygon) *LineCentroidCalculator
AddPolygon方法 向计算器中添加多边形。
Example ¶
calculator := xy.NewLineCentroidCalculator(geom.XY) calculator.AddPolygon(geom.NewPolygonFlat(geom.XY, []float64{0, 0, 1, 1, 3, 3}, []int{6})) centroid := calculator.GetCentroid() fmt.Println(centroid)
Output: [1.5 1.5]
func (*LineCentroidCalculator) GetCentroid ¶
func (calc *LineCentroidCalculator) GetCentroid() geom.Coord
GetCentroid 获取质心,如果没有几何类型加入则返回0
type PointCentroidCalculator ¶
type PointCentroidCalculator struct {
// contains filtered or unexported fields
}
PointCentroidCalculator结构 点质心计算的数据组织结构。 该结构不能使用0值来进行初始化,必须使用 NewPointCentroid 函数来进行创建
func NewPointCentroidCalculator ¶
func NewPointCentroidCalculator() PointCentroidCalculator
NewPointCentroidCalculator函数 创建点的计算器结构/对象 计算器对象创建后可以继续添加坐标或点 使用 GetCentedrid 方法可以获取最新的计算结果
Example ¶
polygon := geom.NewPolygonFlat(geom.XY, []float64{0, 0, 2, 0, 2, 2, 0, 2}, []int{8}) calculator := xy.NewPointCentroidCalculator() coords := polygon.FlatCoords() stride := polygon.Layout().Stride() for i := 0; i < len(coords); i += stride { calculator.AddCoord(geom.Coord(coords[i : i+stride])) } fmt.Println(calculator.GetCentroid())
Output: [1 1]
func (*PointCentroidCalculator) AddCoord ¶
func (calc *PointCentroidCalculator) AddCoord(point geom.Coord)
AddCoord方法 向计算器中添加点坐标
func (*PointCentroidCalculator) AddPoint ¶
func (calc *PointCentroidCalculator) AddPoint(point *geom.Point)
AddPoint方法 向计算器中添加点
func (*PointCentroidCalculator) GetCentroid ¶
func (calc *PointCentroidCalculator) GetCentroid() geom.Coord
GetCentroid方法 获取最新的质心计算结果. 如果计算器中没有点则返回0