Best practice for double coordinates for Point
Asked Answered
E

3

9

There is System.Drawing.Point which accepts int values for X and Y coordinate paramenters.
This is not what I need.

Then there is System.Windows.Point which accepts double values for X and Y. This is what I'd need, however I am not sure if this is the structure I should be using in a class library project.

Lastly, I found System.Drawing.PointF, but it accepts float values for X and Y coordinate parameters. I'd prefer to use double.

What is the best practice?

Further clarification:

Basically, I have a WPF project and a Class Library project, which serves as a model to the UI. I need to remember Rectangle coodinates (double, double) in the model.

Endicott answered 31/12, 2013 at 13:56 Comment(3)
Where do you intend to use them? The classes you mention exist (presumably) because they correspond to the types that those APIs need. If you're doing your own thing, can't you just define your own?Since
My intention was not to define something when there is more than one option out there. Basically, I have a WPF project and a Class Library project, which serves as a model to the UI. I need to remember Rectangle coodinates (double, double) in the model.Endicott
Another advantage of using your own class is that you can convert to/from other classes as you need to. If you need a Point class in your model, you do not require the dependency to a graphical library from the Framework. In other words, it improves decoupling.Orchard
Z
6

If you do not use anything else from System.Drawing or System.Windows implement your own PointD which uses double values. I also did that in a previous project... shouldn't be a big deal.

Zeist answered 31/12, 2013 at 13:59 Comment(2)
And if you need it to be compatible with PointF or Point, you could always make it inherit those with the appropriate trunction/rounding logic as it fits your needs.Klystron
@JeffBridgman late to the show, but just want to point out that those are Structs and cannot be inherited.Grettagreuze
L
3

If you use GDI+ (the native API behind System.Drawing, used a lot in Windows Forms), you have to use System.Drawing.Point or System.Drawing.PointF. System.Windows.Point is only used in WPF. So there isn't a "best practice", it all depends on what type of application you want to create.

If you need a point structure for needs unrelated to either WPF or GDI+, create your own structure.

Lobotomy answered 31/12, 2013 at 14:1 Comment(4)
While the GDI+ types force a dependency on the GDI+ library, System.Drawing.dll, the dependency implications of System.Windows.Point are slightly less strong: Microsoft put that type in WindowsBase.dll to enable you to use it without taking a dependency on WPF (which is in PresentationCore.dll and PresentationFramework.dll).Auricular
@IanGriffiths, strictly speaking, you're correct, but I'm not sure what you would do with just WindowsBase.dll... I don't think it's used by anything but WPF.Lobotomy
I updated the question a bit. What I need is to remember Rectangle coordinates from WPF project in my Class Library model project.Endicott
I've used just WindowsBase.dll in several projects for precisely this reason - I wanted Point and Rect (including some of the related utility functions). It was all for internal use, so the dependencies we took didn't really affect anyone else, but as it happens, we ended up using WindowsBase and not WPF. so it can and does happen.Auricular
G
0

Use Vector2 (System.Numerics) where you can cast the double values to float (where casting to int obviously makes you lose the decimals).

Glabrous answered 10/5, 2022 at 14:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.