Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GraphDrawer
- {
- private Graphics mGraphics;
- private PictureBox mPictureBox;
- public GraphDrawer (PictureBox pb)
- {
- mPictureBox = pb;
- mGraphics = pb.CreateGraphics();
- clear();
- }
- public void draw (double[] ys, bool clear)
- {
- double h = Convert.ToDouble(mPictureBox.Width) / (ys.Length - 1);
- if (clear)
- mGraphics.Clear(Color.White);
- double min = getMinFromArray(ys), max = getMaxFromArray(ys);
- double yAxisZero = max;
- for (int i = 0; i < ys.Length; ++i)
- {
- ys[i] = max - ys[i];
- }
- min = getMinFromArray(ys); max = getMaxFromArray(ys);
- double dHeight = mPictureBox.Height / (max - min) - 0.1;
- //double dHeight = (max - min) / mPictureBox.Height;\
- yAxisZero -= min;
- yAxisZero *= dHeight;
- for (int i = 0; i < ys.Length; ++i)
- {
- ys[i] -= min;
- ys[i] *= dHeight;
- }
- for (int i = 0; i < ys.Length - 1; ++i)
- {
- mGraphics.DrawLine(Pens.Black,
- (float) h * i,
- (float) (ys[i]),
- (float) h * (i + 1),
- (float) (ys[i + 1])
- );
- }
- Pen axisPen = new Pen(Color.Red);
- //axisPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
- mGraphics.DrawLine(axisPen, 0, (float) (yAxisZero), mPictureBox.Width /*- 5*/, (float) (yAxisZero));
- }
- private double getMinFromArray(double[] arr)
- {
- double min = Double.MaxValue;
- for (int i = 0; i < arr.Length; ++i)
- {
- if (min > arr[i])
- min = arr[i];
- }
- return min;
- }
- private double getMaxFromArray(double[] arr)
- {
- double max = Double.MinValue;
- for (int i = 0; i < arr.Length; ++i)
- {
- if (max < arr[i])
- max = arr[i];
- }
- return max;
- }
- internal void clear()
- {
- mGraphics.Clear(Color.White);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement