Dumbo
Dumbo

Reputation: 14112

converting double values for making a Point type

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!

Upvotes: 8

Views: 19018

Answers (3)

Karl von L
Karl von L

Reputation: 1600

Although the System.Drawing.Point struct uses int values, and the System.Drawing.PointF struct uses float values, there is also the System.Windows.Point struct, which uses double values. So this is probably your best choice, if float does not provide enough precision for your needs.

However, since the name of the struct conflicts with that of the int-based one, it can be unwieldy to use in a file that already has a using System.Drawing directive in it:

using System.Drawing;
...
System.Windows.Point coords = new System.Windows.Point(1.5, 3.9);

Fortunately, you can get around this by using an alias:

using System.Drawing;
using PointD = System.Windows.Point;
...
PointD coords = new PointD(1.5, 3.9);

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160912

If it's just for storing these values there's always Tuple:

Tuple<double, double>[] Points = new Tuple<double, double>[50];
Points[0] = Tuple.Create<double, double>(5.33, 12.45);

Upvotes: 1

Cameron
Cameron

Reputation: 98766

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.

Upvotes: 15

Related Questions