-
Notifications
You must be signed in to change notification settings - Fork 327
Improving PdfPoint #54
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -82,6 +82,39 @@ internal PdfVector ToVector() | |||||
| return new PdfVector(X, Y); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Converts this <see cref="PdfPoint"/> into an array. | ||||||
| /// </summary> | ||||||
| /// <returns></returns> | ||||||
| public double[] ToDouble() | ||||||
| { | ||||||
| return new double[] { (double)this.X, (double)this.Y }; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Returns a value indicating whether this <see cref="PdfPoint"/> is equal to a specified <see cref="PdfPoint"/> . | ||||||
| /// </summary> | ||||||
| /// <param name="obj"></param> | ||||||
| /// <returns></returns> | ||||||
| public override bool Equals(object obj) | ||||||
| { | ||||||
| if (obj is PdfPoint) | ||||||
|
Member
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. There's a slightly terser syntax for this:
Suggested change
Then you can remove line 103, but I don't mind, I just know Resharper is going to nag me 😆
Collaborator
Author
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. Very good, thx! |
||||||
| { | ||||||
| PdfPoint point = (PdfPoint)obj; | ||||||
| return point.X == this.X && point.Y == this.Y; | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Returns the hash code for this <see cref="PdfPoint"/>. | ||||||
| /// </summary> | ||||||
| /// <returns></returns> | ||||||
| public override int GetHashCode() | ||||||
| { | ||||||
| return (int)(this.X * 10_000 + 31 * this.Y * 10_000); | ||||||
|
Member
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. Overriding
Initially I thought the approach here might be more vulnerable to collisions but now I'm not so sure, though I guess the only way to be sure is calculate the collision rate or something. I tend to treat hash codes as something like encryption in that they're much easier to use library functions for and easy to get wrong when written from scratch. I generally rely on Resharper when doing this which generates something that closely matches Jon Skeet's answer. For reference this looks something like: The other option is to let the .NET Framework do the heavy lifting by relying on the
Which turns the point into a ValueTuple and uses the implementation from that which is here: https://github.com/dotnet/roslyn/blob/master/src/Compilers/Test/Resources/Core/NetFX/ValueTuple/ValueTuple.cs#L297. Now I'm not sure what that does but I'd hope it's one of the best possible implementations.
Collaborator
Author
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. Hehe exactly. Let's .Net do the job then! |
||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Get a string representation of this point. | ||||||
| /// </summary> | ||||||
|
|
||||||
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.
I wonder if this would be better as a
ValueTuplerather than an array. Since the array will always be known to have 2 members a tuple has a couple of benefits:This would look something like:
I think PdfPig already includes ValueTuple as a dependency (it's a nuget package for earlier versions of the .NET Framework) so all consumers will be able to use it.
(secondary nitpick on the empty
<returns>tags).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.
On this, I would really need a double array for computation purpose...
Uh oh!
There was an error while loading. Please reload this page.
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.
Hmm, I'd be tempted to leave this off the public API for now. My gut instinct is that it'll confuse consumers of the library over the alternative of having a tuple. You could add:
Either to your consumer code or to PdfPig (or internally to PdfPig if it's for layout analysis code internally) but I'd avoid having it as a method directly on the public API of
PdfPointfor now to reduce the amount of autocomplete options.Uh oh!
There was an error while loading. Please reload this page.
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.
okay, you are right. Let's keep it simple and leave it out for the moment