增加圆形相关处理函数

This commit is contained in:
kercylan98 2023-06-20 19:42:38 +08:00
parent da94e20de6
commit 7e29a6017e
2 changed files with 15 additions and 5 deletions

View File

@ -37,7 +37,7 @@ func (slf Circle[V]) Centroid() Point[V] {
// Overlap 与另一个圆是否发生重叠
func (slf Circle[V]) Overlap(circle Circle[V]) bool {
return CalcDistanceWithPoint(slf.Centroid(), circle.Centroid())/2 > slf.radius
return slf.CentroidDistance(circle) < slf.Radius()+circle.Radius()
}
// Area 获取圆形面积
@ -50,8 +50,13 @@ func (slf Circle[V]) Length() V {
return V(2 * math.Pi * float64(slf.Radius()))
}
// GenerateCircle 通过传入圆的半径和需要的点数量,生成一个圆
func GenerateCircle[V generic.SignedNumber](radius V, points int) Circle[V] {
// CentroidDistance 计算与另一个圆的质心距离
func (slf Circle[V]) CentroidDistance(circle Circle[V]) V {
return CalcCircleCentroidDistance(slf, circle)
}
// NewCircle 通过传入圆的半径和需要的点数量,生成一个圆
func NewCircle[V generic.SignedNumber](radius V, points int) Circle[V] {
angle := 2.0 * math.Pi / float64(points)
var shape = make(Shape[V], points)
for i := 0; i < points; i++ {
@ -62,3 +67,8 @@ func GenerateCircle[V generic.SignedNumber](radius V, points int) Circle[V] {
}
return shape.ToCircle()
}
// CalcCircleCentroidDistance 计算两个圆质心距离
func CalcCircleCentroidDistance[V generic.SignedNumber](circle1, circle2 Circle[V]) V {
return CalcDistanceWithPoint(circle1.Centroid(), circle1.Centroid())
}

View File

@ -5,8 +5,8 @@ import (
"github.com/kercylan98/minotaur/utils/geometry"
)
func ExampleGenerateCircle() {
fmt.Println(geometry.GenerateCircle[float64](7, 12))
func ExampleNewCircle() {
fmt.Println(geometry.NewCircle[float64](7, 12))
// Output:
// [[0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [7 0] [6.062177826491071 3.4999999999999996] [3.500000000000001 6.06217782649107] [4.28626379701573e-16 7] [-3.4999999999999982 6.062177826491071] [-6.0621778264910695 3.500000000000002] [-7 8.57252759403146e-16] [-6.062177826491071 -3.4999999999999982] [-3.500000000000003 -6.062177826491069] [-1.285879139104719e-15 -7] [3.499999999999995 -6.062177826491074] [6.062177826491069 -3.500000000000003]]