Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for issue with testHashMethodNeedsToBeInComparingProtocol #2

Merged
merged 7 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.