-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rodolfo Sanchez <[email protected]>
- Loading branch information
Showing
2 changed files
with
49 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
package math | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
"math" | ||
"strconv" | ||
) | ||
|
||
type Point struct { | ||
X, Y float64 | ||
X, Y float64 | ||
} | ||
|
||
type Segment struct { | ||
A, B Point | ||
A, B Point | ||
} | ||
|
||
func CalDistancePoints(pos1 Point, pos2 Point) string { | ||
partX := math.Abs(float64(pos1.X) - float64(pos2.X)) | ||
partY := math.Abs(float64(pos1.Y) - float64(pos2.Y)) | ||
return strconv.Itoa(int(partX + partY)) | ||
partX := math.Abs(float64(pos1.X) - float64(pos2.X)) | ||
partY := math.Abs(float64(pos1.Y) - float64(pos2.Y)) | ||
return strconv.Itoa(int(partX + partY)) | ||
} | ||
|
||
|
||
func CalIntersectionPoint(a, b, c, d Point) (bool, Point) { | ||
cross := (c.Y-d.Y)*(b.X-a.X) - (c.X-d.X)*(b.Y-a.Y) | ||
if cross == 0 { | ||
return false, Point{} // Parallel lines, no intersection | ||
} | ||
|
||
t1 := float64((c.Y-d.Y)*(c.X-a.X) + (d.X-c.X)*(c.Y-a.Y)) / float64(cross) | ||
t2 := float64((a.Y-b.Y)*(c.X-a.X) + (b.X-a.X)*(c.Y-a.Y)) / float64(cross) | ||
|
||
p1 := Point{b.X - a.X, b.Y - a.Y} | ||
// Check if the intersection point is within the line segments | ||
if t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1 { | ||
intersectionX := a.X + t1*p1.X | ||
intersectionY := a.Y + t1*p1.Y | ||
return true, Point{intersectionX, intersectionY} | ||
} | ||
|
||
return false, Point{} // The intersection point is outside the line segments | ||
cross := (c.Y-d.Y)*(b.X-a.X) - (c.X-d.X)*(b.Y-a.Y) | ||
if cross == 0 { | ||
return false, Point{} // Parallel lines, no intersection | ||
} | ||
|
||
t1 := float64((c.Y-d.Y)*(c.X-a.X)+(d.X-c.X)*(c.Y-a.Y)) / float64(cross) | ||
t2 := float64((a.Y-b.Y)*(c.X-a.X)+(b.X-a.X)*(c.Y-a.Y)) / float64(cross) | ||
|
||
p1 := Point{b.X - a.X, b.Y - a.Y} | ||
// Check if the intersection point is within the line segments | ||
if t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1 { | ||
intersectionX := a.X + t1*p1.X | ||
intersectionY := a.Y + t1*p1.Y | ||
return true, Point{intersectionX, intersectionY} | ||
} | ||
|
||
return false, Point{} // The intersection point is outside the line segments | ||
} | ||
|
||
func IsValidTriangle(a, b, c int) bool { | ||
return a+b > c && a+c > b && b+c > a | ||
return a+b > c && a+c > b && b+c > a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
package math | ||
|
||
import ( | ||
"testing" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCalDistancePoints(t *testing.T) { | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: 0})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 0, Y: 5})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: 0})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 0, Y: -5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: 5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: 5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: -5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: -5})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: 0})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 0, Y: 5})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: 0})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 0, Y: -5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: 5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: 5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: -5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: -5})) | ||
} | ||
|
||
func TestCalIntersectionPoint(t *testing.T) { | ||
intersect, value := CalIntersectionPoint(Point{X: 0, Y: 0}, Point{X: 8, Y: 0}, Point{X: 4, Y: -4}, Point{X: 4, Y: 4}) | ||
assert.Equal(t, true, intersect) | ||
assert.Equal(t, Point{4,0}, value) | ||
|
||
intersect, value = CalIntersectionPoint(Point{X: 4, Y: -4}, Point{X: 4, Y: 4}, Point{X: 0, Y: 0}, Point{X: 8, Y: 0}) | ||
assert.Equal(t, true, intersect) | ||
assert.Equal(t, Point{4,0}, value) | ||
intersect, value = CalIntersectionPoint(Point{X: 0, Y: -8}, Point{X: 0, Y: -1}, Point{X: 0, Y: 0}, Point{X: 8, Y: 0}) | ||
assert.Equal(t, false, intersect) | ||
intersect, value := CalIntersectionPoint(Point{X: 0, Y: 0}, Point{X: 8, Y: 0}, Point{X: 4, Y: -4}, Point{X: 4, Y: 4}) | ||
assert.Equal(t, true, intersect) | ||
assert.Equal(t, Point{4, 0}, value) | ||
|
||
intersect, value = CalIntersectionPoint(Point{X: 4, Y: -4}, Point{X: 4, Y: 4}, Point{X: 0, Y: 0}, Point{X: 8, Y: 0}) | ||
assert.Equal(t, true, intersect) | ||
assert.Equal(t, Point{4, 0}, value) | ||
|
||
intersect, value = CalIntersectionPoint(Point{X: 0, Y: -8}, Point{X: 0, Y: -1}, Point{X: 0, Y: 0}, Point{X: 8, Y: 0}) | ||
assert.Equal(t, false, intersect) | ||
} | ||
|
||
func TestTriangleMath(t *testing.T) { | ||
|
||
assert.Equal(t, true, IsValidTriangle(1, 1, 1)) | ||
assert.Equal(t, false, IsValidTriangle(5, 10, 25)) | ||
assert.Equal(t, true, IsValidTriangle(10, 1, 10)) | ||
assert.Equal(t, true, IsValidTriangle(25, 1, 25)) | ||
assert.Equal(t, true, IsValidTriangle(1, 1, 1)) | ||
assert.Equal(t, false, IsValidTriangle(5, 10, 25)) | ||
assert.Equal(t, true, IsValidTriangle(10, 1, 10)) | ||
assert.Equal(t, true, IsValidTriangle(25, 1, 25)) | ||
|
||
} |