-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshapes.go
More file actions
80 lines (60 loc) · 2.49 KB
/
shapes.go
File metadata and controls
80 lines (60 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package coll
import (
"github.com/setanarut/v"
)
// OBB represents an Oriented Bounding Box
type OBB struct {
Pos v.Vec // Center position of the box.
Half v.Vec // Half-extents (half dimensions) from the center.
Angle float64 // Rotation angle from center (Pos). Unit in radians. (Clockwise)
}
// AABB represents an Axis-Aligned Bounding Box.
type AABB struct {
Pos v.Vec // Center position of the box.
Half v.Vec // Half-extents (half dimensions) from the center.
}
// Returns the left x-coordinate of the box.
func (a *AABB) Left() float64 { return a.Pos.X - a.Half.X }
// Returns the right x-coordinate of the box.
func (a *AABB) Right() float64 { return a.Pos.X + a.Half.X }
// Returns the top y-coordinate of the box (usually smaller y).
func (a *AABB) Top() float64 { return a.Pos.Y - a.Half.Y }
// Returns the bottom y-coordinate of the box (usually larger y).
func (a *AABB) Bottom() float64 { return a.Pos.Y + a.Half.Y }
// Sets the left x-coordinate and updates Pos.X accordingly.
func (a *AABB) SetLeft(l float64) { a.Pos.X = l + a.Half.X }
// Sets the right x-coordinate and updates Pos.X accordingly.
func (a *AABB) SetRight(r float64) { a.Pos.X = r - a.Half.X }
// Sets the top y-coordinate and updates Pos.Y accordingly.
func (a *AABB) SetTop(t float64) { a.Pos.Y = t + a.Half.Y }
// Sets the bottom y-coordinate and updates Pos.Y accordingly.
func (a *AABB) SetBottom(b float64) { a.Pos.Y = b - a.Half.Y }
// Returns the total width (X dimension) of the box.
func (a *AABB) Width() float64 { return a.Half.X * 2 }
// Returns the total height (Y dimension) of the box.
func (a *AABB) Height() float64 { return a.Half.Y * 2 }
// Returns top-left point
func (a *AABB) Min() v.Vec { return v.Vec{a.Left(), a.Top()} }
// Returns bottom-right point
func (a *AABB) Max() v.Vec { return v.Vec{a.Right(), a.Bottom()} }
// Circle represents a circular bounding volume.
type Circle struct {
Pos v.Vec // Center position of the circle.
Radius float64 // The radius of the circle.
}
// Segment shape with A and B points.
type Segment struct {
A, B v.Vec
}
// NewAABB returns new AABB
func NewAABB(centerX, centerY, halfWidth, halfHeight float64) *AABB {
return &AABB{Pos: v.Vec{centerX, centerY}, Half: v.Vec{halfWidth, halfHeight}}
}
// NewCircle returns new Circle
func NewCircle(x, y, radius float64) *Circle {
return &Circle{Pos: v.Vec{x, y}, Radius: radius}
}
// NewSegment returns new Segment
func NewSegment(ax, ay, bx, by float64) *Segment {
return &Segment{A: v.Vec{ax, ay}, B: v.Vec{bx, by}}
}