Skip to content

Commit

Permalink
tests 50% done.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tourahi committed Aug 31, 2023
1 parent 823fb3c commit 1047a4c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
8 changes: 4 additions & 4 deletions init.moon
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ isCircleCompletelyInsideCircle = (circle1x, circle1y, circle1radius, circle2x, c

-- POLY
getSignedPolygonArea = (...) ->
points = {unpack(checkInput(...))}
points = checkInput(...)

points[#points + 1] = points[1]
points[#points + 1] = points[2]
Expand Down Expand Up @@ -984,7 +984,7 @@ getTriangleHeight = (base, ...) ->
(2 * area) / base, area

getCentroid = (...) ->
points = {unpack(checkInput(...))}
points = checkInput(...)

points[#points + 1] = points[1]
points[#points + 1] = points[2]
Expand All @@ -993,13 +993,13 @@ getCentroid = (...) ->

centroidX = (1 / (6 * area)) * ( getSummation(1, #points / 2,
(index) ->
index *= 2 - 1
index = index * 2 - 1
return ((points[index] + cycle(points, index + 2 )) * ((points[index] * cycle(points, index + 3)) - (cycle(points, index + 2) * points[index + 1])))
))

centroidY = (1 / (6 * area)) * (getSummation(1, #points / 2,
(index) ->
index *= 2 - 1
index = index * 2 - 1
return ((points[index + 1] + cycle(points, index + 3)) * ((points[index] * cycle(points, index + 3)) - (cycle(points, index + 2) * points[index + 1])))
))

Expand Down
57 changes: 57 additions & 0 deletions mm_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,60 @@ describe "circle.isCircleCompletelyInside", ->
assert.true circle.isCircleCompletelyInside( 1, 1, 2, 2, 1, 4 )
assert.true circle.isCircleCompletelyInside( 1, 1, 3, 2, 1, 4 )
assert.false circle.isCircleCompletelyInside( 8, 2, .1, 2, 1, 4 )


describe "polygon.getTriangleHeight", ->
it " :: Given points of triangle and length of base.", ->
polygon = mm.polygon
assert.multipleFUzzyEqual {polygon.getTriangleHeight( 3, 0, 0, 0, 4, 3, 0 )}, {4, 6}
assert.multipleFUzzyEqual {polygon.getTriangleHeight( 6, -2, 1, 2, 4, 4, 1 )}, {3, 9}
assert.multipleFUzzyEqual {polygon.getTriangleHeight( 3, 1, 1, 3, 4, 0, 4 )}, {3, 4.5}

it " :: Given the length of the base and the area.", ->
polygon = mm.polygon
assert.fuzzyEqual polygon.getTriangleHeight( 3, 6 ), 4
assert.fuzzyEqual polygon.getTriangleHeight( 6, 9 ), 3

describe "polygon.getSignedArea", ->
it " :: Gives the sigend area of the shape. Positive if clockwise.", ->
polygon = mm.polygon
assert.fuzzyEqual polygon.getSignedArea( 0, 0, 3, 0, 3, 4, 0, 4 ), 12
assert.fuzzyEqual polygon.getSignedArea( 0, 0, 3, 0, 0, 4 ), 6
assert.fuzzyEqual polygon.getSignedArea( 4, 4, 0, 4, 0, 0, 4, 0 ), 16

it " :: Negative if counter clock-wise.", ->
polygon = mm.polygon
assert.fuzzyEqual polygon.getSignedArea( 0, 0, 0, 4, 3, 4, 3, 0 ), -12
assert.fuzzyEqual polygon.getSignedArea( 0, 0, 0, 4, 3, 0 ), -6


describe "polygon.getArea", ->
it " :: Gives the sigend area of the shape. Positive if clockwise.", ->
polygon = mm.polygon
assert.fuzzyEqual polygon.getArea( 0, 0, 3, 0, 3, 4, 0, 4 ), 12
assert.fuzzyEqual polygon.getArea( 0, 0, 3, 0, 0, 4 ), 6
assert.fuzzyEqual polygon.getArea( 4, 4, 0, 4, 0, 0, 4, 0 ), 16

it " :: Gives the area of the shape. Negative if counter clock-wise.", ->
polygon = mm.polygon
assert.fuzzyEqual polygon.getArea( 0, 0, 0, 4, 3, 4, 3, 0 ), 12
assert.fuzzyEqual polygon.getArea( 0, 0, 0, 4, 3, 0 ), 6

describe "polygon.getCentroid", ->
it " :: Gives the x and y of the centroid.", ->
polygon = mm.polygon
assert.multipleFUzzyEqual {polygon.getCentroid( 0, 0, 0, 4, 4, 4, 4, 0 )}, { 2, 2 }
assert.multipleFUzzyEqual {polygon.getCentroid( 0, 0, 0, 6, 3, 0 ) }, { 1, 2 }
assert.multipleFUzzyEqual {polygon.getCentroid( 2, -1, 2, 1, 1, 2, -1, 2, -2, 1, -2, -1, -1, -2, 1, -2 ) }, { 0, 0 }
assert.multipleFUzzyEqual {polygon.getCentroid( 2, 0, 3, 0, 4, 1, 3, 2, 2, 2, 1, 1 ) }, { 2.5, 1 }
assert.multipleFUzzyEqual {polygon.getCentroid( 3, 5, 2, 2, 4, 2 ) }, { 3, 3 }

describe "polygon.isSegmentInside", ->
it " :: Returns true if the segment is fully inside the polygon.", ->
polygon = mm.polygon
assert.true polygon.isSegmentInside( 4, 4, 4, 5, 4, 3, 2, 5, 3, 6, 5, 6, 6, 5 )
assert.false polygon.isSegmentInside( 6, 3, 7, 6, 4, 3, 2, 5, 3, 6, 5, 6, 6, 5 )

it " :: True if at least part of the segment is on/inside.", ->
polygon = mm.polygon
assert.true polygon.isSegmentInside( 6, 3, 4, 5, 4, 3, 2, 5, 3, 6, 5, 6, 6, 5 )
5 changes: 2 additions & 3 deletions vec2D_spec.moon
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
m = assert require 'moon'
dump = m.p

mm = assert require "init"
vec2D = mm.vec2D!
vec2DArr = mm.vec2DArr!
vec2D = assert require "vec2D"
vec2DArr = assert require "vec2DArr"

checkFuzzy = (a, b) ->
(a - .00001 <= b and b <= a + .00001)
Expand Down

0 comments on commit 1047a4c

Please sign in to comment.