DelayNoMore/dnmshared/geometry.go

51 lines
1.1 KiB
Go
Raw Normal View History

package dnmshared
2022-09-20 15:50:01 +00:00
import (
2022-11-09 06:20:26 +00:00
. "dnmshared/sharedprotos"
2022-09-20 15:50:01 +00:00
"math"
)
func NormVec2D(dx, dy float64) Vec2D {
2022-11-09 06:20:26 +00:00
return Vec2D{X: dy, Y: -dx}
}
2022-11-09 04:19:29 +00:00
func AlignPolygon2DToBoundingBox(input *Polygon2D) *Polygon2D {
// Transform again to put "anchor" at the "bottom-left point (w.r.t. world space)" of the bounding box for "resolv"
boundingBoxBL := &Vec2D{
2022-11-09 04:19:29 +00:00
X: math.MaxFloat64,
Y: math.MaxFloat64,
}
for _, p := range input.Points {
if p.X < boundingBoxBL.X {
boundingBoxBL.X = p.X
2022-11-09 04:19:29 +00:00
}
if p.Y < boundingBoxBL.Y {
boundingBoxBL.Y = p.Y
2022-11-09 04:19:29 +00:00
}
}
// Now "input.Anchor" should move to "input.Anchor+boundingBoxBL", thus "boundingBoxBL" is also the value of the negative diff for all "input.Points"
2022-11-09 04:19:29 +00:00
output := &Polygon2D{
Anchor: &Vec2D{
X: input.Anchor.X + boundingBoxBL.X,
Y: input.Anchor.Y + boundingBoxBL.Y,
2022-11-09 04:19:29 +00:00
},
2022-11-09 06:20:26 +00:00
Points: make([]*Vec2D, len(input.Points)),
2022-11-09 04:19:29 +00:00
}
for i, p := range input.Points {
output.Points[i] = &Vec2D{
X: p.X - boundingBoxBL.X,
Y: p.Y - boundingBoxBL.Y,
2022-11-09 04:19:29 +00:00
}
}
return output
2022-09-20 15:50:01 +00:00
}
func Distance(pt1 *Vec2D, pt2 *Vec2D) float64 {
dx := pt1.X - pt2.X
dy := pt1.Y - pt2.Y
return math.Sqrt(dx*dx + dy*dy)
}