Advertisement
csaki

Grafika - Hermit görbe

Nov 12th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 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 hermit_gorbe
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         Graphics g;
  15.         List<Point> points = new List<Point>();
  16.         List<HermitPoints> hermits = new List<HermitPoints>();
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             g = CreateGraphics();
  22.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  23.             MouseClick += new MouseEventHandler(Form1_MouseClick);
  24.         }
  25.  
  26.         void Form1_MouseClick(object sender, MouseEventArgs e)
  27.         {
  28.             if (points.Count < 4)
  29.             {
  30.                 points.Add(e.Location);
  31.                 DrawPoints();
  32.                 if (points.Count == 4)
  33.                 {
  34.                     hermits.Add(new HermitPoints(points[0], points[1], points[2], points[3]));
  35.                     for (int i = 0; i < 3; i++)
  36.                     {
  37.                         points.RemoveAt(0);
  38.                     }
  39.                 }
  40.             }
  41.             else
  42.             {
  43.                 MessageBox.Show("4 pontot tehetsz!");
  44.             }
  45.         }
  46.  
  47.         private void button1_Click(object sender, EventArgs e)
  48.         {
  49.             if (hermits.Count < 1)
  50.             {
  51.                 MessageBox.Show("Legalább 4 pont kell a hermit görbéhez! Neked csak " + points.Count + " pontod van.");
  52.             }
  53.             else
  54.             {
  55.                 Invalidate();
  56.                 Update();
  57.                 DrawPoints();
  58.                 DrawHermit();
  59.             }
  60.         }
  61.  
  62.         public void DrawHermit()
  63.         {
  64.             for (int i = 0; i < hermits.Count; i++)
  65.             {
  66.                 for (float u = 0; u <= 1; u = u + 0.01f)
  67.                 {
  68.                     double x = CalculateHermit(hermits[i].Start.X, hermits[i].End.X, hermits[i].startDirection.X - hermits[i].Start.X, hermits[i].End.X - hermits[i].endDirection.X, u);
  69.                     double y = CalculateHermit(hermits[i].Start.Y, hermits[i].End.Y, hermits[i].startDirection.Y - hermits[i].Start.Y, hermits[i].End.Y - hermits[i].endDirection.Y, u);
  70.                     g.FillEllipse(Brushes.Black, Convert.ToInt32(x - 2), Convert.ToInt32(y - 2), 4, 4);
  71.                 }
  72.             }
  73.             points.Clear();
  74.             hermits.Clear();
  75.         }
  76.  
  77.         public double CalculateHermit(float a0, float a1, float a2, float a3, float u)
  78.         {
  79.             return (2 * Math.Pow(u, 3) - 3 * Math.Pow(u, 2) + 1) * a0 + (-2 * Math.Pow(u, 3) + 3 * Math.Pow(u, 2)) * a1 + (Math.Pow(u, 3) - 2 * Math.Pow(u, 2) + u) * a2 + (Math.Pow(u, 3) - Math.Pow(u, 2)) * a3;
  80.         }
  81.  
  82.         public void DrawPoints()
  83.         {
  84.             foreach (var p in points)
  85.             {
  86.                 g.DrawEllipse(Pens.Blue, p.X - 5, p.Y - 5, 10, 10);
  87.                 g.FillEllipse(Brushes.Red, p.X - 5, p.Y - 5, 10, 10);
  88.             }
  89.             foreach (var h in hermits)
  90.             {
  91.         // NINCS KÉSZ MÉG
  92.                 g.DrawEllipse(Pens.Blue, h.Start.X - 5, h.Start.Y - 5, 10, 10);
  93.                 g.FillEllipse(Brushes.Red, h.Start.X - 5, h.Start.Y - 5, 10, 10);
  94.  
  95.                 g.DrawEllipse(Pens.Blue, h.Start.X - 5, h.Start.Y - 5, 10, 10); // startdirection
  96.                 g.FillEllipse(Brushes.Red, h.Start.X - 5, h.Start.Y - 5, 10, 10);
  97.  
  98.                 g.DrawEllipse(Pens.Blue, h.Start.X - 5, h.Start.Y - 5, 10, 10); // enddirection
  99.                 g.FillEllipse(Brushes.Red, h.Start.X - 5, h.Start.Y - 5, 10, 10);
  100.  
  101.                 g.DrawEllipse(Pens.Blue, h.Start.X - 5, h.Start.Y - 5, 10, 10);
  102.                 g.FillEllipse(Brushes.Red, h.Start.X - 5, h.Start.Y - 5, 10, 10); // end
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement