Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace FunctionPloter
- {
- public partial class FunctionPloterForm : Form
- {
- public float OffsetX { get; set; }
- public float OffsetY { get; set; }
- public float BorderSpace { get; set; }
- public Pen PlotXPen { get; set; }
- public Pen PlotYPen { get; set; }
- public Pen PlotPen { get; set; }
- public Pen PlotPointPen { get; set; }
- public Pen PlotZeroPointPen { get; set; }
- public Pen PlotXPointsPen { get; set; }
- public Pen PlotMaxPointPen { get; set; }
- public Graphics PlotGraphics { get; set; }
- public Brush StringAxisBrush { get; set; }
- public float ResulationX { get; set; }
- public float ResulationY { get; set; }
- public PointF[] Points { get; set; }
- public FunctionPloterForm()
- {
- initEnviroment();
- InitializeComponent();
- initOffsets();
- initPens();
- initResulations();
- }
- private void initEnviroment()
- {
- this.BackColor = Color.White;
- }
- private void initOffsets()
- {
- OffsetX = this.ClientRectangle.Width / 2;
- OffsetY = this.ClientRectangle.Height / 2;
- }
- private void initGraphics()
- {
- PlotGraphics = this.CreateGraphics();
- }
- private void initPens()
- {
- PlotXPen = new Pen(Color.Coral, 0.2F);
- PlotYPen = new Pen(Color.Coral, 0.2F);
- PlotPen = new Pen(Color.Blue, 0.1F);
- PlotPointPen = new Pen(Color.Black, 1.3F);
- PlotZeroPointPen = new Pen(Color.Black, 2F);
- PlotXPointsPen = new Pen(Color.Khaki, 3F);
- PlotMaxPointPen = new Pen(Color.Red, 3.5F);
- }
- private void initResulations()
- {
- ResulationX = 10F;
- ResulationY = 10F;
- }
- private void DrawXY( Graphics g )
- {
- BorderSpace = 20;
- StringAxisBrush = new SolidBrush( Color.Black);
- g.DrawLine(PlotXPen, new PointF(0 + BorderSpace-15, OffsetY), new PointF(this.ClientRectangle.Width - BorderSpace, OffsetY));
- g.DrawLine(PlotYPen, new PointF(OffsetX, BorderSpace), new PointF(OffsetX, this.ClientRectangle.Height - BorderSpace+15 ));
- g.DrawString("Y", this.Font, StringAxisBrush, new PointF(OffsetX-5, 4.5F));
- g.DrawString("X", this.Font, StringAxisBrush, new PointF(this.ClientRectangle.Width-15, OffsetY-6.5F));
- PlotGraphics.DrawEllipse(PlotZeroPointPen, OffsetX - (PlotZeroPointPen.Width / 2), OffsetY - (PlotZeroPointPen.Width / 2), PlotZeroPointPen.Width, PlotZeroPointPen.Width);
- }
- private void FunctionPloterForm_Paint(object sender, PaintEventArgs e)
- {
- //Graphics g = e.Graphics;
- PlotGraphics = e.Graphics;
- Graphics g = PlotGraphics;
- DrawXY(g);
- //Plot( new PointF[]{ new PointF(0, 5), new PointF(5, 30), new PointF(10, 55), new PointF(20, 75), new PointF(30, 85), new PointF(40, 145) } );
- //Plot((x) => (float)Math.Pow(x, 2), -100, 100, 0.099F);
- Plot((x) => (float)Math.Sin(x), -10, 10, 0.08F);
- //Plot((x) => (float)x, -1, 100, 1F);
- //Plot((x) => (float)Math.Cos(x), -30, 30, 0.2F);
- //Plot((x) => (float)Math.Cos(x), -30, 30, 0.2F);
- //Plot( (x) => (float)(x/((x-1)*(x-2))), (float)3,(float)200, 1F);
- //Plot((x) => (float)x*2, -50, 50, 0.5F);
- //Plot((x) => (float)(10*x+5), -10, 40, 0.5F);
- //Plot((x) => 2*x, -100, 100, 1F);
- //Plot((x) => ( (5 * (x * x * x * x)) - (8 * (x * x)) + 8), -5.0F, 5.0F, 0.1F);
- }
- public void PlotLine(PointF point1, PointF point2)
- {
- PlotGraphics.DrawLine(PlotPen, new PointF((point1.X*ResulationX) + OffsetX, OffsetY - (point1.Y * ResulationY) ), new PointF((point2.X*ResulationX) + OffsetX, OffsetY - (point2.Y * ResulationY)));
- }
- public void PlotPoint(PointF point)
- {
- //PlotGraphics.DrawLine(PlotPointPen, point, point);
- //PlotGraphics.DrawPolygon(PlotPointPen, new PointF[]{point} );
- //PlotGraphics.DrawEllipse(PlotPointPen, ((point.X * ResulationX) + OffsetX) - (PlotPointPen.Width / 2), (OffsetY - (point.Y * ResulationY)) - (PlotPointPen.Width / 2), PlotPointPen.Width / 2, PlotPointPen.Width / 2);
- Plot(point, PlotPointPen);
- }
- public void Plot(PointF point, Pen pen)
- {
- PlotGraphics.DrawEllipse(pen, ((point.X * ResulationX) + OffsetX) - (pen.Width / 2), (OffsetY - (point.Y * ResulationY)) - (pen.Width / 2), pen.Width / 2, pen.Width / 2);
- }
- public void PlotMaxPoint(PointF point)
- {
- Plot(point, PlotMaxPointPen);
- }
- public void Plot(PointF[] points)
- {
- //List<PointF> maxes = new List<PointF>();
- PointF current;
- PlotXSectionLimit(points[0].X);
- //PlotYSectionLimit(points[0].Y);
- PlotXSectionLimit(points[points.Length - 1].X);
- //PlotYSectionLimit(points[points.Length - 1].Y);
- for (int index = 0; index < points.Length; ++index)
- {
- current = points[index];
- /*if (points[index].X == 0)
- Plot(points[index], PlotXPointsPen);
- else
- if (maxes.Count == 0)
- maxes.Add( current );
- else if (maxes[0].Y == current.Y)
- maxes.Add(current);
- else if (maxes[0].Y < current.Y)
- {
- maxes.Clear();
- maxes.Add(current);
- }*/
- PlotPoint(current);
- if (index + 1 < points.Length)
- {
- PlotLine(current, points[index + 1]);
- }
- }
- //foreach (PointF point in maxes)
- //PlotMaxPoint(point);
- }
- public void Plot(Func<float, float> func)
- {
- Plot(func, -50, 50, 0.1F);
- }
- public void Plot(Func<float, float> func, float begin = 0, float end = 100F, float interval = 0.1F)
- {
- Plot(func, (uint)( Math.Abs(Math.Abs((end-begin))/interval) ), begin, end, interval);
- }
- public void PlotXSectionLimit(float value)
- {
- PlotGraphics.DrawLine(PlotPointPen, new PointF((value * ResulationX) + OffsetX, OffsetY - 5), new PointF((value * ResulationX) + OffsetX, OffsetY + 5));
- PlotGraphics.DrawString(value.ToString(), this.Font, this.StringAxisBrush, new PointF((value * ResulationX) + OffsetX - value.ToString().Length*3, OffsetY - 20));
- }
- public void PlotYSectionLimit(float value)
- {
- PlotGraphics.DrawLine(PlotPointPen, new PointF(OffsetX - 5, OffsetY - (value * ResulationY)), new PointF(OffsetX + 5, OffsetY - (value * ResulationY)));
- PlotGraphics.DrawString(value.ToString(), this.Font, this.StringAxisBrush, new PointF(OffsetX + 5 + 5 ,OffsetY - (value * ResulationY) - 7));
- }
- // public void Plot(Func<float, float> func, uint pointsCount = 100, float begin = 0, float end = 100F )
- // {
- // Plot(func, pointsCount, begin, end, Math.Abs(end - begin) / 2);
- // }
- public void Plot(Func<float, float> func, uint pointsCount = 100, float begin = 0, float end = 100F, float intervarl = 0.1F)
- {
- Points = new PointF[pointsCount+1];
- float current = begin-intervarl;
- for (int index = 0; index < Points.Length; ++index)
- {
- current += intervarl;
- Points[index] = new PointF(current, func(current));
- }
- Plot(Points);
- }
- private void FunctionPloterForm_KeyPress(object sender, KeyPressEventArgs e)
- {
- // if (e.KeyChar == 'l')
- // {
- // //PlotPointPen.Width *= 2;
- // PlotPen.Width *= 2;
- //
- // this.Refresh();
- // }
- // else if (e.KeyChar == 'c')
- // {
- // PlotPointPen.Width *= 2;
- //
- // this.Refresh();
- // }
- // else if (e.KeyChar == 'r')
- // {
- // ResulationX *= 2.5F;
- // ResulationY *= 2.5F;
- //
- // //OffsetX *= 1.1F;
- // //OffsetY *= 1.1F;
- //
- // this.Refresh();
- // }
- }
- private void FunctionPloterForm_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Add)
- {
- ResulationX *= 1.1F;
- ResulationY *= 1.1F;
- }
- else if (e.KeyCode == Keys.Subtract)
- {
- ResulationX /= 1.1F;
- ResulationY /= 1.1F;
- }
- else if (e.KeyCode == Keys.Up)
- {
- // OffsetX += 3F;
- OffsetY -= 3F;
- }
- else if (e.KeyCode == Keys.Down)
- {
- // OffsetX += 3F;
- OffsetY += 3F;
- }
- else if (e.KeyCode == Keys.Left)
- {
- OffsetX -= 3F;
- //OffsetY *= 1.1F;
- }
- else if (e.KeyCode == Keys.Right)
- {
- OffsetX += 3F;
- //OffsetY *= 1.1F;
- }
- else if (e.KeyCode == Keys.PageUp)
- {
- ResulationY *= 1.1F;
- }
- else if (e.KeyCode == Keys.PageDown)
- {
- ResulationY /= 1.1F;
- }
- else if (e.KeyCode == Keys.Home)
- {
- //PlotPointPen.Width *= 1.1F;
- ResulationX *= 1.1F;
- }
- else if (e.KeyCode == Keys.Delete)
- {
- //PlotPointPen.Width /= 1.1F;
- ResulationX /= 1.1F;
- }
- else if (e.KeyCode == Keys.Q)
- {
- PlotPointPen.Width *= 1.1F;
- }
- else if (e.KeyCode == Keys.W)
- {
- PlotPointPen.Width /= 1.1F;
- }
- this.Refresh();
- }
- private void FunctionPloterForm_MouseMove(object sender, MouseEventArgs e)
- {
- bool update = false;
- if (e.X == OffsetX)
- {
- //PlotXPen = new Pen(Color.Coral, 0.2F);
- PlotYPen = new Pen(Color.Red, 3.5F);
- update = true;
- }
- if (e.Y == OffsetY)
- {
- PlotXPen = new Pen(Color.Red, 3.5F);
- //PlotYPen = new Pen(Color.Coral, 0.2F);
- update = true;
- }
- if( false == update )
- {
- if (PlotXPen.Color != Color.Coral)
- {
- PlotXPen = new Pen(Color.Coral, 0.2F);
- update = true;
- }
- if (PlotYPen.Color != Color.Coral)
- {
- PlotYPen = new Pen(Color.Coral, 0.2F);
- update = true;
- }
- }
- if (update)
- this.Refresh();
- }
- private void FunctionPloterForm_Resize(object sender, EventArgs e)
- {
- initOffsets();
- this.Refresh();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement