增加PointOnLine 判断点是否在一条线段上

This commit is contained in:
kercylan98 2023-06-15 11:04:42 +08:00
parent 3c4eb23041
commit eb1def1bf4
1 changed files with 11 additions and 0 deletions

View File

@ -1,5 +1,7 @@
package g2d package g2d
import "github.com/kercylan98/minotaur/utils/generic"
// CoordinateToCoordinateArray 将坐标转换为x、y的坐标数组 // CoordinateToCoordinateArray 将坐标转换为x、y的坐标数组
func CoordinateToCoordinateArray(x, y int) [2]int { func CoordinateToCoordinateArray(x, y int) [2]int {
return [2]int{x, y} return [2]int{x, y}
@ -127,3 +129,12 @@ func MatrixToPosMatrix[V any](matrix [][]V) (width int, posMatrix []V) {
} }
return return
} }
// PointOnLine 接受六个参数 top、bottom、left、right 和 x、y分别表示一个矩形位置尺寸和一个点的横纵坐标判断这个点是否在一条线段上。
// - 首先计算点 (x, y) 与线段起点 (left, top) 之间的斜率即 (x - left) / (y - top)。
// - 然后计算线段起点 (left, top) 与线段终点 (right, bottom) 之间的斜率,即 (right - left) / (bottom - top)。
// - 如果这两个斜率等,那么点 (x, y) 就在这条线段上。为了避免除法可能导致的浮点数误差,我们可以将两个斜率的计算转换为乘法形式,即比较 (x - left) * (bottom - top) 是否等于 (right - left) * y - top)。
// - 如果上述等式成立,说明点 (x, y) 在线段上,函数返回 true否则返回 false。
func PointOnLine[V generic.Number](top, bottom, left, right, x, y V) bool {
return (x-left)*(bottom-top) == (right-left)*(y-top)
}