8

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!

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
Dumbo
  • 13,555
  • 54
  • 184
  • 288

3 Answers3

15

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.

Community
  • 1
  • 1
Cameron
  • 96,106
  • 25
  • 196
  • 225
1

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);
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 1
    tuples don't add any context and is way to general and makes the code hard to read. I'd say use them as last resort. – vidstige Aug 16 '11 at 07:56
0

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);
Karl von L
  • 1,591
  • 3
  • 15
  • 22