Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace deCasteljau
- {
- public partial class Form1 : Form
- {
- Graphics g;
- public Form1()
- {
- InitializeComponent();
- g = CreateGraphics();
- MouseUp += new MouseEventHandler(Form1_MouseUp);
- MouseDown +=new MouseEventHandler(Form1_MouseDown);
- MouseMove +=new MouseEventHandler(Form1_MouseMove);
- }
- const int DB = 200;
- static PointF[] kp;
- static List<PointF> pontok = new List<PointF>();
- static int R = 4;
- static bool megvan_ = false;
- static int pontIndex = 0;
- private PointF deCasteljau_iter2(float u)
- {
- int n = kp.Length - 1;
- PointF[] Q = new PointF[n + 1];
- for (int i = 0; i <= n; i++)
- Q[i] = kp[i];
- for (int r = 1; r <= n; r++)
- for (int i = 0; i <= n - r; i++)
- Q[i] = new PointF((1 - u) * Q[i].X + u * Q[i + 1].X, (1 - u) * Q[i].Y + u * Q[i + 1].Y);
- return Q[0];
- }
- private PointF deCasteljau_rek(int i, int r, float u)
- {
- if (r == 0) { return kp[i]; }
- else
- {
- PointF P1 = deCasteljau_rek(i, r - 1, u);
- PointF P2 = deCasteljau_rek(i + 1, r - 1, u);
- return new PointF(((1 - u) * P1.X + u * P2.X), ((1 - u) * P1.Y + u * P2.Y));
- }
- }
- private void drawDeCasteljau_iter()
- {
- //Invalidate();
- //Update();
- Graphics myG = CreateGraphics();
- int db = DB;
- double A = 0;
- double b = 1;
- double h = Math.Abs(b - A) / db;//h:lépésköz, db:a görbe pontjainak a szama
- double u = A;
- PointF[] pontok = new PointF[db + 1];//a görbe pontjai
- for (int i = 0; i <= db; i++)
- {
- // pontok[i] = deCasteljau(0, kp.Length - 1, (float)u);
- pontok[i] = deCasteljau_iter2((float)u);
- u += h;
- }
- myG.DrawLines(new Pen(Color.Blue), pontok);
- }
- private void drawDeCasteljau()
- {
- //Invalidate();
- //Update();
- Graphics myG = CreateGraphics();
- int db = DB;
- double A = 0;
- double b = 1;
- double h = Math.Abs(b - A) / db;//h:lépésköz, db:a görbe pontjainak a szama
- double u = A;
- PointF[] pontok = new PointF[db + 1];//a görbe pontjai
- for (int i = 0; i <= db; i++)
- {
- // pontok[i] = deCasteljau(0, kp.Length - 1, (float)u);
- pontok[i] = Bezier((float)u);
- u += h;
- }
- myG.DrawLines(new Pen(Color.Blue), pontok);
- }
- private PointF Bezier(float u)
- {
- int n = kp.Length - 1;
- PointF point = new PointF();
- for (int i = 0; i <= n; i++)
- {
- float b = Bernstein((double)i, (double)n, u);
- point.X += kp[i].X * b;
- point.Y += kp[i].Y * b;
- }
- return point;
- }
- private int Fact(double n)
- {
- int x = 1;
- for (int i = 1; i <= n; i++)
- x *= i;
- return x;
- }
- private float Bernstein(double i, double n, float u)
- {
- if (i < 0 || i > n) return 0;
- return (float)(Fact(n) / (Fact(i) * Fact(n - i)) * Math.Pow(u, i) * Math.Pow(1 - u, n - i));
- }
- private void Rajz()
- {
- kp = new PointF[pontok.Count];
- for (int i = 0; i < kp.Length; i++)
- kp[i] = pontok[i];
- g.DrawLines(new Pen(Color.Black), kp);
- drawDeCasteljau();
- }
- private void Form1_MouseUp(object sender, MouseEventArgs e)
- {
- if (!megvan_) { pontok.Add(new PointF(e.X, e.Y)); }
- Invalidate();
- Update();
- foreach (PointF x in pontok)
- {
- g.DrawEllipse(Pens.Red, x.X - R, x.Y - R, 2 * R, 2 * R);
- }
- if (pontok.Count >= 3) Rajz();
- megvan_ = false;
- }
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- for (int i = 0; i < pontok.Count; i++)
- {
- PointF x = pontok[i];
- if (e.X > x.X - R && e.X < x.X + R && e.Y > x.Y - R && e.Y < x.Y + R)
- {
- pontIndex = i;
- megvan_ = true;
- break;
- }
- }
- if (e.Button == System.Windows.Forms.MouseButtons.Right)
- {
- pontok.RemoveAt(pontIndex);
- }
- }
- private void Form1_MouseMove(object sender, MouseEventArgs e)
- {
- if (megvan_)
- {
- pontok[pontIndex] = new PointF(e.X, e.Y);
- Invalidate();
- Update();
- Rajz();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement