mirror of https://github.com/mum4k/termdash.git
Moving some functions from numbers -> trig.
This commit is contained in:
parent
6276788be2
commit
286731aa62
|
@ -16,7 +16,6 @@
|
||||||
package numbers
|
package numbers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -163,31 +162,3 @@ func RadiansToDegrees(radians float64) int {
|
||||||
}
|
}
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
// CirclePointAtAngle given an angle in degrees and a circle midpoint and
|
|
||||||
// radius, calculates coordinates of a point on the circle at that angle.
|
|
||||||
// Angles are zero at the X axis and grow counter-clockwise.
|
|
||||||
func CirclePointAtAngle(degrees int, mid image.Point, radius int) image.Point {
|
|
||||||
angle := DegreesToRadians(degrees)
|
|
||||||
r := float64(radius)
|
|
||||||
x := mid.X + int(Round(r*math.Cos(angle)))
|
|
||||||
// Y coordinates grow down on the canvas.
|
|
||||||
y := mid.Y - int(Round(r*math.Sin(angle)))
|
|
||||||
return image.Point{x, y}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CircleAngleAtPoint given a point on a circle and its midpoint,
|
|
||||||
// calculates the angle in degrees.
|
|
||||||
// Angles are zero at the X axis and grow counter-clockwise.
|
|
||||||
func CircleAngleAtPoint(point, mid image.Point) int {
|
|
||||||
adj := float64(point.X - mid.X)
|
|
||||||
opp := float64(mid.Y - point.Y)
|
|
||||||
if opp != 0 {
|
|
||||||
angle := math.Atan2(opp, adj)
|
|
||||||
return RadiansToDegrees(angle)
|
|
||||||
} else if adj >= 0 {
|
|
||||||
return 0
|
|
||||||
} else {
|
|
||||||
return 180
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -16,7 +16,6 @@ package numbers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
|
||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -328,55 +327,3 @@ func TestRadiansToDegrees(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCirclePointAtAngleAndAngle(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
degrees int
|
|
||||||
mid image.Point
|
|
||||||
radius int
|
|
||||||
want image.Point
|
|
||||||
}{
|
|
||||||
{0, image.Point{0, 0}, 1, image.Point{1, 0}},
|
|
||||||
{90, image.Point{0, 0}, 1, image.Point{0, -1}},
|
|
||||||
{180, image.Point{0, 0}, 1, image.Point{-1, 0}},
|
|
||||||
{270, image.Point{0, 0}, 1, image.Point{0, 1}},
|
|
||||||
|
|
||||||
// Non-zero mid point.
|
|
||||||
{0, image.Point{5, 5}, 1, image.Point{6, 5}},
|
|
||||||
{90, image.Point{5, 5}, 1, image.Point{5, 4}},
|
|
||||||
{180, image.Point{5, 5}, 1, image.Point{4, 5}},
|
|
||||||
{270, image.Point{5, 5}, 1, image.Point{5, 6}},
|
|
||||||
{0, image.Point{1, 1}, 1, image.Point{2, 1}},
|
|
||||||
{90, image.Point{1, 1}, 1, image.Point{1, 0}},
|
|
||||||
{180, image.Point{1, 1}, 1, image.Point{0, 1}},
|
|
||||||
{270, image.Point{1, 1}, 1, image.Point{1, 2}},
|
|
||||||
|
|
||||||
// Larger radius.
|
|
||||||
{0, image.Point{0, 0}, 11, image.Point{11, 0}},
|
|
||||||
{90, image.Point{0, 0}, 11, image.Point{0, -11}},
|
|
||||||
{180, image.Point{0, 0}, 11, image.Point{-11, 0}},
|
|
||||||
{270, image.Point{0, 0}, 11, image.Point{0, 11}},
|
|
||||||
|
|
||||||
// Other angles.
|
|
||||||
{27, image.Point{0, 0}, 11, image.Point{10, -5}},
|
|
||||||
{68, image.Point{0, 0}, 11, image.Point{4, -10}},
|
|
||||||
{333, image.Point{2, 2}, 2, image.Point{4, 3}},
|
|
||||||
{153, image.Point{2, 2}, 2, image.Point{0, 1}},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range tests {
|
|
||||||
t.Run(fmt.Sprintf("CirclePointAtAngle %v %v %v", tc.degrees, tc.mid, tc.radius), func(t *testing.T) {
|
|
||||||
got := CirclePointAtAngle(tc.degrees, tc.mid, tc.radius)
|
|
||||||
if got != tc.want {
|
|
||||||
t.Errorf("CirclePointAtAngle(%v, %v, %v) => %v, want %v", tc.degrees, tc.mid, tc.radius, got, tc.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
t.Run(fmt.Sprintf("CircleAngleAtPoint %v %v", tc.want, tc.mid), func(t *testing.T) {
|
|
||||||
got := CircleAngleAtPoint(tc.want, tc.mid)
|
|
||||||
want := tc.degrees
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("CircleAngleAtPoint(%v, %v) => %v, want %v", tc.want, tc.mid, got, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue