Skip to content

Commit

Permalink
go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
unitoftime committed Dec 2, 2024
1 parent 354d28c commit 1e493cf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
4 changes: 2 additions & 2 deletions glm/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
22 changes: 11 additions & 11 deletions glm/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
3 changes: 2 additions & 1 deletion glm/rect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions spatial/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ 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?
// ShapePolygon // TODO: An arbitrary convex polygon (Question: How to support arbitrary length points)
)

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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -242,4 +243,3 @@ func polygonContainsPoint(poly []glm.Vec2, point glm.Vec2) bool {

// return false
// }

24 changes: 11 additions & 13 deletions spatial/shape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type intersectTest struct {
a, b Shape
a, b Shape
expected bool
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1e493cf

Please sign in to comment.