C# double precision coordinates for drawing in Windows Form

3k Views Asked by At

first of all I'm new with C#, I always used Java. First,

I wanted to ask "Can you have double coords?", I mean "Can you go deeper than a pixel when you draw to screen?",

and also, "Is windows form the best way to make a simple graphic application (like a simple doodle jump game)?"

but that's simple curiosity. The problem is, that I need to draw some simple objects (points, lines, especially rectangles) with double/float/decimal precision (it doesn't matter, with more precision than an integer) on a windows form (I'm using Visual Studio), but I can't find anything.

I've seen like Pointf or RectangleF etc. but I'm not sure if they are used for this purpose. I've also seen that Rect (System.Windows.Shapes) uses double precision but are not usable with windows form (I'm not sure, but if possible, I would like to find the easiest way to do this thing on windows form).

Hoping that I've been clear enough, thanks in advance.

1

There are 1 best solutions below

0
On

As noted in my comment, maybe that is even what you need.

Graphics.DrawLine() and other drawing methods take overloads with single precision coordinates, either in form of individual floats or PointF, RectangleF structures.

Results may vary depending on the SmoothingMode however.

E.g.:

using (var bmp = new Bitmap(100, 100))
{
    using (var g = Graphics.FromImage(bmp))
    {
        //Note that this enabled anti-aliasing
        g.SmoothingMode = SmoothingMode.HighQuality;

        g.Clear(Color.White);
        float x = 20.0f;
        float y1 = 1.0f;
        float y2 = 10.0f;
        g.DrawLine(Pens.Red, x, y1, x, y2);
        x = 20.5f;
        y1 = 11.0f;
        y2 = 20.0f;
        g.DrawLine(Pens.Red, x, y1, x, y2);
        x = 21.0f;
        y1 = 21.0f;
        y2 = 30.0f;
        g.DrawLine(Pens.Red, x, y1, x, y2);
    }
}

Here, the line is actually interpolated in intensity between the two coordinates. If SmoothingMode would be left at default, the line would be exactly 1 pixel wide and rounded to the nearest pixel. You should test and choose the method that suits your graphic the most.

Here is the zoomed-in result with interpolation using SmoothingMode.HighQuality:

enter image description here