Advertisement
inf926k

Untitled

Oct 17th, 2016
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1.         public class GraphDrawer
  2.         {
  3.             private Graphics mGraphics;
  4.             private PictureBox mPictureBox;
  5.  
  6.             public GraphDrawer (PictureBox pb)
  7.             {
  8.                 mPictureBox = pb;
  9.                 mGraphics = pb.CreateGraphics();
  10.                 clear();
  11.             }
  12.  
  13.             public void draw (double[] ys, bool clear)
  14.             {
  15.                 double h = Convert.ToDouble(mPictureBox.Width) / (ys.Length - 1);
  16.                 if (clear)
  17.                     mGraphics.Clear(Color.White);
  18.  
  19.                 double min = getMinFromArray(ys), max = getMaxFromArray(ys);
  20.  
  21.                 double yAxisZero = max;
  22.                 for (int i = 0; i < ys.Length; ++i)
  23.                 {
  24.  
  25.                     ys[i] = max - ys[i];
  26.                 }
  27.                 min = getMinFromArray(ys); max = getMaxFromArray(ys);
  28.  
  29.                 double dHeight = mPictureBox.Height / (max - min) - 0.1;
  30.                 //double dHeight = (max - min) / mPictureBox.Height;\
  31.                 yAxisZero -= min;
  32.                 yAxisZero *= dHeight;
  33.                 for (int i = 0; i < ys.Length; ++i)
  34.                 {
  35.                     ys[i] -= min;
  36.                     ys[i] *= dHeight;
  37.                 }
  38.  
  39.                 for (int i = 0; i < ys.Length - 1; ++i)
  40.                 {
  41.                     mGraphics.DrawLine(Pens.Black,
  42.                         (float) h * i,
  43.                         (float) (ys[i]),
  44.                         (float) h * (i + 1),
  45.                         (float) (ys[i + 1])
  46.                         );
  47.                 }
  48.                 Pen axisPen = new Pen(Color.Red);
  49.                 //axisPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  50.                 mGraphics.DrawLine(axisPen, 0, (float) (yAxisZero), mPictureBox.Width /*- 5*/, (float) (yAxisZero));
  51.             }
  52.  
  53.             private double getMinFromArray(double[] arr)
  54.             {
  55.                 double min = Double.MaxValue;
  56.                 for (int i = 0; i < arr.Length; ++i)
  57.                 {
  58.                     if (min > arr[i])
  59.                         min = arr[i];
  60.                 }
  61.                 return min;
  62.             }
  63.  
  64.             private double getMaxFromArray(double[] arr)
  65.             {
  66.                 double max = Double.MinValue;
  67.                 for (int i = 0; i < arr.Length; ++i)
  68.                 {
  69.                     if (max < arr[i])
  70.                         max = arr[i];
  71.                 }
  72.                 return max;
  73.             }
  74.  
  75.             internal void clear()
  76.             {
  77.                 mGraphics.Clear(Color.White);
  78.             }
  79.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement