简体   繁体   中英

How to handle NaN value in graphics.DrawLine method in C#

I want to handle NaN value in Graphics.DrawLine method. Please refer below code snippet.

Private Graphics g;

m_g.DrawLine(pen, x1, y1, x2, y2);

in some cases in our project y1 and y2 value is NaN . so it throws the exception like "overflow error".

how to handle this for NaN value.

The only solutions is to check if any of your values are NaN, and don't call DrawLine if they are (if x1, x2, etc. are float s):

if (Single.IsNaN(y1) || Single.IsNaN...)
{
    //Handle error
}
else
{        
    m_g.DrawLine(pen, x1, y1, x2, y2);
}

Assuming your data represents some kind of graph, a NaN could mean one of two things:

  1. It could mean there's an error in the data.
  2. It could repesent a valid gap in the data.

In the first case, a NaN means the data is bad and you have two choices: Don't draw any of it, or draw all of it up to the first NaN. Either way, you should alert the user to the problem.

In the second case, if there are any NaNs you should "chop" up your data into individual sections that do not contain any NaNs, and just draw those sections. Then your graph will have (valid) gaps in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM