Advertisement
Guest User

Untitled

a guest
May 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace Graphic
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         private static readonly float MAX_Y_AXIS_HEIGHT = 1e5F;
  11.         private static readonly float MIN_Y_AXIS_HEIGHT = -1e5F;
  12.         private static readonly float MAX_X_AXIS_WIDTH = 1e5F;
  13.         private static readonly float MIN_X_AXIS_WIDTH = -1e5F;
  14.  
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         /*
  21.          * Отрисовка графика по нажатию кнопки
  22.          */
  23.         private void Button1_Click(object sender, EventArgs e)
  24.         {
  25.             var formula = textBox1.Text;
  26.             Function function = new Function(formula);
  27.             pictureBox1.Refresh();
  28.             BuildGraph(function, GetPoints(function, double.Parse(textBox2.Text), double.Parse(textBox3.Text), double.Parse(textBox4.Text)));
  29.         }
  30.  
  31.         /*
  32.          * Запрет ввода пробелов
  33.          */
  34.         private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
  35.         {
  36.             if (!char.IsWhiteSpace(e.KeyChar)) return;
  37.             else e.Handled = true;
  38.         }
  39.  
  40.         /*
  41.          * Построение графика функции
  42.          */
  43.         private void BuildGraph(Function function, List<Pair> points)
  44.         {
  45.             label5.Text = function.MethodChord(-10, 10, 0.01).ToString();
  46.  
  47.             float coef_y = 1, coef_x = 1;
  48.             float max_y = 0, min_y = float.MaxValue;
  49.             float max_x = 0, min_x = float.MaxValue;
  50.  
  51.             for (var i = 0; i < points.Count; i++)
  52.             {
  53.                 if (points[i].second > max_y)
  54.                     max_y = points[i].second;
  55.                 if (points[i].second < min_y)
  56.                     min_y = points[i].second;
  57.                 if (points[i].first > max_x)
  58.                     max_x = points[i].first;
  59.                 if (points[i].first < min_x)
  60.                     min_x = points[i].first;
  61.             }
  62.  
  63.             coef_x = pictureBox1.Width/ Math.Abs(min_x - max_x);
  64.             if (float.IsInfinity(max_y))
  65.                 coef_y = pictureBox1.Height / Math.Abs(100500F);
  66.             else
  67.                 coef_y = pictureBox1.Height / Math.Abs(max_y);
  68.  
  69.             using (Graphics gr = pictureBox1.CreateGraphics())
  70.             {
  71.                 gr.TranslateTransform((pictureBox1.Right + pictureBox1.Left) / 2, (pictureBox1.Bottom + pictureBox1.Top) / 2);
  72.                 Pen pen = new Pen(Color.Black, 1);
  73.                 Brush brush = new SolidBrush(pictureBox1.BackColor);
  74.  
  75.                 pen.Width = 0.01F;
  76.                 gr.DrawLine(pen, 0, MIN_Y_AXIS_HEIGHT, 0, MAX_Y_AXIS_HEIGHT); //Ось x
  77.                 gr.DrawLine(pen, MIN_X_AXIS_WIDTH, 0, MAX_X_AXIS_WIDTH, 0); //Ось y
  78.  
  79.                 pen.Color = Color.Red;
  80.                 gr.ScaleTransform(coef_x, coef_y);
  81.  
  82.                 float last_x = points[0].first;
  83.                 float last_y = -points[0].second;
  84.                 for (var i = 1; i < points.Count; i++)
  85.                 {
  86.                     float next_x = points[i].first;
  87.                     float next_y = -points[i].second;
  88.  
  89.                     if (next_y > MAX_Y_AXIS_HEIGHT)
  90.                     {
  91.                         last_x = points[i].first;
  92.                         last_y = MIN_Y_AXIS_HEIGHT;
  93.                         continue;
  94.                     }
  95.                     if (next_y < MIN_Y_AXIS_HEIGHT)
  96.                     {
  97.                         last_x = points[i].first;
  98.                         last_y = MAX_Y_AXIS_HEIGHT;
  99.                         continue;
  100.                     }
  101.                    
  102.                     gr.DrawLine(pen, last_x, last_y, next_x, next_y);
  103.  
  104.                     last_x = next_x;
  105.                     last_y = next_y;
  106.                 }
  107.  
  108.                 pen.Dispose();
  109.             }
  110.         }
  111.  
  112.         /*
  113.          * Возращает набор точек, используя функцию
  114.          */
  115.         private List<Pair> GetPoints(Function function, double start, double end, double step)
  116.         {
  117.             List<Pair> points = new List<Pair>();
  118.             for (double val = start; val <= end; val += step)
  119.             {
  120.                 double next_y = function.GetValue(val);
  121.                 points.Add(new Pair((float)val, (float)next_y));
  122.             }
  123.             return points;
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement