Skip to content

Commit

Permalink
Add point rotation.
Browse files Browse the repository at this point in the history
Part of #19
  • Loading branch information
jecisc committed Jan 20, 2019
1 parent d1cfd78 commit 43dc779
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Geometry-Tests/GPointTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ GPointTest >> testIntersectionsWithSegment [
self assertCollection: (point intersectionsWith: segment) hasSameElements: (point intersectionsWithSegment: segment)
]

{ #category : #tests }
GPointTest >> testRotateBy [
point := 5 , 5.
point rotateBy: 90 degrees.
self assert: point =~ (-5 , 5).
point rotateBy: 90 degrees.
self assert: point =~ (-5 , -5)
]

{ #category : #tests }
GPointTest >> testRotateByAbout [
point := 5 , 5.
point rotateBy: 90 degrees about: 3 , 3.
self assert: point =~ (1 , 5).
point rotateBy: 90 degrees about: 3 , 3.
self assert: point =~ (1 , 1)
]

{ #category : #tests }
GPointTest >> testRotatedBy [
point := 5 , 5.
self assert: (point rotatedBy: 90 degrees) =~ (-5 , 5).
self deny: (point rotatedBy: 90 degrees) == point
]

{ #category : #tests }
GPointTest >> testRotatedByAbout [
point := 5 , 5.
self assert: (point rotatedBy: 90 degrees about: 3 , 3) =~ (1 , 5).
self deny: (point rotatedBy: 90 degrees about: 3 , 3) == point
]

{ #category : #tests }
GPointTest >> testTranslateBy [
| vector |
Expand Down
55 changes: 55 additions & 0 deletions src/Geometry/GPoint.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,61 @@ GPoint >> length [
^ 0
]

{ #category : #transformations }
GPoint >> rotateBy: aGAngle [
"Rotate the point around the origin (0,0) of the plan.
This will change the receiver. To get a new point use #rotatedBy:.
This is only valid for 2D points."

self rotateBy: aGAngle about: 0 , 0
]

{ #category : #transformations }
GPoint >> rotateBy: aGAngle about: aGPoint [
"Rotate the point around the another point of the plan.
This will change the receiver. To get a new point use #rotatedBy:about:.
This is only valid for 2D points."

| cos sin diffX diffY |
cos := aGAngle cos.
sin := aGAngle sin.
diffX := self x - aGPoint x.
diffY := self y - aGPoint y.
self x: cos * diffX - (sin * diffY) + aGPoint x.
self y: sin * diffX + (cos * diffY) + aGPoint y
]

{ #category : #transformations }
GPoint >> rotatedBy: aGAngle [
"Rotate the point around the origin (0,0) of the plan.
This will return a new point. To change the receiver use #rotateBy:.
This is only valid for 2D points."

^ self rotatedBy: aGAngle about: 0 , 0
]

{ #category : #transformations }
GPoint >> rotatedBy: aGAngle about: aGPoint [
"Rotate the point around another point of the plan.
This will return a new point. To change the receiver use #rotateBy:about:.
This is only valid for 2D points."

| cos sin diffX diffY |
cos := aGAngle cos.
sin := aGAngle sin.
diffX := self x - aGPoint x.
diffY := self y - aGPoint y.
^ cos * diffX - (sin * diffY) + aGPoint x , (sin * diffX + (cos * diffY) + aGPoint y)
]

{ #category : #accessing }
GPoint >> segment: aPoint [
^ GSegment with: self with: aPoint
Expand Down

0 comments on commit 43dc779

Please sign in to comment.