From 03f5a4fbdb2beb4999c108178d765d09b3f3a9b8 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Fri, 16 Jun 2023 16:34:02 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=BA=BF=E6=AE=B5=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=94=AF=E6=8C=81=E5=9D=90=E6=A0=87=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=8F=8A=E9=A1=BA=E5=BA=8F=E4=BD=8D=E7=BD=AE=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/geometry/line.go | 51 ++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/utils/geometry/line.go b/utils/geometry/line.go index e03f9fa..c7161e2 100644 --- a/utils/geometry/line.go +++ b/utils/geometry/line.go @@ -9,15 +9,42 @@ func PointOnLineWithCoordinate[V generic.Number](x1, y1, x2, y2, x, y V) bool { return (x-x1)*(y2-y1) == (x2-x1)*(y-y1) } -// -//func PointOnLineWithPos[V generic.Number](width, pos1, pos2, pos V) bool { -// x1, y1 := PosToCoordinate(width, pos1) -// x2, y2 := PosToCoordinate(width, pos2) -// return (x-x1)*(y2-y1) == (x2-x1)*(y-y1) -//} -// -//// PointOnSegment 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上 -//// - 与 PointOnLine 不同的是, PointOnSegment 中会判断线段及点的位置是否正确 -//func PointOnSegment[V generic.Number](x1, y1, x2, y2, x, y V) bool { -// return x >= x1 && x <= x2 && y >= y1 && y <= y2 && PointOnLine(x1, y1, x2, y2, x, y) -//} +// PointOnLineWithPos 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上 +func PointOnLineWithPos[V generic.Number](width, pos1, pos2, pos V) bool { + x1, y1 := PosToCoordinate(width, pos1) + x2, y2 := PosToCoordinate(width, pos2) + x, y := PosToCoordinate(width, pos) + return PointOnLineWithCoordinate(x1, y1, x2, y2, x, y) +} + +// PointOnLineWithCoordinateArray 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上 +func PointOnLineWithCoordinateArray[V generic.Number](point1, point2, point Point[V]) bool { + x1, y1 := point1.GetXY() + x2, y2 := point2.GetXY() + x, y := point.GetXY() + return PointOnLineWithCoordinate(x1, y1, x2, y2, x, y) +} + +// PointOnSegmentWithCoordinate 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上 +// - 与 PointOnLineWithCoordinate 不同的是, PointOnSegmentWithCoordinate 中会判断线段及点的位置是否正确 +func PointOnSegmentWithCoordinate[V generic.Number](x1, y1, x2, y2, x, y V) bool { + return x >= x1 && x <= x2 && y >= y1 && y <= y2 && PointOnLineWithCoordinate(x1, y1, x2, y2, x, y) +} + +// PointOnSegmentWithPos 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上 +// - 与 PointOnLineWithPos 不同的是, PointOnSegmentWithPos 中会判断线段及点的位置是否正确 +func PointOnSegmentWithPos[V generic.Number](width, pos1, pos2, pos V) bool { + x1, y1 := PosToCoordinate(width, pos1) + x2, y2 := PosToCoordinate(width, pos2) + x, y := PosToCoordinate(width, pos) + return x >= x1 && x <= x2 && y >= y1 && y <= y2 && PointOnLineWithCoordinate(x1, y1, x2, y2, x, y) +} + +// PointOnSegmentWithCoordinateArray 通过一个线段两个点的位置和一个点的坐标,判断这个点是否在一条线段上 +// - 与 PointOnLineWithCoordinateArray 不同的是, PointOnSegmentWithCoordinateArray 中会判断线段及点的位置是否正确 +func PointOnSegmentWithCoordinateArray[V generic.Number](point1, point2, point Point[V]) bool { + x1, y1 := point1.GetXY() + x2, y2 := point2.GetXY() + x, y := point.GetXY() + return x >= x1 && x <= x2 && y >= y1 && y <= y2 && PointOnLineWithCoordinate(x1, y1, x2, y2, x, y) +}