The easiest way to extend Struct (PointF)

We need to save one additional information using PointF (location parameter t along the Bezier curve).

Since this data cannot be easily recalculated, I want to save it with PointF at the time of calculating the point for use in other routines.

We have hundreds of references to PointF, so I was hoping not to create a new substitution class, but to "expand" the PointF structure with one additional property.

The client code will look something like this:

PointF intersection = new PointF();
intersection.X = 3457.23;
intersection.Y = -277.738;
intersection.t = 0.744;

Is this possible (or something like that)?

+3
source share
3 answers

, . .

+2

PointF - ( ), .

+3

This is pretty dirty, but if the value of T is always the same for a given PointF, you can use extension methods to mimic the get / set behavior offered by the properties. Assuming you are using a C # 3.0 compiler, you can do something like this.

public static class PointFExtensions
{
      private static Dictionary<PointF, float> _dict = new Dictionary<PointF, float>();

      public static void SetT(this PointF self, float t)
      {
         _dict.Add(self, t);
      }

      public static float GetT(this PointF self)
      {
        return _dict[self];
      }
}

Then you can use these methods as follows:

PointF pf = new PointF(4.0F, 5.0F);
pf.SetT(42);
float t = pf.GetT();
+2
source

Source: https://habr.com/ru/post/1788092/


All Articles