增加部分计算函数

CalcProjectionPoint 计算一个点到一条线段的最近点
DoublePointToCoordinate 将两个位置转换为 x1, y1, x2, y2 的坐标进行返回
CalcDistanceSquared 计算两点之间的平方距离
Clamp 将给定值限制在最小值和最大值之间
This commit is contained in:
kercylan98 2023-06-19 11:26:20 +08:00
parent f64edc7fe8
commit 4f05f7f522
3 changed files with 34 additions and 0 deletions

View File

@ -114,6 +114,13 @@ func CalcDistance[V generic.SignedNumber](x1, y1, x2, y2 V) V {
return V(math.Sqrt(math.Pow(float64(x2-x1), 2) + math.Pow(float64(y2-y1), 2))) return V(math.Sqrt(math.Pow(float64(x2-x1), 2) + math.Pow(float64(y2-y1), 2)))
} }
// CalcDistanceSquared 计算两点之间的平方距离
// - 这个函数的主要用途是在需要计算两点之间距离的情况下,但不需要得到实际的距离值,而只需要比较距离大小。因为平方根运算相对较为耗时,所以在只需要比较大小的情况下,通常会使用平方距离。
func CalcDistanceSquared[V generic.SignedNumber](x1, y1, x2, y2 V) V {
dx, dy := x2-x1, y2-y1
return dx*dx + dy*dy
}
// CalcAngle 计算点2位于点1之间的角度 // CalcAngle 计算点2位于点1之间的角度
func CalcAngle[V generic.SignedNumber](x1, y1, x2, y2 V) V { func CalcAngle[V generic.SignedNumber](x1, y1, x2, y2 V) V {
return V(math.Atan2(float64(y2-y1), float64(x2-x1)) * 180 / math.Pi) return V(math.Atan2(float64(y2-y1), float64(x2-x1)) * 180 / math.Pi)

View File

@ -2,6 +2,7 @@ package geometry
import ( import (
"github.com/kercylan98/minotaur/utils/generic" "github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/utils/maths"
"math" "math"
) )
@ -157,3 +158,18 @@ func PosToCoordinateArrayWithMulti[V generic.SignedNumber](width V, positions ..
func PosSameRow[V generic.SignedNumber](width, pos1, pos2 V) bool { func PosSameRow[V generic.SignedNumber](width, pos1, pos2 V) bool {
return (pos1 / width) == (pos2 / width) return (pos1 / width) == (pos2 / width)
} }
// DoublePointToCoordinate 将两个位置转换为 x1, y1, x2, y2 的坐标进行返回
func DoublePointToCoordinate[V generic.SignedNumber](point1, point2 Point[V]) (x1, y1, x2, y2 V) {
return point1.GetX(), point1.GetY(), point2.GetX(), point2.GetY()
}
// CalcProjectionPoint 计算一个点到一条线段的最近点(即投影点)的。这个函数接收一个点和一条线段作为输入,线段由两个端点组成。
// - 该函数的主要用于需要计算一个点到一条线段的最近点的情况下
func CalcProjectionPoint[V generic.SignedNumber](linePointA, linePointB, point Point[V]) Point[V] {
ax, ay, bx, by := DoublePointToCoordinate(linePointA, linePointB)
ds := CalcDistanceSquared(ax, ay, bx, by)
px, py := point.GetXY()
clamp := maths.Clamp((px-ax)*(bx-ax)+(py-ay)*(by-ay)/ds, V(0), V(1))
return NewPoint(ax+clamp*(bx-ax), ay+clamp*(by-ay))
}

View File

@ -83,3 +83,14 @@ func MaxMin[V generic.Number](a, b V) (max, min V) {
} }
return b, a return b, a
} }
// Clamp 将给定值限制在最小值和最大值之间
func Clamp[V generic.Number](value, min, max V) V {
if value < min {
return min
}
if value > max {
return max
}
return value
}