Skip to content

Commit

Permalink
format geometry pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Rodolfo Sanchez <[email protected]>
  • Loading branch information
dolfolife committed Feb 20, 2024
1 parent 2c007bf commit bb49fc4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
51 changes: 25 additions & 26 deletions pkg/math/geometry.go
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
}
48 changes: 24 additions & 24 deletions pkg/math/geometry_test.go
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))

}

0 comments on commit bb49fc4

Please sign in to comment.