Advertisement
csaki

sierpinski

Sep 17th, 2013
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 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 sierpinski
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         Graphics g;
  15.         Pen pen;
  16.         PointF A, B, C;
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             g = CreateGraphics();
  21.             pen = new Pen(Color.Black);
  22.             A = new PointF(500f, 200f);
  23.             B = new PointF(250f, 400f);
  24.             C = new PointF(550f, 500f);
  25.         }
  26.  
  27.         public PointF Felez(PointF A, PointF B)
  28.         {
  29.             return new PointF((A.X + B.X) / 2, (A.Y + B.Y) / 2);
  30.         }
  31.  
  32.         public double Tavolsag(PointF A, PointF B)
  33.         {
  34.             return Math.Sqrt((A.X - B.X) + (A.Y - B.Y));
  35.         }
  36.  
  37.         public void Sierp(PointF A, PointF B, PointF C)
  38.         {
  39.             PointF AB = Felez(A, B);
  40.             PointF AC = Felez(A, C);
  41.             PointF BC = Felez(B, C);
  42.  
  43.             if (Tavolsag(A, B) >= 1)
  44.             {
  45.                 Sierp(A, AB, AC);
  46.                 Sierp(B, BC, AB);
  47.                 Sierp(C, BC, AC);
  48.             }
  49.             if (Tavolsag(A, C) >= 1)
  50.             {
  51.                 Sierp(A, AB, AC);
  52.                 Sierp(B, BC, AB);
  53.                 Sierp(C, BC, AC);
  54.             }
  55.             if (Tavolsag(B, C) >= 1)
  56.             {
  57.                 Sierp(A, AB, AC);
  58.                 Sierp(B, BC, AB);
  59.                 Sierp(C, BC, AC);
  60.             }
  61.             else
  62.             {
  63.                 //Kirajzolás
  64.                 var points = new PointF[] { A, B, C };
  65.                 g.DrawPolygon(pen, points);
  66.             }
  67.         }
  68.  
  69.         private void button1_Click(object sender, EventArgs e)
  70.         {
  71.             Sierp(A, B, C);
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement