-
Notifications
You must be signed in to change notification settings - Fork 2
Implement operators and equality comparison in Point and Size #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac1a89b
Overload equality and hashcode on Size coretype
glen3b 4c458f5
Overrode Point equality and hashcode
glen3b 6662b2f
Implemented Point addition, subtraction, and unary negation
glen3b 211ebbe
Implemented Size addition and subtraction
glen3b f539fd3
Add scalar multiplication and divison operators to size
glen3b 6aba387
Add scalar multiplication and divison operators to point type
glen3b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,5 +15,64 @@ public Point(int x, int y) | |
|
|
||
| public int X; | ||
| public int Y; | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| // Uses http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode | ||
| unchecked // Overflow is fine, just wrap | ||
| { | ||
| int hash = 37; | ||
| hash = hash * 67 + X.GetHashCode(); | ||
| hash = hash * 67 + Y.GetHashCode(); | ||
| return hash; | ||
| } | ||
| } | ||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| if (obj == null || !(obj is Point)) | ||
| { | ||
| return false; | ||
| } | ||
| Point oth = (Point)obj; | ||
| return X == oth.X && Y == oth.Y; | ||
| } | ||
|
|
||
| public static bool operator ==(Point left, Point right) | ||
| { | ||
| // Struct, so no need for null check | ||
| return left.X == right.X && left.Y == right.Y; | ||
| } | ||
|
|
||
| public static bool operator !=(Point left, Point right) | ||
| { | ||
| // Struct, so no need for null check | ||
| return left.X != right.X || left.Y != right.Y; | ||
| } | ||
|
|
||
| public static Point operator +(Point left, Point right) | ||
| { | ||
| return new Point(left.X + right.X, left.Y + right.Y); | ||
| } | ||
|
|
||
| public static Point operator -(Point left, Point right) | ||
| { | ||
| return new Point(left.X - right.X, left.Y - right.Y); | ||
| } | ||
|
|
||
| public static Point operator -(Point unary) | ||
| { | ||
| return new Point(-unary.X, -unary.Y); | ||
| } | ||
|
|
||
| public static Point operator *(Point point, int scalar) | ||
| { | ||
| return new Point(point.X * scalar, point.Y * scalar); | ||
| } | ||
|
|
||
| public static Point operator /(Point point, int scalar) | ||
| { | ||
| return new Point(point.X / scalar, point.Y / scalar); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding/subtracting points is not really meaningful mathematically, but I understand we're treating these as Vectors essentially. If we later find a need for a Vector class, these methods should instead be implemented there. At the moment, it's convenient to have them though. |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for referencing the source SO post, great read!