Skip to content

Commit

Permalink
Merge pull request #2 from akevalion/master
Browse files Browse the repository at this point in the history
Update for issue with testHashMethodNeedsToBeInComparingProtocol
  • Loading branch information
noha authored Sep 2, 2020
2 parents 65a03cf + d890f34 commit 28e1b67
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
24 changes: 24 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,13 @@ GEllipseTest >> testBoundaryContains [
self deny: (ellipse boundaryContains: 3, 0.13).
]

{ #category : #tests }
GEllipseTest >> testEmpty [
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 }
GEllipseTest >> testEncompassingRectangle [
ellipse := GEllipse center: 4 , -1 vertex: 9 , -1 coVertex: 4 , 2.
Expand Down Expand Up @@ -210,6 +223,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
21 changes: 20 additions & 1 deletion src/Geometry/GEllipse.class.st
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
"
I am ellipse I have center and 2 vertices.
Example
```Smalltalk
""circle radius = 10""
GEllipse center: 0@0 vertex: 0@10 coVertex: 10@0
```
"
Class {
#name : #GEllipse,
#superclass : #GShape,
Expand All @@ -11,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 @@ -133,6 +151,7 @@ GEllipse >> intersectionsWithLine: aGLine [
k := center y.
a := self semiMajorAxisLength.
b := self semiMinorAxisLength.

m := aGLine a / aGLine b negated.
c := aGLine c / aGLine b negated.
e := c - k.
Expand All @@ -148,8 +167,8 @@ GEllipse >> intersectionsWithLine: aGLine [
tk := t * k.
sqrtContent := (a2m2 + b2 - t2 - k2 + (2 * tk)).
sqrtContent >= 0 ifFalse: [ ^ { } ]. "No intersections"

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).
y1 := (b2 * t + (k * a2m2) + (abm * sqrt)) / (a2m2 + b2).
Expand Down
5 changes: 5 additions & 0 deletions src/Geometry/GPoint.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ GPoint >> asGPoint [
^ self
]

{ #category : #converting }
GPoint >> asPoint [
^ self x @ self y
]

{ #category : #accessing }
GPoint >> coordinates [
^ coordinates
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/GSegment.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ GSegment >> distanceTo: aGPoint [
^ self asGLine distanceTo: aGPoint
]

{ #category : #initialization }
{ #category : #comparing }
GSegment >> hash [
^ v1 hash bitXor: v2 hash
]
Expand Down
8 changes: 0 additions & 8 deletions src/Geometry/ManifestGeometry.class.st

This file was deleted.

0 comments on commit 28e1b67

Please sign in to comment.