converting double values for making a Point type

Saeid Yazdani picture Saeid Yazdani · Mar 20, 2011 · Viewed 16.8k times · Source

I want to draw a curve (a diode curve) in a picturebox using imagefrom bitmap. I have a problem now and it is my point data are saved as Double and it is really important to keep the precesion.

for example a point in the plot I have is like this:

Voltage: -0.175 Current: -9.930625E-06

Yes, it is a Double! now how can I have a point to do for example:

        Point[] ptarray = new Point[3];
        ptarray[0] = new Point(250, 250);

Is there an alternative to Point[] that accepts double values? I have a 500x500 picture box. is there a way to convert those values to valid points that can still save precesion? I am wotking with micro Ampers (10^-6) and Voltages!

Answer

Cameron picture Cameron · Mar 20, 2011

Well, if float is enough precision, then you can use the PointF struct:

var point = new PointF(3.5f, 7.9f);

If you really need to, you can define your own PointD struct:

public struct PointD {
    public double X;
    public double Y;

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

    public Point ToPoint() {
        return new Point((int)X, (int)Y);
    }

    public override bool Equals(object obj) {
      return obj is PointD && this == (PointD)obj;
    }
    public override int GetHashCode() {
      return X.GetHashCode() ^ Y.GetHashCode();
    }
    public static bool operator ==(PointD a, PointD b) {
      return a.X == b.X && a.Y == b.Y;
    }
    public static bool operator !=(PointD a, PointD b) {
      return !(a == b);
    }
}

Equality code originally from here.

The ToPoint() method lets you convert it to a Point object, though of course the precision will be truncated.