Skip to content

Commit

Permalink
In geometry there are not empty circles or rectangles
Browse files Browse the repository at this point in the history
  • Loading branch information
akevalion committed Jun 17, 2020
1 parent e33bcad commit 89a4bf8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
23 changes: 3 additions & 20 deletions src/Geometry-Tests/GEllipseTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,9 @@ GEllipseTest >> testBoundaryContains [

{ #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.
self should: [ GEllipse center: 0,0 vertex: 0,0 coVertex: 0,0. ] raise: Error.
self should: [ GEllipse center: 10,10 vertex: 10,10 coVertex: 10,10. ] raise: Error.
self should: [ GEllipse center: 0,0 vertex: 0,0 coVertex: 0,3. ] raise: Error.
]

{ #category : #tests }
Expand Down
10 changes: 9 additions & 1 deletion src/Geometry/GEllipse.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ Class {

{ #category : #'instance creation' }
GEllipse class >> center: aGPoint vertex: aGPoint2 coVertex: aGPoint3 [
| d1 d2 zero |
d1 := aGPoint - aGPoint2.
d2 := aGPoint - aGPoint3.
zero := GVector x: 0 y: 0."Zero vector"
(d1 = zero and: [ d2 = zero ]) ifTrue: [
self error: 'This is not an ellipse but a point.' ].
(d1 = zero or: [ d2 = zero ]) ifTrue: [
self error: 'This is not an ellipse but a line.' ].
^ self new
center: aGPoint;
vertex: aGPoint2;
Expand Down Expand Up @@ -159,7 +167,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 89a4bf8

Please sign in to comment.