Skip to content

Commit

Permalink
fix: 修复 geometry 包 SimpleCircle.Projection 函数不正确的问题。优化部分注释及添加部分函数
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Mar 4, 2024
1 parent 7333101 commit f7c3701
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions utils/collection/listings/priority_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sort"
)

// NewPrioritySlice 创建一个优先级切片
// NewPrioritySlice 创建一个优先级切片,优先级越低越靠前
func NewPrioritySlice[V any](lengthAndCap ...int) *PrioritySlice[V] {
p := &PrioritySlice[V]{}
if len(lengthAndCap) > 0 {
Expand All @@ -19,7 +19,7 @@ func NewPrioritySlice[V any](lengthAndCap ...int) *PrioritySlice[V] {
return p
}

// PrioritySlice 是一个优先级切片
// PrioritySlice 是一个优先级切片,优先级越低越靠前
type PrioritySlice[V any] struct {
items []*priorityItem[V]
}
Expand Down
13 changes: 13 additions & 0 deletions utils/geometry/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ func (slf Point[V]) Max(point Point[V]) Point[V] {
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 返回两个位置中每个维度的最小值组成的新的位置
func (slf Point[V]) Min(point Point[V]) Point[V] {
x, y := slf.GetXY()
Expand Down
18 changes: 13 additions & 5 deletions utils/geometry/simple_circle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

// IsZero 反映该圆形是否为零值的无效圆形
func (sc SimpleCircle[V]) IsZero() bool {
return sc.radius == V(0)
}

// Centroid 获取圆形质心位置
func (sc SimpleCircle[V]) Centroid() Point[V] {
return sc.centroid
Expand Down Expand Up @@ -74,15 +79,18 @@ func (sc SimpleCircle[V]) Area() V {
return sc.radius * sc.radius
}

// Projection 获取圆形投影到另一个圆形的特定比例下的位置和半径
// Projection 获取圆形投影到另一个圆形的特定比例下的位置和半径(同心合并)
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
newX := float64(sc.CentroidX()) + moveDistance*(float64(circle.CentroidX())-float64(sc.CentroidX()))/distance
newY := float64(sc.CentroidY()) + moveDistance*(float64(circle.CentroidY())-float64(sc.CentroidY()))/distance
newX := float64(circle.CentroidX()) + moveDistance*(float64(sc.CentroidX())-float64(circle.CentroidX()))/distance
newY := float64(circle.CentroidY()) + moveDistance*(float64(sc.CentroidY())-float64(circle.CentroidY()))/distance

// 计算新的半径
newRadius := float64(sc.radius) + (float64(circle.radius)-float64(sc.radius))*(1-ratio)

return NewSimpleCircle(V(float64(sc.radius)*ratio), NewPoint(V(newX), V(newY)))
return NewSimpleCircle(V(newRadius), NewPoint(V(newX), V(newY)))
}

// Length 获取圆的周长
Expand Down
24 changes: 24 additions & 0 deletions utils/random/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ func Int64(min int64, max int64) int64 {
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类型的随机数。
func Int(min int, max int) int {
if min == max {
Expand Down
6 changes: 6 additions & 0 deletions utils/times/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package times

import (
"fmt"
"github.com/kercylan98/minotaur/utils/generic"
"math"
"strconv"
"time"
Expand Down Expand Up @@ -60,3 +61,8 @@ func IntervalFormat(time1, time2 time.Time) string {
}
return res
}

// ToSecDuration 转换为秒级 time.Duration
func ToSecDuration[V generic.Number](v V) time.Duration {
return Second * time.Duration(int64(v))
}

0 comments on commit f7c3701

Please sign in to comment.