Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting double values for making a Point type

Tags:

c#

double

point

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!

like image 291
Saeid Yazdani Avatar asked Mar 20 '11 03:03

Saeid Yazdani


People also ask

What is convert ToDouble?

The Convert. ToDouble() method in C# converts the specified string representation of a number to an equivalent double-precision floating-point number, using the specified culture-specific formatting information.

Can you cast a double to a float?

floatValue() to Convert Double to Float in Java. Another way to convert a double value into a float data type in Java is by using the wrapper class Double. This class wraps a primitive data type double in an object.

How do I convert a string to a double in C#?

String value can be converted to double using Convert. ToDouble() or Double. Parse() method. These methods take string representation of a number as input and return its equivalent double-precision floating-point number.


2 Answers

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.

like image 154
Cameron Avatar answered Oct 19 '22 09:10

Cameron


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);
like image 25
BrokenGlass Avatar answered Oct 19 '22 08:10

BrokenGlass