float32 -> float64
This commit is contained in:
parent
bfa28464d6
commit
461ee96e83
10
collision.go
10
collision.go
@ -51,7 +51,7 @@ func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, *Point2d) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProjectPolygon(axis Vector2d, p Polygon2d) (min float32, max float32) {
|
func ProjectPolygon(axis Vector2d, p Polygon2d) (min float64, max float64) {
|
||||||
min = axis.Dot(p.Origin.Add(p.Points[0]).ToVector())
|
min = axis.Dot(p.Origin.Add(p.Points[0]).ToVector())
|
||||||
max = min
|
max = min
|
||||||
for _, point := range p.Points {
|
for _, point := range p.Points {
|
||||||
@ -72,7 +72,7 @@ func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, *Point2d) {
|
|||||||
// inside situations
|
// inside situations
|
||||||
intersection := false
|
intersection := false
|
||||||
var iPoint *Point2d
|
var iPoint *Point2d
|
||||||
distance_sq := float32(math.MaxFloat32)
|
distance_sq := math.MaxFloat64
|
||||||
|
|
||||||
for point, _ := range r.Points {
|
for point, _ := range r.Points {
|
||||||
edgePt, edgeVec := r.Edge(point)
|
edgePt, edgeVec := r.Edge(point)
|
||||||
@ -95,7 +95,7 @@ func PointInPolygon(p Point2d, py Polygon2d) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func IntervalDistance(p1min, p1max, p2min, p2max float32) float32 {
|
func IntervalDistance(p1min, p1max, p2min, p2max float64) float64 {
|
||||||
if p1min < p2min {
|
if p1min < p2min {
|
||||||
return p2min - p1max
|
return p2min - p1max
|
||||||
} else {
|
} else {
|
||||||
@ -110,7 +110,7 @@ func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d) (bool, bool,
|
|||||||
will_intersect := true
|
will_intersect := true
|
||||||
translation := Vector2d{}
|
translation := Vector2d{}
|
||||||
edges := []Vector2d{}
|
edges := []Vector2d{}
|
||||||
minimum_interval_distance := float32(math.MaxFloat32)
|
minimum_interval_distance := math.MaxFloat64
|
||||||
|
|
||||||
for point, _ := range p1.Points {
|
for point, _ := range p1.Points {
|
||||||
_, edgeVec := p1.Edge(point)
|
_, edgeVec := p1.Edge(point)
|
||||||
@ -151,7 +151,7 @@ func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d) (bool, bool,
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
interval_distance := float32(math.Abs(float64(dist)))
|
interval_distance := math.Abs(dist)
|
||||||
if interval_distance < minimum_interval_distance {
|
if interval_distance < minimum_interval_distance {
|
||||||
minimum_interval_distance = interval_distance
|
minimum_interval_distance = interval_distance
|
||||||
translation = axis
|
translation = axis
|
||||||
|
38
geometry.go
38
geometry.go
@ -31,13 +31,13 @@ func (p Polygon2d) Edge(i int) (Point2d, Vector2d) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Vector2d struct {
|
type Vector2d struct {
|
||||||
X float32 `json:"x"`
|
X float64 `json:"x"`
|
||||||
Y float32 `json:"y"`
|
Y float64 `json:"y"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Point2d struct {
|
type Point2d struct {
|
||||||
X float32 `json:"x"`
|
X float64 `json:"x"`
|
||||||
Y float32 `json:"y"`
|
Y float64 `json:"y"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const Epsilon = 1e-7
|
const Epsilon = 1e-7
|
||||||
@ -52,27 +52,27 @@ func (p Point2d) Add(v Vector2d) Point2d {
|
|||||||
return Point2d{p.X + v.X, p.Y + v.Y}
|
return Point2d{p.X + v.X, p.Y + v.Y}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Vector2d) Mag() float32 {
|
func (v Vector2d) Mag() float64 {
|
||||||
return float32(math.Abs(math.Sqrt(float64(v.X*v.X) + float64(v.Y*v.Y))))
|
return math.Abs(math.Sqrt(v.X*v.X + v.Y*v.Y))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Vector2d) MagSquared() float32 {
|
func (v Vector2d) MagSquared() float64 {
|
||||||
return float32(math.Abs(float64(v.X*v.X) + float64(v.Y*v.Y)))
|
return math.Abs(v.X*v.X + v.Y*v.Y)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Vector2d) PopPop() float32 {
|
func (v Vector2d) PopPop() float64 {
|
||||||
return v.Mag()
|
return v.Mag()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Vector2d) Scale(s float32) Vector2d {
|
func (v Vector2d) Scale(s float64) Vector2d {
|
||||||
return Vector2d{v.X * s, v.Y * s}
|
return Vector2d{v.X * s, v.Y * s}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v1 Vector2d) Cross(v2 Vector2d) float32 {
|
func (v1 Vector2d) Cross(v2 Vector2d) float64 {
|
||||||
return v1.X*v2.Y - v1.Y*v2.X
|
return v1.X*v2.Y - v1.Y*v2.X
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v1 Vector2d) Dot(v2 Vector2d) float32 {
|
func (v1 Vector2d) Dot(v2 Vector2d) float64 {
|
||||||
return (v1.X * v2.X) + (v1.Y * v2.Y)
|
return (v1.X * v2.X) + (v1.Y * v2.Y)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,17 +84,17 @@ func (v Vector2d) ToPoint() Point2d {
|
|||||||
return Point2d{v.X, v.Y}
|
return Point2d{v.X, v.Y}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v1 Vector2d) Rotate(a float32) Vector2d {
|
func (v1 Vector2d) Rotate(a float64) Vector2d {
|
||||||
x := float64(v1.X)*math.Cos(float64(a)) - float64(v1.Y)*math.Sin(float64(a))
|
x := float64(v1.X)*math.Cos(float64(a)) - float64(v1.Y)*math.Sin(float64(a))
|
||||||
y := float64(v1.X)*math.Sin(float64(a)) + float64(v1.Y)*math.Cos(float64(a))
|
y := float64(v1.X)*math.Sin(float64(a)) + float64(v1.Y)*math.Cos(float64(a))
|
||||||
return Vector2d{float32(x), float32(y)}
|
return Vector2d{x, y}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PointInRect(p Point2d, r AABB2d) bool {
|
func PointInRect(p Point2d, r AABB2d) bool {
|
||||||
return (p.X > r.A.X) && (p.X < r.B.X) && (p.Y > r.A.Y) && (p.Y < r.B.Y)
|
return (p.X > r.A.X) && (p.X < r.B.X) && (p.Y > r.A.Y) && (p.Y < r.B.Y)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AASquareAtPoint(p Point2d, edgeLength float32) AABB2d {
|
func AASquareAtPoint(p Point2d, edgeLength float64) AABB2d {
|
||||||
size := edgeLength / 2.0
|
size := edgeLength / 2.0
|
||||||
return AABB2d{
|
return AABB2d{
|
||||||
A: Point2d{X: p.X - size, Y: p.Y - size},
|
A: Point2d{X: p.X - size, Y: p.Y - size},
|
||||||
@ -113,7 +113,7 @@ func (r AABB2d) ToPolygon() Polygon2d {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func OrientedSquare(center Point2d, heading Vector2d, size float32) Polygon2d {
|
func OrientedSquare(center Point2d, heading Vector2d, size float64) Polygon2d {
|
||||||
out := Polygon2d{}
|
out := Polygon2d{}
|
||||||
out.Origin.X = center.X
|
out.Origin.X = center.X
|
||||||
out.Origin.Y = center.Y
|
out.Origin.Y = center.Y
|
||||||
@ -136,12 +136,12 @@ func (v Vector2d) Normalize() Vector2d {
|
|||||||
return Vector2d{v.X / m, v.Y / m}
|
return Vector2d{v.X / m, v.Y / m}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Distance(p1, p2 Point2d) float32 {
|
func Distance(p1, p2 Point2d) float64 {
|
||||||
return p1.Sub(p2).Mag()
|
return p1.Sub(p2).Mag()
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns radians
|
// returns radians
|
||||||
func Angle(v1, v2 Vector2d) float32 {
|
func Angle(v1, v2 Vector2d) float64 {
|
||||||
x := (v1.Dot(v2) / (v1.Mag() * v2.Mag()))
|
x := (v1.Dot(v2) / (v1.Mag() * v2.Mag()))
|
||||||
angle := math.Acos(float64(x))
|
angle := math.Acos(float64(x))
|
||||||
|
|
||||||
@ -158,5 +158,5 @@ func Angle(v1, v2 Vector2d) float32 {
|
|||||||
if v1.Y*v2.X > v1.X*v2.Y {
|
if v1.Y*v2.X > v1.X*v2.Y {
|
||||||
angle = angle * -1.0
|
angle = angle * -1.0
|
||||||
}
|
}
|
||||||
return float32(angle)
|
return angle
|
||||||
}
|
}
|
||||||
|
@ -37,10 +37,10 @@ func Neville(P []Point2d, ts []float64, t float64) (*Point2d, error) {
|
|||||||
order := len(P) - 1
|
order := len(P) - 1
|
||||||
for i := 0; i < order; i++ {
|
for i := 0; i < order; i++ {
|
||||||
for j := 0; j < order-i; j++ {
|
for j := 0; j < order-i; j++ {
|
||||||
var tLeft float32 = float32(ts[i+j+1] - t)
|
tLeft := ts[i+j+1] - t
|
||||||
var tRight float32 = float32(t - ts[j])
|
tRight := t - ts[j]
|
||||||
Q[j] = Q[j].ToVector().Scale(tLeft).ToPoint().Add(P[j+1].ToVector().Scale(tRight))
|
Q[j] = Q[j].ToVector().Scale(tLeft).ToPoint().Add(P[j+1].ToVector().Scale(tRight))
|
||||||
Q[j] = Q[j].ToVector().Scale(float32(1.0 / (ts[i+j+1] - ts[j]))).ToPoint()
|
Q[j] = Q[j].ToVector().Scale(1.0 / (ts[i+j+1] - ts[j])).ToPoint()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ func TestAngle(t *testing.T) {
|
|||||||
cases := []struct {
|
cases := []struct {
|
||||||
v1 Vector2d
|
v1 Vector2d
|
||||||
v2 Vector2d
|
v2 Vector2d
|
||||||
expected float32
|
expected float64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
v1: Vector2d{10, 0},
|
v1: Vector2d{10, 0},
|
||||||
@ -159,7 +159,7 @@ func TestAngle(t *testing.T) {
|
|||||||
expected: math.Pi * Rad2deg,
|
expected: math.Pi * Rad2deg,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
var a float32
|
var a float64
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
a = Angle(c.v2, c.v1)
|
a = Angle(c.v2, c.v1)
|
||||||
|
|
||||||
@ -508,11 +508,12 @@ func TestPolyPolyNoOverlapMoveOnto(t *testing.T) {
|
|||||||
func TestOrientedSquare(t *testing.T) {
|
func TestOrientedSquare(t *testing.T) {
|
||||||
p := OrientedSquare(Point2d{10, 10}, Vector2d{0.5, 0.5}, 5)
|
p := OrientedSquare(Point2d{10, 10}, Vector2d{0.5, 0.5}, 5)
|
||||||
expected := []Vector2d{
|
expected := []Vector2d{
|
||||||
{0, 7.071068},
|
{X: 0, Y: 7.071067811865475},
|
||||||
{-7.071068, 0},
|
{X: -7.071067811865475, Y: 0},
|
||||||
{0, -7.071068},
|
{X: 0, Y: -7.071067811865475},
|
||||||
{7.071068, 0},
|
{X: 7.071067811865475, Y: 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, v := range expected {
|
for i, v := range expected {
|
||||||
if p.Points[i] != v {
|
if p.Points[i] != v {
|
||||||
t.Errorf("unexpected points: %+v, expected: %+v", p.Points, expected)
|
t.Errorf("unexpected points: %+v, expected: %+v", p.Points, expected)
|
||||||
@ -522,7 +523,7 @@ func TestOrientedSquare(t *testing.T) {
|
|||||||
|
|
||||||
func TestPopPop(t *testing.T) {
|
func TestPopPop(t *testing.T) {
|
||||||
v1 := Vector2d{10, 10}
|
v1 := Vector2d{10, 10}
|
||||||
if v1.PopPop() != float32(math.Sqrt(10*10+10*10)) {
|
if v1.PopPop() != math.Sqrt(10*10+10*10) {
|
||||||
t.Errorf("Incorrect Magnitude!")
|
t.Errorf("Incorrect Magnitude!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user