Skip to content

Commit

Permalink
added some tests with an empty ellipse
Browse files Browse the repository at this point in the history
  • Loading branch information
akevalion committed Jun 17, 2020
1 parent e2233da commit 0d1d674
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/Geometry-Tests/GEllipseTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ GEllipseTest >> actualClass [
^ GEllipse
]

{ #category : #util }
GEllipseTest >> sortedIntersectionsBetween: shape and: line [
^ (shape intersectionsWith: line) asOrderedCollection
sorted: [ :a :b | a asPoint < b asPoint ].
]

{ #category : #tests }
GEllipseTest >> testArea [
ellipse := GEllipse center: 5 , -1 vertex: 10 , -1 coVertex: 5 , 2.
Expand All @@ -29,6 +35,30 @@ GEllipseTest >> testBoundaryContains [
self deny: (ellipse boundaryContains: 3, 0.13).
]

{ #category : #tests }
GEllipseTest >> testEmpty [
| shape line intersections |
shape := GEllipse center: 0@0 vertex: 0@0 coVertex: 0@0.
line := GLine through: shape center and: 10@10.
intersections := self sortedIntersectionsBetween: shape and: line.

self assert: intersections isNotEmpty.
self assert: (intersections first asPoint closeTo: 0@0).

"horizontal line "
line := GLine through: 10@20 and: 20@10.
intersections := self sortedIntersectionsBetween: shape and: line.
self assert: intersections isEmpty.

"vertical line"
"intersectionsWithVerticalLine:
does not work with an empty ellipse"
shape := GEllipse center: 0@0 vertex: 9@0 coVertex: 0@9.
line := GLine through: 10@10 and: 10@20.
intersections := self sortedIntersectionsBetween: shape and: line.
self assert: intersections isEmpty.
]

{ #category : #tests }
GEllipseTest >> testEncompassingRectangle [
ellipse := GEllipse center: 4 , -1 vertex: 9 , -1 coVertex: 4 , 2.
Expand Down Expand Up @@ -210,6 +240,17 @@ GEllipseTest >> testMinorAxis [
self assert: ellipse minorAxis equals: (GSegment with: 10 , 8 with: 10 , 12)
]

{ #category : #tests }
GEllipseTest >> testNonEmpty [
| shape line intersections |
shape := GEllipse center: 0@0 vertex: 0@10 coVertex: 5@10.
line := GLine through: shape center and: 10@10.
intersections := self sortedIntersectionsBetween: shape and: line.
self assert: intersections isNotEmpty.
self assert: (intersections first asPoint closeTo: -7.4535599249993@ -7.4535599249993).
self assert: (intersections second asPoint closeTo: 7.4535599249993@ 7.4535599249993).
]

{ #category : #tests }
GEllipseTest >> testPerimeter [
ellipse := GEllipse center: 2,6 vertex: 7, 6 coVertex: 2,9.
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/GEllipse.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ GEllipse >> intersectionsWithLine: aGLine [
a := self semiMajorAxisLength.
b := self semiMinorAxisLength.

(a = b and: [ a = 0 ]) ifTrue: [ ^ {center} asSet ].
m := aGLine a / aGLine b negated.
c := aGLine c / aGLine b negated.
e := c - k.
Expand All @@ -160,6 +159,7 @@ GEllipse >> intersectionsWithLine: aGLine [
tk := t * k.
sqrtContent := (a2m2 + b2 - t2 - k2 + (2 * tk)).
sqrtContent >= 0 ifFalse: [ ^ { } ]. "No intersections"
(a = b and: [ a = 0 ]) ifTrue: [ ^ Set with: center ].
sqrt := (a2m2 + b2 - t2 - k2 + (2 * tk)) sqrt.
x1 := (h * b2 - (m * a2 * e) + (ab * sqrt)) / (a2m2 + b2).
x2 := (h * b2 - (m * a2 * e) - (ab * sqrt)) / (a2m2 + b2).
Expand Down

0 comments on commit 0d1d674

Please sign in to comment.