From 1e493cfd68c0ee52a9915e14137851afd5566398 Mon Sep 17 00:00:00 2001 From: Jacob <2606873+unitoftime@users.noreply.github.com> Date: Mon, 2 Dec 2024 08:00:55 -0500 Subject: [PATCH] go fmt --- glm/color.go | 4 ++-- glm/line.go | 22 +++++++++++----------- glm/rect.go | 3 ++- spatial/shape.go | 24 ++++++++++++------------ spatial/shape_test.go | 24 +++++++++++------------- 5 files changed, 38 insertions(+), 39 deletions(-) diff --git a/glm/color.go b/glm/color.go index b5b54aa..2e50200 100644 --- a/glm/color.go +++ b/glm/color.go @@ -27,8 +27,8 @@ func FromUint8(r, g, b, a uint8) RGBA { func HexColor(col uint64, alpha uint8) RGBA { return FromNRGBA(color.NRGBA{ - R: uint8((col>>16) & 0xff), - G: uint8((col>>8) & 0xff), + R: uint8((col >> 16) & 0xff), + G: uint8((col >> 8) & 0xff), B: uint8(col & 0xff), A: alpha, }) diff --git a/glm/line.go b/glm/line.go index debe63e..fb9b591 100644 --- a/glm/line.go +++ b/glm/line.go @@ -18,30 +18,30 @@ func (l1 Line2) Intersects(l2 Line2) bool { y4 := l2.B.Y // Ensure no line is 0 length - if ((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) { + if (x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4) { return false } - den := ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)) + den := ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)) // Lines are parallel if den == 0 { return false } - ua := ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / den - ub := ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / den + ua := ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / den + ub := ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / den - // is the intersection along the segments - if (ua < 0 || ua > 1 || ub < 0 || ub > 1) { + // is the intersection along the segments + if ua < 0 || ua > 1 || ub < 0 || ub > 1 { return false - } + } return true - // // Return a object with the x and y coordinates of the intersection - // x := x1 + ua * (x2 - x1) - // y := y1 + ua * (y2 - y1) + // // Return a object with the x and y coordinates of the intersection + // x := x1 + ua * (x2 - x1) + // y := y1 + ua * (y2 - y1) - // return {x, y} + // return {x, y} } diff --git a/glm/rect.go b/glm/rect.go index 39e572f..83606d3 100644 --- a/glm/rect.go +++ b/glm/rect.go @@ -230,6 +230,7 @@ func (r *Rect) CutTop(amount float64) Rect { func (r Rect) LeftHalf() Rect { return r.CutLeft(0.5 * r.W()) } + // Returns the right half of the rectangle. Doesn't modify the original rectangle func (r Rect) RightHalf() Rect { return r.CutRight(0.5 * r.W()) @@ -239,6 +240,7 @@ func (r Rect) RightHalf() Rect { func (r Rect) TopHalf() Rect { return r.CutTop(0.5 * r.H()) } + // Returns the bottom half of the rectangle. Doesn't modify the original rectangle func (r Rect) BottomHalf() Rect { return r.CutBottom(0.5 * r.H()) @@ -297,7 +299,6 @@ func (r Rect) PadBottom(padding float64) Rect { return r.Pad(R(0, padding, 0, 0)) } - // Adds padding to a rectangle (pads inward if padding is negative) func (r Rect) Pad(pad Rect) Rect { return R(r.Min.X-pad.Min.X, r.Min.Y-pad.Min.Y, r.Max.X+pad.Max.X, r.Max.Y+pad.Max.Y) diff --git a/spatial/shape.go b/spatial/shape.go index b84b368..4c6e743 100644 --- a/spatial/shape.go +++ b/spatial/shape.go @@ -5,9 +5,10 @@ import ( ) type ShapeType uint8 + const ( ShapeAABB ShapeType = iota // A rectangle, not rotated nor scaled - ShapeRect // Can be rotated or scaled + ShapeRect // Can be rotated or scaled // ShapeCircle // TODO: A circle // ShapeRing // TODO: A ring (Circle with excluded middle segment // ShapeEllipses // TODO: Maybe combine with circle? @@ -15,14 +16,14 @@ const ( ) type Shape struct { - Type ShapeType - Bounds glm.Rect // The bounding AABB + Type ShapeType + Bounds glm.Rect // The bounding AABB Vectors [4]glm.Vec2 // Vector data which is stored differently depending on the shape type } func AABB(rect glm.Rect) Shape { return Shape{ - Type: ShapeAABB, + Type: ShapeAABB, Bounds: rect, Vectors: [4]glm.Vec2{ rect.Min, @@ -47,7 +48,7 @@ func Rect(r glm.Rect, mat glm.Mat4) Shape { bounds := glm.R(xMin, yMin, xMax, yMax) return Shape{ - Type: ShapeRect, + Type: ShapeRect, Bounds: bounds, Vectors: [4]glm.Vec2{ bl, tl, tr, br, @@ -113,10 +114,10 @@ func polygonIntersectionCheck(a, b []glm.Vec2) bool { var l1, l2 glm.Line2 for i := range a { l1.A = a[i] - l1.B = a[(i+1) % lenA] + l1.B = a[(i+1)%lenA] for j := range b { l2.A = b[j] - l2.B = b[(j+1) % lenB] + l2.B = b[(j+1)%lenB] if l1.Intersects(l2) { return true @@ -153,19 +154,19 @@ func polygonContainsPoint(poly []glm.Vec2, point glm.Vec2) bool { var inside = false for i := range poly { a := poly[i] - b := poly[(i+1) % length] + b := poly[(i+1)%length] xi := a.X yi := a.Y xj := b.X yj := b.Y - intersect := ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi) - if (intersect) { + intersect := ((yi > y) != (yj > y)) && (x < (xj-xi)*(y-yi)/(yj-yi)+xi) + if intersect { inside = !inside } } - return inside; + return inside } // Note: Originally I was doing this, but it actually takes way more projections than I thought. So I opted to just do a generic line intersection test for all edges of both rectangles. Even though this one may be faster. Maybe I"ll add it back in the future @@ -242,4 +243,3 @@ func polygonContainsPoint(poly []glm.Vec2, point glm.Vec2) bool { // return false // } - diff --git a/spatial/shape_test.go b/spatial/shape_test.go index 6b66491..4e27f32 100644 --- a/spatial/shape_test.go +++ b/spatial/shape_test.go @@ -9,7 +9,7 @@ import ( ) type intersectTest struct { - a, b Shape + a, b Shape expected bool } @@ -101,49 +101,47 @@ func TestIntersectAABB(t *testing.T) { } } - func TestIntersectRect(t *testing.T) { baseRect5 := glm.CR(5) // baseRect10 := glm.CR(10) tests := []intersectTest{ { - Rect(baseRect5, *glm.IM4().Rotate(math.Pi / 4, glm.Vec3{0, 0, 1})), + Rect(baseRect5, *glm.IM4().Rotate(math.Pi/4, glm.Vec3{0, 0, 1})), Rect(baseRect5, *glm.IM4().Translate(0, 0, 0)), true, }, { - Rect(baseRect5, *glm.IM4().Rotate(math.Pi / 4, glm.Vec3{0, 0, 1})), + Rect(baseRect5, *glm.IM4().Rotate(math.Pi/4, glm.Vec3{0, 0, 1})), Rect(baseRect5, *glm.IM4().Translate(5, 0, 0)), true, }, { // Rotated 45 degrees and nudged inward - Rect(baseRect5, *glm.IM4().Rotate(math.Pi / 4, glm.Vec3{0, 0, 1})), - Rect(baseRect5, *glm.IM4().Translate(5 + 7.07106781187 - 0.001, 0, 0)), + Rect(baseRect5, *glm.IM4().Rotate(math.Pi/4, glm.Vec3{0, 0, 1})), + Rect(baseRect5, *glm.IM4().Translate(5+7.07106781187-0.001, 0, 0)), true, }, { // Rotated 45 degrees and nudged outward - Rect(baseRect5, *glm.IM4().Rotate(math.Pi / 4, glm.Vec3{0, 0, 1})), - Rect(baseRect5, *glm.IM4().Translate(5 + 7.07106781187 + 0.001, 0, 0)), + Rect(baseRect5, *glm.IM4().Rotate(math.Pi/4, glm.Vec3{0, 0, 1})), + Rect(baseRect5, *glm.IM4().Translate(5+7.07106781187+0.001, 0, 0)), false, }, { // Rotated 45 degrees and nudged inward - Rect(baseRect5, *glm.IM4().Rotate(math.Pi / 4, glm.Vec3{0, 0, 1}).Translate(10, 10, 0)), - Rect(baseRect5, *glm.IM4().Translate(5 + 7.07106781187 - 0.001, 0, 0).Translate(10, 10, 0)), + Rect(baseRect5, *glm.IM4().Rotate(math.Pi/4, glm.Vec3{0, 0, 1}).Translate(10, 10, 0)), + Rect(baseRect5, *glm.IM4().Translate(5+7.07106781187-0.001, 0, 0).Translate(10, 10, 0)), true, }, { // Rotated 45 degrees and both translated - Rect(baseRect5, *glm.IM4().Rotate(math.Pi / 4, glm.Vec3{0, 0, 1}).Translate(10, 10, 0)), - Rect(baseRect5, *glm.IM4().Translate(5 + 7.07106781187 + 0.001, 0, 0).Translate(10, 10, 0)), + Rect(baseRect5, *glm.IM4().Rotate(math.Pi/4, glm.Vec3{0, 0, 1}).Translate(10, 10, 0)), + Rect(baseRect5, *glm.IM4().Translate(5+7.07106781187+0.001, 0, 0).Translate(10, 10, 0)), false, }, - } for i := range tests {