Advertisement
m1o2

Function Plotter, Beta, m1o2

Dec 15th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace FunctionPloter
  11. {
  12.     public partial class FunctionPloterForm : Form
  13.     {
  14.         public float OffsetX { get; set; }
  15.         public float OffsetY { get; set; }
  16.         public float BorderSpace { get; set; }
  17.         public Pen PlotXPen { get; set; }
  18.         public Pen PlotYPen { get; set; }
  19.         public Pen PlotPen { get; set; }
  20.         public Pen PlotPointPen { get; set; }
  21.         public Pen PlotZeroPointPen { get; set; }
  22.         public Pen PlotXPointsPen { get; set; }
  23.         public Pen PlotMaxPointPen { get; set; }
  24.         public Graphics PlotGraphics { get; set; }
  25.         public Brush StringAxisBrush { get; set; }
  26.         public float ResulationX { get; set; }
  27.         public float ResulationY { get; set; }
  28.         public PointF[] Points { get; set; }
  29.  
  30.         public FunctionPloterForm()
  31.         {
  32.             initEnviroment();
  33.             InitializeComponent();
  34.             initOffsets();
  35.             initPens();
  36.             initResulations();
  37.         }
  38.  
  39.         private void initEnviroment()
  40.         {
  41.             this.BackColor = Color.White;
  42.         }
  43.  
  44.         private void initOffsets()
  45.         {
  46.             OffsetX = this.ClientRectangle.Width / 2;
  47.             OffsetY = this.ClientRectangle.Height / 2;
  48.         }
  49.  
  50.         private void initGraphics()
  51.         {
  52.             PlotGraphics = this.CreateGraphics();
  53.         }
  54.  
  55.         private void initPens()
  56.         {
  57.             PlotXPen = new Pen(Color.Coral, 0.2F);
  58.             PlotYPen = new Pen(Color.Coral, 0.2F);
  59.             PlotPen = new Pen(Color.Blue, 0.1F);
  60.             PlotPointPen = new Pen(Color.Black, 1.3F);
  61.             PlotZeroPointPen = new Pen(Color.Black, 2F);
  62.             PlotXPointsPen = new Pen(Color.Khaki, 3F);
  63.             PlotMaxPointPen = new Pen(Color.Red, 3.5F);
  64.         }
  65.  
  66.         private void initResulations()
  67.         {
  68.             ResulationX = 10F;
  69.             ResulationY = 10F;
  70.         }
  71.  
  72.         private void DrawXY( Graphics g )
  73.         {
  74.             BorderSpace = 20;
  75.             StringAxisBrush = new SolidBrush( Color.Black);
  76.  
  77.             g.DrawLine(PlotXPen, new PointF(0 + BorderSpace-15, OffsetY), new PointF(this.ClientRectangle.Width - BorderSpace, OffsetY));
  78.             g.DrawLine(PlotYPen, new PointF(OffsetX, BorderSpace), new PointF(OffsetX, this.ClientRectangle.Height - BorderSpace+15 ));
  79.             g.DrawString("Y", this.Font, StringAxisBrush, new PointF(OffsetX-5, 4.5F));
  80.             g.DrawString("X", this.Font, StringAxisBrush, new PointF(this.ClientRectangle.Width-15, OffsetY-6.5F));
  81.  
  82.             PlotGraphics.DrawEllipse(PlotZeroPointPen, OffsetX - (PlotZeroPointPen.Width / 2), OffsetY - (PlotZeroPointPen.Width / 2), PlotZeroPointPen.Width, PlotZeroPointPen.Width);
  83.         }
  84.  
  85.         private void FunctionPloterForm_Paint(object sender, PaintEventArgs e)
  86.         {
  87.             //Graphics g = e.Graphics;
  88.             PlotGraphics = e.Graphics;
  89.             Graphics g = PlotGraphics;
  90.            
  91.  
  92.             DrawXY(g);
  93.             //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) } );
  94.             //Plot((x) => (float)Math.Pow(x, 2), -100, 100, 0.099F);
  95.             Plot((x) => (float)Math.Sin(x), -10, 10, 0.08F);
  96.             //Plot((x) => (float)x, -1, 100, 1F);
  97.             //Plot((x) => (float)Math.Cos(x), -30, 30, 0.2F);
  98.             //Plot((x) => (float)Math.Cos(x), -30, 30, 0.2F);
  99.             //Plot( (x) => (float)(x/((x-1)*(x-2))), (float)3,(float)200, 1F);
  100.             //Plot((x) => (float)x*2, -50, 50, 0.5F);
  101.             //Plot((x) => (float)(10*x+5), -10, 40, 0.5F);
  102.             //Plot((x) => 2*x, -100, 100, 1F);
  103.  
  104.             //Plot((x) => ( (5 * (x * x * x * x)) - (8 * (x * x)) + 8), -5.0F, 5.0F, 0.1F);
  105.         }
  106.  
  107.         public void PlotLine(PointF point1, PointF point2)
  108.         {
  109.             PlotGraphics.DrawLine(PlotPen, new PointF((point1.X*ResulationX) + OffsetX, OffsetY - (point1.Y * ResulationY) ), new PointF((point2.X*ResulationX) + OffsetX, OffsetY - (point2.Y * ResulationY)));
  110.         }
  111.  
  112.         public void PlotPoint(PointF point)
  113.         {
  114.             //PlotGraphics.DrawLine(PlotPointPen, point, point);
  115.             //PlotGraphics.DrawPolygon(PlotPointPen, new PointF[]{point} );
  116.             //PlotGraphics.DrawEllipse(PlotPointPen, ((point.X * ResulationX) + OffsetX) - (PlotPointPen.Width / 2), (OffsetY - (point.Y * ResulationY)) - (PlotPointPen.Width / 2), PlotPointPen.Width / 2, PlotPointPen.Width / 2);
  117.             Plot(point, PlotPointPen);
  118.         }
  119.  
  120.         public void Plot(PointF point, Pen pen)
  121.         {
  122.             PlotGraphics.DrawEllipse(pen, ((point.X * ResulationX) + OffsetX) - (pen.Width / 2), (OffsetY - (point.Y * ResulationY)) - (pen.Width / 2), pen.Width / 2, pen.Width / 2);
  123.         }
  124.  
  125.         public void PlotMaxPoint(PointF point)
  126.         {
  127.             Plot(point, PlotMaxPointPen);
  128.         }
  129.  
  130.         public void Plot(PointF[] points)
  131.         {
  132.             //List<PointF> maxes = new List<PointF>();
  133.             PointF current;
  134.  
  135.             PlotXSectionLimit(points[0].X);
  136.             //PlotYSectionLimit(points[0].Y);
  137.  
  138.             PlotXSectionLimit(points[points.Length - 1].X);
  139.             //PlotYSectionLimit(points[points.Length - 1].Y);
  140.  
  141.             for (int index = 0; index < points.Length; ++index)
  142.             {
  143.                 current = points[index];
  144.                /*if (points[index].X == 0)
  145.                     Plot(points[index], PlotXPointsPen);
  146.                 else
  147.                 if (maxes.Count == 0)
  148.                     maxes.Add( current );
  149.                 else if (maxes[0].Y == current.Y)
  150.                     maxes.Add(current);
  151.                 else if (maxes[0].Y < current.Y)
  152.                 {
  153.                     maxes.Clear();
  154.                     maxes.Add(current);
  155.                 }*/
  156.                     PlotPoint(current);
  157.  
  158.                 if (index + 1 < points.Length)
  159.                 {
  160.                     PlotLine(current, points[index + 1]);
  161.                 }
  162.             }
  163.  
  164.             //foreach (PointF point in maxes)
  165.                 //PlotMaxPoint(point);
  166.         }
  167.  
  168.  
  169.         public void Plot(Func<float, float> func)
  170.         {
  171.             Plot(func, -50, 50, 0.1F);
  172.         }
  173.  
  174.  
  175.         public void Plot(Func<float, float> func, float begin = 0, float end = 100F, float interval = 0.1F)
  176.         {
  177.             Plot(func, (uint)( Math.Abs(Math.Abs((end-begin))/interval) ), begin, end, interval);
  178.         }
  179.  
  180.         public void PlotXSectionLimit(float value)
  181.         {
  182.             PlotGraphics.DrawLine(PlotPointPen, new PointF((value * ResulationX) + OffsetX, OffsetY - 5), new PointF((value * ResulationX) + OffsetX, OffsetY + 5));
  183.             PlotGraphics.DrawString(value.ToString(), this.Font, this.StringAxisBrush, new PointF((value * ResulationX) + OffsetX - value.ToString().Length*3, OffsetY - 20));
  184.         }
  185.  
  186.         public void PlotYSectionLimit(float value)
  187.         {
  188.             PlotGraphics.DrawLine(PlotPointPen, new PointF(OffsetX - 5,  OffsetY - (value * ResulationY)), new PointF(OffsetX + 5, OffsetY - (value * ResulationY)));
  189.             PlotGraphics.DrawString(value.ToString(), this.Font, this.StringAxisBrush, new PointF(OffsetX + 5 + 5 ,OffsetY - (value * ResulationY) - 7));
  190.         }
  191.  
  192. //         public void Plot(Func<float, float> func, uint pointsCount = 100, float begin = 0, float end = 100F )
  193. //         {
  194. //             Plot(func, pointsCount, begin, end, Math.Abs(end - begin) / 2);
  195. //         }
  196.  
  197.         public void Plot(Func<float, float> func, uint pointsCount = 100, float begin = 0, float end = 100F, float intervarl = 0.1F)
  198.         {
  199.  
  200.             Points = new PointF[pointsCount+1];
  201.  
  202.             float current = begin-intervarl;
  203.             for (int index = 0; index < Points.Length; ++index)
  204.             {
  205.                 current += intervarl;
  206.                 Points[index] = new PointF(current, func(current));
  207.             }
  208.  
  209.             Plot(Points);
  210.         }
  211.  
  212.         private void FunctionPloterForm_KeyPress(object sender, KeyPressEventArgs e)
  213.         {
  214. //             if (e.KeyChar == 'l')
  215. //             {
  216. //                 //PlotPointPen.Width *= 2;
  217. //                 PlotPen.Width *= 2;
  218. //
  219. //                 this.Refresh();
  220. //             }
  221. //             else if (e.KeyChar == 'c')
  222. //             {
  223. //                 PlotPointPen.Width *= 2;
  224. //
  225. //                 this.Refresh();
  226. //             }
  227. //             else if (e.KeyChar == 'r')
  228. //             {
  229. //                 ResulationX *= 2.5F;
  230. //                 ResulationY *= 2.5F;
  231. //
  232. //                 //OffsetX *= 1.1F;
  233. //                 //OffsetY *= 1.1F;
  234. //
  235. //                 this.Refresh();
  236. //             }
  237.         }
  238.  
  239.         private void FunctionPloterForm_KeyDown(object sender, KeyEventArgs e)
  240.         {
  241.             if (e.KeyCode == Keys.Add)
  242.             {
  243.                 ResulationX *= 1.1F;
  244.                 ResulationY *= 1.1F;
  245.             }
  246.             else if (e.KeyCode == Keys.Subtract)
  247.             {
  248.                 ResulationX /= 1.1F;
  249.                 ResulationY /= 1.1F;
  250.             }
  251.             else if (e.KeyCode == Keys.Up)
  252.             {
  253.                 // OffsetX += 3F;
  254.                 OffsetY -= 3F;
  255.             }
  256.             else if (e.KeyCode == Keys.Down)
  257.             {
  258.                 // OffsetX += 3F;
  259.                 OffsetY += 3F;
  260.             }
  261.             else if (e.KeyCode == Keys.Left)
  262.             {
  263.                 OffsetX -= 3F;
  264.                 //OffsetY *= 1.1F;
  265.             }
  266.             else if (e.KeyCode == Keys.Right)
  267.             {
  268.                 OffsetX += 3F;
  269.                 //OffsetY *= 1.1F;
  270.             }
  271.             else if (e.KeyCode == Keys.PageUp)
  272.             {
  273.                 ResulationY *= 1.1F;
  274.             }
  275.             else if (e.KeyCode == Keys.PageDown)
  276.             {
  277.                 ResulationY /= 1.1F;
  278.             }
  279.             else if (e.KeyCode == Keys.Home)
  280.             {
  281.                 //PlotPointPen.Width *= 1.1F;
  282.                 ResulationX *= 1.1F;
  283.             }
  284.             else if (e.KeyCode == Keys.Delete)
  285.             {
  286.                 //PlotPointPen.Width /= 1.1F;
  287.                 ResulationX /= 1.1F;
  288.             }
  289.             else if (e.KeyCode == Keys.Q)
  290.             {
  291.                 PlotPointPen.Width *= 1.1F;
  292.             }
  293.             else if (e.KeyCode == Keys.W)
  294.             {
  295.                 PlotPointPen.Width /= 1.1F;
  296.             }
  297.  
  298.             this.Refresh();
  299.         }
  300.  
  301.         private void FunctionPloterForm_MouseMove(object sender, MouseEventArgs e)
  302.         {
  303.  
  304.             bool update = false;
  305.  
  306.             if (e.X == OffsetX)
  307.             {
  308.                 //PlotXPen = new Pen(Color.Coral, 0.2F);
  309.                 PlotYPen = new Pen(Color.Red, 3.5F);
  310.  
  311.                 update = true;
  312.             }
  313.  
  314.             if (e.Y == OffsetY)
  315.             {
  316.                 PlotXPen = new Pen(Color.Red, 3.5F);
  317.                 //PlotYPen = new Pen(Color.Coral, 0.2F);
  318.  
  319.                 update = true;
  320.             }
  321.  
  322.             if( false == update )
  323.             {
  324.  
  325.                 if (PlotXPen.Color != Color.Coral)
  326.                 {
  327.                     PlotXPen = new Pen(Color.Coral, 0.2F);
  328.                     update = true;
  329.                 }
  330.  
  331.                 if (PlotYPen.Color != Color.Coral)
  332.                 {
  333.                     PlotYPen = new Pen(Color.Coral, 0.2F);
  334.                     update = true;
  335.                 }
  336.             }
  337.  
  338.             if (update)
  339.                 this.Refresh();
  340.         }
  341.  
  342.         private void FunctionPloterForm_Resize(object sender, EventArgs e)
  343.         {
  344.             initOffsets();
  345.             this.Refresh();
  346.         }
  347.  
  348.      
  349.     }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement