fix: 修复 geometry 包 SimpleCircle.Projection 函数不正确的问题。优化部分注释及添加部分函数
This commit is contained in:
parent
7333101dc6
commit
f7c37016ce
|
@ -5,7 +5,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewPrioritySlice 创建一个优先级切片
|
// NewPrioritySlice 创建一个优先级切片,优先级越低越靠前
|
||||||
func NewPrioritySlice[V any](lengthAndCap ...int) *PrioritySlice[V] {
|
func NewPrioritySlice[V any](lengthAndCap ...int) *PrioritySlice[V] {
|
||||||
p := &PrioritySlice[V]{}
|
p := &PrioritySlice[V]{}
|
||||||
if len(lengthAndCap) > 0 {
|
if len(lengthAndCap) > 0 {
|
||||||
|
@ -19,7 +19,7 @@ func NewPrioritySlice[V any](lengthAndCap ...int) *PrioritySlice[V] {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrioritySlice 是一个优先级切片
|
// PrioritySlice 是一个优先级切片,优先级越低越靠前
|
||||||
type PrioritySlice[V any] struct {
|
type PrioritySlice[V any] struct {
|
||||||
items []*priorityItem[V]
|
items []*priorityItem[V]
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,19 @@ func (slf Point[V]) Max(point Point[V]) Point[V] {
|
||||||
return NewPoint(x, y)
|
return NewPoint(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Move 返回向特定角度移动特定距离后的新的位置,其中 angle 期待的角度范围是 -180~180
|
||||||
|
func (slf Point[V]) Move(angle, direction V) Point[V] {
|
||||||
|
df := float64(direction)
|
||||||
|
// 将角度转换为弧度
|
||||||
|
radian := float64(angle) * (math.Pi / 180.0)
|
||||||
|
|
||||||
|
// 计算新的坐标
|
||||||
|
newX := float64(slf.GetX()) + df*math.Cos(radian)
|
||||||
|
newY := float64(slf.GetY()) + df*math.Sin(radian)
|
||||||
|
|
||||||
|
return NewPoint(V(newX), V(newY))
|
||||||
|
}
|
||||||
|
|
||||||
// Min 返回两个位置中每个维度的最小值组成的新的位置
|
// Min 返回两个位置中每个维度的最小值组成的新的位置
|
||||||
func (slf Point[V]) Min(point Point[V]) Point[V] {
|
func (slf Point[V]) Min(point Point[V]) Point[V] {
|
||||||
x, y := slf.GetXY()
|
x, y := slf.GetXY()
|
||||||
|
|
|
@ -29,6 +29,11 @@ func (sc SimpleCircle[V]) String() string {
|
||||||
return fmt.Sprintf("SimpleCircle{centroid: %v, %v, radius: %v}", sc.centroid.GetX(), sc.centroid.GetY(), sc.radius)
|
return fmt.Sprintf("SimpleCircle{centroid: %v, %v, radius: %v}", sc.centroid.GetX(), sc.centroid.GetY(), sc.radius)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsZero 反映该圆形是否为零值的无效圆形
|
||||||
|
func (sc SimpleCircle[V]) IsZero() bool {
|
||||||
|
return sc.radius == V(0)
|
||||||
|
}
|
||||||
|
|
||||||
// Centroid 获取圆形质心位置
|
// Centroid 获取圆形质心位置
|
||||||
func (sc SimpleCircle[V]) Centroid() Point[V] {
|
func (sc SimpleCircle[V]) Centroid() Point[V] {
|
||||||
return sc.centroid
|
return sc.centroid
|
||||||
|
@ -74,15 +79,18 @@ func (sc SimpleCircle[V]) Area() V {
|
||||||
return sc.radius * sc.radius
|
return sc.radius * sc.radius
|
||||||
}
|
}
|
||||||
|
|
||||||
// Projection 获取圆形投影到另一个圆形的特定比例下的位置和半径
|
// Projection 获取圆形投影到另一个圆形的特定比例下的位置和半径(同心合并)
|
||||||
func (sc SimpleCircle[V]) Projection(circle SimpleCircle[V], ratio float64) SimpleCircle[V] {
|
func (sc SimpleCircle[V]) Projection(circle SimpleCircle[V], ratio float64) SimpleCircle[V] {
|
||||||
// 计算圆心朝目标按比例移动后的位置
|
// 计算圆心朝目标按比例移动后的位置
|
||||||
distance := float64(sc.Centroid().Distance(circle.centroid))
|
distance := sc.Centroid().Distance(circle.Centroid())
|
||||||
moveDistance := distance * ratio
|
moveDistance := distance * ratio
|
||||||
newX := float64(sc.CentroidX()) + moveDistance*(float64(circle.CentroidX())-float64(sc.CentroidX()))/distance
|
newX := float64(circle.CentroidX()) + moveDistance*(float64(sc.CentroidX())-float64(circle.CentroidX()))/distance
|
||||||
newY := float64(sc.CentroidY()) + moveDistance*(float64(circle.CentroidY())-float64(sc.CentroidY()))/distance
|
newY := float64(circle.CentroidY()) + moveDistance*(float64(sc.CentroidY())-float64(circle.CentroidY()))/distance
|
||||||
|
|
||||||
return NewSimpleCircle(V(float64(sc.radius)*ratio), NewPoint(V(newX), V(newY)))
|
// 计算新的半径
|
||||||
|
newRadius := float64(sc.radius) + (float64(circle.radius)-float64(sc.radius))*(1-ratio)
|
||||||
|
|
||||||
|
return NewSimpleCircle(V(newRadius), NewPoint(V(newX), V(newY)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Length 获取圆的周长
|
// Length 获取圆的周长
|
||||||
|
|
|
@ -13,6 +13,30 @@ func Int64(min int64, max int64) int64 {
|
||||||
return min + rand.Int63n(max+1-min)
|
return min + rand.Int63n(max+1-min)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int32 返回一个介于min和max之间的的int32类型的随机数。
|
||||||
|
func Int32(min int32, max int32) int32 {
|
||||||
|
if min == max {
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
return int32(Int64(int64(min), int64(max)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float32Range 返回一个介于min和max之间的的float32类型的随机数。
|
||||||
|
func Float32Range(min float32, max float32) float32 {
|
||||||
|
if min == max {
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
return min + rand.Float32()*(max-min)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64Range 返回一个介于min和max之间的的float64类型的随机数。
|
||||||
|
func Float64Range(min float64, max float64) float64 {
|
||||||
|
if min == max {
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
return min + rand.Float64()*(max-min)
|
||||||
|
}
|
||||||
|
|
||||||
// Int 返回一个介于min和max之间的的int类型的随机数。
|
// Int 返回一个介于min和max之间的的int类型的随机数。
|
||||||
func Int(min int, max int) int {
|
func Int(min int, max int) int {
|
||||||
if min == max {
|
if min == max {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package times
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/kercylan98/minotaur/utils/generic"
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -60,3 +61,8 @@ func IntervalFormat(time1, time2 time.Time) string {
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToSecDuration 转换为秒级 time.Duration
|
||||||
|
func ToSecDuration[V generic.Number](v V) time.Duration {
|
||||||
|
return Second * time.Duration(int64(v))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue