Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add String functions for better printing/debugging #40

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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