structure optimization
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user