Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.27 KB | None | 0 0
  1. Исходный код программы:
  2. Form1.cs
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.Drawing.Imaging;
  15. using System.Windows.Forms;
  16.  
  17. namespace lab1
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         void DrawLeaves()
  22.         {
  23.             Point[] leaves1 =
  24.             {
  25.                 new Point(175, 150),
  26.                 new Point(185, 85),
  27.                 new Point(230, 125),
  28.                 new Point(175, 150),
  29.             };
  30.             Point[] leaves2 =
  31.             {
  32.                 new Point(220, 120),
  33.                 new Point(205, 55),
  34.                 new Point(190, 35),
  35.                 new Point(220, 20),
  36.                 new Point(240, 40),
  37.                 new Point(260, 50),
  38.              
  39.                 new Point(220, 120),
  40.             };
  41.             Point[] leaves3 =
  42.             {
  43.                 new Point(300, 120),
  44.                 new Point(260, 70),
  45.                 new Point(300, 45),
  46.                 new Point(380, 70),
  47.                 new Point(340, 90),
  48.                 new Point(300, 120),
  49.             };
  50.             Graphics g = this.CreateGraphics();
  51.             Pen penRed = new Pen(Color.Green, 6);
  52.  
  53.             g.DrawCurve(penRed, leaves1);
  54.             g.DrawCurve(penRed, leaves2);
  55.             g.DrawCurve(penRed, leaves3);
  56.         }
  57.         void DrawTree()
  58.         {
  59.             Point[] polygon1 =
  60.             {
  61.                 new Point(185, 350),
  62.                 new Point(210, 300),
  63.                 new Point(210, 150),
  64.                
  65.                 new Point(190, 120),
  66.                 new Point(180, 90),
  67.                 new Point(185, 90),
  68.                 new Point(210, 120),
  69.  
  70.                 new Point(280, 150),
  71.                 new Point(280, 310),
  72.                 new Point(310, 350),
  73.                
  74.             };
  75.             Point[] polygon2 =
  76.             {
  77.                 new Point(210, 130),
  78.                 new Point(220, 90),
  79.                 new Point(220, 50),
  80.                 new Point(195, 30),
  81.                 new Point(205, 20),
  82.                 new Point(235, 50),
  83.                 new Point(230, 80),
  84.                 new Point(230, 130),
  85.                            
  86.             };
  87.             Point[] polygon3 =
  88.             {
  89.                 new Point(210, 130),
  90.                 new Point(295, 90),
  91.                 new Point(310, 70),
  92.                 new Point(320, 70),
  93.                 new Point(315, 90),
  94.                 new Point(280, 150),
  95.                            
  96.             };
  97.            
  98.             SolidBrush SolidBrown = new SolidBrush(Color.Brown);
  99.             Graphics g = this.CreateGraphics();
  100.             Pen penRed = new Pen(Color.Green, 6);
  101.            
  102.             g.FillPolygon(SolidBrown, polygon1);
  103.             g.FillPolygon(SolidBrown, polygon2);
  104.             g.FillPolygon(SolidBrown, polygon3);
  105.        
  106.         }
  107.         void Animation()
  108.         {
  109.            Point[] leaves1 =
  110.             {
  111.                 new Point(175, 150),
  112.                 new Point(185, 85),
  113.                 new Point(230, 125),
  114.                 new Point(175, 150),
  115.             };
  116.  
  117.             Point[] leaves2 =
  118.             {
  119.                 new Point(220, 120),
  120.                 new Point(205, 55),
  121.                 new Point(190, 35),
  122.                 new Point(220, 20),
  123.                 new Point(240, 40),
  124.                 new Point(260, 50),
  125.                 new Point(220, 120),
  126.             };
  127.             Point[] leaves3 =
  128.             {
  129.                 new Point(300, 120),
  130.                 new Point(260, 70),
  131.                 new Point(300, 45),
  132.                 new Point(380, 70),
  133.                 new Point(340, 90),
  134.                 new Point(300, 120),
  135.             };
  136.  
  137.             Graphics g = this.CreateGraphics();
  138.             Pen penBrown = new Pen(Color.Green, 6);
  139.             g.Clear(Color.White);
  140.  
  141.             int step = 5;
  142.          
  143.  
  144.             for (int i = 1; i < 10; i++)
  145.             {
  146.                
  147.                 for (int j = 0; j < 4; j++) leaves1[j].X = leaves1[j].X + step;
  148.                 for (int j = 0; j < 7; j++) leaves2[j].X = leaves2[j].X + step;
  149.                 for (int j = 0; j < 4; j++) leaves3[j].X = leaves3[j].X + step;
  150.                                
  151.                 DrawTree();
  152.                 g.DrawCurve(penBrown, leaves1);
  153.                 g.DrawCurve(penBrown, leaves2);
  154.                 g.DrawCurve(penBrown, leaves3);
  155.                
  156.                
  157.                 Thread.Sleep(240);
  158.                 g.Clear(Color.White);
  159.  
  160.                 for (int j = 0; j < 4; j++) leaves1[j].X = leaves1[j].X - step;
  161.                 for (int j = 0; j < 7; j++) leaves2[j].X = leaves2[j].X - step;
  162.                 for (int j = 0; j < 4; j++) leaves3[j].X = leaves3[j].X - step;
  163.                
  164.                 DrawTree();
  165.                 g.DrawCurve(penBrown, leaves1);
  166.                 g.DrawCurve(penBrown, leaves2);
  167.                 g.DrawCurve(penBrown, leaves3);
  168.  
  169.  
  170.                 Thread.Sleep(240);
  171.                 if(i!=9) g.Clear(Color.White);
  172.             }
  173.         }
  174.         void DrawName()
  175.         {
  176.             Graphics g = this.CreateGraphics();
  177.             LinearGradientBrush myBrush = new LinearGradientBrush(ClientRectangle, Color.Black,
  178.                                                   Color.Yellow, LinearGradientMode.Horizontal);
  179.             Font myFont = new Font("Tahoma", 12, FontStyle.Regular);
  180.             g.DrawString("Шагалеев Гали", myFont, myBrush, new RectangleF(70, 130, 120, 200));
  181.         }
  182.         void DrawGraphic()
  183.         {
  184.             //система координат:
  185.             Graphics g = this.CreateGraphics();
  186.             g.Clear(Color.White);
  187.             Pen penBlue = new Pen(Color.Blue);
  188.             Font drawFont = new Font("Arial", 12);
  189.             Font signatureFont = new Font("Arial", 7);
  190.  
  191.             SolidBrush drawBrush = new SolidBrush(Color.Blue);
  192.             StringFormat drawFormat = new StringFormat();
  193.             drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
  194.  
  195.             int sizeWidth = Form1.ActiveForm.Width;
  196.             int sizeHeight = Form1.ActiveForm.Height;
  197.             Point center = new Point(((int)((sizeWidth / 2) - 100)), (int)((sizeHeight / 2) - 20));
  198.  
  199.             g.DrawLine(penBlue, 10, center.Y, center.X, center.Y);
  200.             g.DrawLine(penBlue, center.X, center.Y, 2 * center.X - 10, center.Y);
  201.             g.DrawLine(penBlue, center.X, 10, center.X, center.Y);
  202.             g.DrawLine(penBlue, center.X, center.Y, center.X, 2 * center.Y - 10);
  203.             g.DrawString("X", drawFont, drawBrush, new PointF(2 * center.X - 5, center.Y + 10), drawFormat);
  204.             g.DrawString("Y", drawFont, drawBrush, new PointF(center.X + 30, 5), drawFormat);
  205.             g.DrawString("O", drawFont, drawBrush, new PointF(center.X - 30, center.Y + 30), drawFormat);
  206.  
  207.             g.DrawLine(penBlue, center.X, 10, center.X + 5, 20);
  208.             g.DrawLine(penBlue, center.X, 10, center.X - 5, 20);
  209.             g.DrawLine(penBlue, 2 * center.X - 10, center.Y, 2 * center.X - 20, center.Y + 5);
  210.             g.DrawLine(penBlue, 2 * center.X - 10, center.Y, 2 * center.X - 20, center.Y - 5);
  211.  
  212.             int stepForAxes = 25;
  213.             int lenghtShtrih = 3;
  214.             int maxValueForAxesX = 4;
  215.             int maxValueForAxesY = 3;
  216.  
  217.             float oneDelenieX = (float)maxValueForAxesX / ((float)center.X / (float)stepForAxes);
  218.             float oneDelenieY = (float)maxValueForAxesY / ((float)center.Y / (float)stepForAxes);
  219.  
  220.             for (int i = center.X, j = center.X, k = 1; i < 2 * center.X - 30; j -= stepForAxes, i += stepForAxes, k++)
  221.             {
  222.                 g.DrawLine(penBlue, i, center.Y - lenghtShtrih, i, center.Y + lenghtShtrih);
  223.                 g.DrawLine(penBlue, j, center.Y - lenghtShtrih, j, center.Y + lenghtShtrih);
  224.  
  225.                 if (i < 2 * center.X - 55)
  226.                 {
  227.                     g.DrawString((k * oneDelenieX).ToString("0.0"), signatureFont, drawBrush,
  228.                         new PointF(i + stepForAxes + 9, center.Y + 6), drawFormat);
  229.                     g.DrawString((k * oneDelenieX).ToString("0.0") + "-", signatureFont, drawBrush,
  230.                         new PointF(j - stepForAxes + 9, center.Y + 6), drawFormat);
  231.                 }
  232.             }
  233.  
  234.  
  235.             for (int i = center.Y, j = center.Y, k = 1; j < 2 * center.Y - 30; j += stepForAxes, i -= stepForAxes, k++)
  236.             {
  237.                 g.DrawLine(penBlue, center.X - lenghtShtrih, i, center.X + lenghtShtrih, i);
  238.                 g.DrawLine(penBlue, center.X - lenghtShtrih, j, center.X + lenghtShtrih, j);
  239.  
  240.                 if (j < 2 * center.Y - 55)
  241.                 {
  242.                     g.DrawString((k * oneDelenieY).ToString("0.0"), signatureFont, drawBrush,
  243.                         new PointF(center.X - 6, i - stepForAxes - 9), drawFormat);
  244.                     g.DrawString((k * oneDelenieY).ToString("0.0") + "-", signatureFont, drawBrush,
  245.                         new PointF(center.X - 6, j + stepForAxes - 9), drawFormat);
  246.                 }
  247.             }
  248.             //функция:
  249.             int numOfPoint = 100;
  250.             double step = 4 / (double)numOfPoint;
  251.  
  252.             double[] first = new double[numOfPoint];
  253.             double deg = 0;
  254.             for (int i = 0; i < numOfPoint; i++)
  255.             {
  256.                 deg = deg + (2 * Math.PI / numOfPoint);
  257.                 first[i] = (double)(2 * Math.Cos(deg) - Math.Cos(2 * deg));
  258.  
  259.             }
  260.             deg = 0;
  261.             double[] second = new double[numOfPoint];
  262.             for (int i = 0; i < numOfPoint; i++)
  263.             {
  264.                 deg = deg + (2 * Math.PI / numOfPoint);
  265.                 second[i] = (double)(2 * Math.Sin(deg) - Math.Sin(2 * deg));
  266.             }
  267.  
  268.             Point[] pointOne = new Point[numOfPoint];
  269.  
  270.             float tempX = 1 / oneDelenieX * stepForAxes;
  271.             float tempY = 1 / oneDelenieY * stepForAxes;
  272.  
  273.             for (int i = 0; i < numOfPoint; i++)
  274.             {
  275.                 pointOne[i].X = center.X + (int)(first[i] * tempX);
  276.                 pointOne[i].Y = center.Y + (int)(second[i] * tempY);
  277.             }
  278.  
  279.             g.DrawLines(new Pen(Color.Red), pointOne);
  280.             g.DrawCurve(new Pen(Color.Green), pointOne);
  281.         } //отрисовка графика
  282.         void SaveScreen()
  283.         {
  284.             ImageFormat img = ImageFormat.Jpeg;
  285.             saveFileDialog1.ShowDialog();
  286.             switch (saveFileDialog1.FilterIndex)
  287.             {
  288.                 case 0: img = ImageFormat.Bmp; break;
  289.                 case 1: img = ImageFormat.Jpeg; break;
  290.                 case 2: img = ImageFormat.Png; break;
  291.             }
  292.             Rectangle bounds = this.Bounds;
  293.             Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
  294.             Graphics g = Graphics.FromImage(bitmap);
  295.             g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
  296.             bitmap.Save(saveFileDialog1.FileName, img);
  297.         }
  298.         void OpenPicture()
  299.         {
  300.             openFileDialog1.ShowDialog();
  301.             pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
  302.         }
  303.        
  304.  
  305.        
  306.         public Form1()
  307.         {
  308.            
  309.             InitializeComponent();
  310.             Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  311.             pictureBox2.Hide();
  312.             pictureBox1.Hide();
  313.         }
  314.  
  315.         private void button1_Click(object sender, EventArgs e)
  316.         {
  317.             pictureBox2.Hide();
  318.             pictureBox1.Hide();
  319.             DrawTree(); DrawLeaves();          
  320.         }
  321.  
  322.         private void button3_Click(object sender, EventArgs e)
  323.         {
  324.             pictureBox2.Hide();
  325.             pictureBox1.Hide();
  326.             Thread a = new Thread(Animation);
  327.             a.Start();
  328.  
  329.         }
  330.  
  331.         private void button2_Click(object sender, EventArgs e)
  332.         {
  333.             pictureBox2.Hide();
  334.             pictureBox1.Hide();
  335.             DrawName();
  336.         }
  337.  
  338.        
  339.  
  340.         private void button4_Click(object sender, EventArgs e)
  341.         {
  342.             pictureBox2.Show();
  343.             pictureBox1.Hide();
  344.             DrawGraphic();
  345.         }
  346.  
  347.         private void pictureBox1_Click(object sender, EventArgs e)
  348.         {
  349.  
  350.         }
  351.  
  352.         private void button5_Click(object sender, EventArgs e)
  353.         {
  354.             SaveScreen();
  355.         }
  356.  
  357.         private void button6_Click(object sender, EventArgs e)
  358.         {
  359.             pictureBox2.Hide();
  360.             OpenPicture();
  361.             pictureBox1.Show();
  362.  
  363.         }
  364.  
  365.         private void button7_Click(object sender, EventArgs e)
  366.         {
  367.             Graphics g = this.CreateGraphics();
  368.             g.Clear(Color.White);
  369.             Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  370.             Graphics gr = Graphics.FromImage(bitmap);
  371.             gr.Clear(Color.White);
  372.             pictureBox1.Image = bitmap;
  373.             pictureBox1.Hide();
  374.            
  375.         }
  376.     }
  377. }
  378. Пример выполнения программы:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement