-
Notifications
You must be signed in to change notification settings - Fork 3
/
rect.go
39 lines (32 loc) · 1.02 KB
/
rect.go
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
package cs
import "math"
type Rect struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Width float64 `json:"width,omitempty"`
Height float64 `json:"height,omitempty"`
}
func (rect Rect) Center() Vector {
return Vector{
rect.X + rect.Width/2,
rect.Y + rect.Height/2,
}
}
func (rect Rect) PointInRectangle(point Vector) bool {
return point.X >= rect.X && point.X <= (rect.X+rect.Width) && point.Y >= rect.Y && point.Y <= (rect.Y+rect.Height)
}
func (rect Rect) PointInRotatedRectangle(point Vector, rectAngle float64) bool {
// rotate around rectangle center by -rectAngle
var s = math.Sin(-rectAngle)
var c = math.Cos(-rectAngle)
// set origin to rect center
center := rect.Center()
newPoint := point.Subtract(center)
// rotate
newPoint = Vector{newPoint.X*c - newPoint.Y*s, newPoint.X*s + newPoint.Y*c}
// put origin back
newPoint = newPoint.Add(center)
// check if our transformed point is in the rectangle, which is no longer
// rotated relative to the point
return rect.PointInRectangle(newPoint)
}