0

Can DrawLine handle coordinates outside the defined area?

For example myGraphics.DrawLine(MyPen, -20, -80, 20, 90);

I would expect this to produce a line correctly as though it had used an infinite canvas but plotting only the section within my graphic.

My code is as follows. I am plotting movement from coordinates recorded in a database. Occasionally the subject moves further than expected, beyond the edges of my bitmap. I do not check for this occurrence as I was relying on DrawLine to handle it.

Bitmap Border = new Bitmap(5000, 5000);
Border.SetResolution(254, 254);

Graphics MyGraphics= Graphics.FromImage(Border);
Pen MyPen = new Pen(Color.Black, 1);


for (Int32 Point = 1; Point <= Points; Point++)
{
   XCoord2 = XCoord1;
   YCoord2 = YCoord1;
   XCoord1 = *READ FROM DATABASE*
   YCoord1 = *READ FROM DATABASE*

   if (Point > 1)
   {
        MyGraphics.DrawLine(MyPen, XCoord1, YCoord1, XCoord2, YCoord2);
   }
}

In reality, my plots work most of the time. However I do get an occasional graphic with missing lines or with an obscure line originating from a strange coordinate.

In summary, should the behaviour of DrawLine predictable with unusual parameters. Should I introduce some trigonometry to force the plots to always be within my grid?

MortimerCat
  • 829
  • 9
  • 26
  • 1
    Unless you are using very large coordinates (which could cause overflow or rounding issues) you should be OK I think. GDI has all sorts of viewport and window support which means handling this stuff should be a doddle for the implementers. I've never had any problems with the clipping. – Matthew Watson Mar 28 '13 at 21:43
  • @AshBurlaczenko As the question says "my plots work most of the time". I have spent hours trying to track down why I have an occasional failure. When I scan through my 1000-2000 coordinates, nothing seems unusual, which is why I am now suspecting DrawLine. I came here in the hope that someone might confirm that. – MortimerCat Mar 28 '13 at 21:59
  • All GDI+ drawing is done internally with *float*. Rounding errors are common. – Hans Passant Mar 28 '13 at 23:56

2 Answers2

0

The actual limits are a billion positive or negative see this past question (that used .net )

What are the hard bounds for drawing coordinates in GDI+?

My guess is that your database pulls are wrong, this can happen if you are using a string data storage and forcing that to be parsed.

Add a thread.sleep() and have it Debug.WriteLine the new pulls (or just breakpoint things), likely a value is getting in there that is either odd or getting parsed oddly

Community
  • 1
  • 1
yasth
  • 171
  • 5
0

After more experimentation, I finally cured my problem with...

SolidBrush WhiteBrush = new SolidBrush(Color.White);
myGraphics.FillRectangle(WhiteBrush,0,0,5000,5000);

i.e. I gave my graphics a solid white background before I drew any lines. Before I was drawing a black line on a NULL background. I have no idea why this would affect anything, but it did!

MortimerCat
  • 829
  • 9
  • 26