structure optimization

This commit is contained in:
kercylan98
2023-04-07 14:28:58 +08:00
parent 17fdf1b7ac
commit 45a9d9b4e3
7 changed files with 128 additions and 7 deletions
+12
View File
@@ -1 +1,13 @@
package g2d
import "math"
// CalcDistance 计算两点之间的距离
func CalcDistance(x1, y1, x2, y2 float64) float64 {
return math.Sqrt(math.Pow(x2-x1, 2) + math.Pow(y2-y1, 2))
}
// CalcAngle 计算点2位于点1之间的角度
func CalcAngle(x1, y1, x2, y2 float64) float64 {
return math.Atan2(y2-y1, x2-x1) * 180 / math.Pi
}
+25
View File
@@ -0,0 +1,25 @@
package g2d
type Direction uint8
const (
DirectionUp = Direction(iota)
DirectionDown
DirectionLeft
DirectionRight
)
// CalcDirection 计算点2位于点1的方向
func CalcDirection(x1, y1, x2, y2 float64) Direction {
angle := CalcAngle(x1, y1, x2, y2)
if angle > -45 && angle < 45 {
return DirectionRight
} else if angle > 135 && angle < -135 {
return DirectionLeft
} else if angle > 45 && angle < 135 {
return DirectionUp
} else if angle > -135 && angle < -45 {
return DirectionDown
}
return 0
}