Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Graphic
- {
- public partial class Form1 : Form
- {
- private static readonly float MAX_Y_AXIS_HEIGHT = 1e5F;
- private static readonly float MIN_Y_AXIS_HEIGHT = -1e5F;
- private static readonly float MAX_X_AXIS_WIDTH = 1e5F;
- private static readonly float MIN_X_AXIS_WIDTH = -1e5F;
- public Form1()
- {
- InitializeComponent();
- }
- /*
- * Отрисовка графика по нажатию кнопки
- */
- private void Button1_Click(object sender, EventArgs e)
- {
- var formula = textBox1.Text;
- Function function = new Function(formula);
- pictureBox1.Refresh();
- BuildGraph(function, GetPoints(function, double.Parse(textBox2.Text), double.Parse(textBox3.Text), double.Parse(textBox4.Text)));
- }
- /*
- * Запрет ввода пробелов
- */
- private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!char.IsWhiteSpace(e.KeyChar)) return;
- else e.Handled = true;
- }
- /*
- * Построение графика функции
- */
- private void BuildGraph(Function function, List<Pair> points)
- {
- label5.Text = function.MethodChord(-10, 10, 0.01).ToString();
- float coef_y = 1, coef_x = 1;
- float max_y = 0, min_y = float.MaxValue;
- float max_x = 0, min_x = float.MaxValue;
- for (var i = 0; i < points.Count; i++)
- {
- if (points[i].second > max_y)
- max_y = points[i].second;
- if (points[i].second < min_y)
- min_y = points[i].second;
- if (points[i].first > max_x)
- max_x = points[i].first;
- if (points[i].first < min_x)
- min_x = points[i].first;
- }
- coef_x = pictureBox1.Width/ Math.Abs(min_x - max_x);
- if (float.IsInfinity(max_y))
- coef_y = pictureBox1.Height / Math.Abs(100500F);
- else
- coef_y = pictureBox1.Height / Math.Abs(max_y);
- using (Graphics gr = pictureBox1.CreateGraphics())
- {
- gr.TranslateTransform((pictureBox1.Right + pictureBox1.Left) / 2, (pictureBox1.Bottom + pictureBox1.Top) / 2);
- Pen pen = new Pen(Color.Black, 1);
- Brush brush = new SolidBrush(pictureBox1.BackColor);
- pen.Width = 0.01F;
- gr.DrawLine(pen, 0, MIN_Y_AXIS_HEIGHT, 0, MAX_Y_AXIS_HEIGHT); //Ось x
- gr.DrawLine(pen, MIN_X_AXIS_WIDTH, 0, MAX_X_AXIS_WIDTH, 0); //Ось y
- pen.Color = Color.Red;
- gr.ScaleTransform(coef_x, coef_y);
- float last_x = points[0].first;
- float last_y = -points[0].second;
- for (var i = 1; i < points.Count; i++)
- {
- float next_x = points[i].first;
- float next_y = -points[i].second;
- if (next_y > MAX_Y_AXIS_HEIGHT)
- {
- last_x = points[i].first;
- last_y = MIN_Y_AXIS_HEIGHT;
- continue;
- }
- if (next_y < MIN_Y_AXIS_HEIGHT)
- {
- last_x = points[i].first;
- last_y = MAX_Y_AXIS_HEIGHT;
- continue;
- }
- gr.DrawLine(pen, last_x, last_y, next_x, next_y);
- last_x = next_x;
- last_y = next_y;
- }
- pen.Dispose();
- }
- }
- /*
- * Возращает набор точек, используя функцию
- */
- private List<Pair> GetPoints(Function function, double start, double end, double step)
- {
- List<Pair> points = new List<Pair>();
- for (double val = start; val <= end; val += step)
- {
- double next_y = function.GetValue(val);
- points.Add(new Pair((float)val, (float)next_y));
- }
- return points;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement