Can we overload the Point object in C # so that it supports doubling?

I am drawing some graphs using a Point object and I want to set it so it supports double parameters. I am working on Visual C #, WindowsConsoleApplication

Thanks.

+2


a source to share


3 answers


You can just use PointF

. All System.Drawing

support floating point operations. Why not use them? Otherwise, if you really want to use double

and pass them to entire drawing functions, you will need to create your own structure. Something like this:

struct MyPoint {
 public double X{get;set;}
 public double Y{get;set;}

 public MyPoint(double x, double y) {
   X = x;
   Y = y;
 }

 public implicit operator Point() {
  return new Point(X, Y);
 }
}

      



This is a very stripped down implementation, if you look at the original metadata Point

(going down to the definition) you can see all the overrides and operators you need to implement.

Also, as a point of interest, you may notice that it Point

can implicitly convert to PointF

, but not vice versa. This is not supported due to the potential loss of precision. If I were you, I would revisit the design as it seems to go against the best practices of API designers.

+6


a source


The built-in structure Point uses int and PointF uses float, but you can make your own that uses double.



+1


a source


No, it is not possible to overload a point object.

A dot is basically a struct, so it doesn't support overloading.

Better you create your own class that will behave just like the point.

Hope this helps!

0


a source







All Articles