Skip to content

Commit

Permalink
add String functions for better printing/debugging (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
harbdog authored Aug 17, 2024
1 parent 8359877 commit dd925a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions geom/geometry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package geom

import (
"fmt"
"math"
)

Expand Down Expand Up @@ -55,6 +56,10 @@ type Vector2 struct {
X, Y float64
}

func (v *Vector2) String() string {
return fmt.Sprintf("{%0.3f,%0.3f}", v.X, v.Y)
}

func (v *Vector2) Add(v2 *Vector2) *Vector2 {
v.X += v2.X
v.Y += v2.Y
Expand Down Expand Up @@ -101,6 +106,10 @@ type Line struct {
X1, Y1, X2, Y2 float64
}

func (l *Line) String() string {
return fmt.Sprintf("{%0.3f,%0.3f->%0.3f,%0.3f}", l.X1, l.Y1, l.X2, l.Y2)
}

// Angle gets the angle of the line
func (l *Line) Angle() float64 {
return math.Atan2(l.Y2-l.Y1, l.X2-l.X1)
Expand Down
9 changes: 9 additions & 0 deletions geom3d/geometry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package geom3d

import (
"fmt"
"math"
)

Expand All @@ -11,6 +12,10 @@ type Vector3 struct {
X, Y, Z float64
}

func (v *Vector3) String() string {
return fmt.Sprintf("{%0.3f,%0.3f,%0.3f}", v.X, v.Y, v.Z)
}

func (v *Vector3) Add(v3 *Vector3) *Vector3 {
v.X += v3.X
v.Y += v3.Y
Expand Down Expand Up @@ -38,6 +43,10 @@ type Line3d struct {
X1, Y1, Z1, X2, Y2, Z2 float64
}

func (l *Line3d) String() string {
return fmt.Sprintf("{%0.3f,%0.3f,%0.3f->%0.3f,%0.3f,%0.3f}", l.X1, l.Y1, l.Z1, l.X2, l.Y2, l.Z2)
}

// Heading gets the XY axis angle of the 3-dimensional line
func (l *Line3d) Heading() float64 {
return math.Atan2(l.Y2-l.Y1, l.X2-l.X1)
Expand Down

0 comments on commit dd925a7

Please sign in to comment.