From 8cdfc0acfaa74db96599c568d0e387bbefe871aa Mon Sep 17 00:00:00 2001 From: maxluli Date: Fri, 3 Jun 2022 11:13:49 +0200 Subject: [PATCH] Cleaned Bezier code to prepare the following --- Paint_2/BezierPencil.cs | 96 +++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 62 deletions(-) diff --git a/Paint_2/BezierPencil.cs b/Paint_2/BezierPencil.cs index ce7f59b..bf277e8 100644 --- a/Paint_2/BezierPencil.cs +++ b/Paint_2/BezierPencil.cs @@ -95,36 +95,6 @@ namespace Paint_2 int yDiff = end.Y - start.Y; int resultX = start.X + (int)(t * xDiff); int resultY = start.Y + (int)(t * yDiff); - /* - if (xDiff > 0) - { - //Ex start 100 end 200 - resultX = start.X + (int)(t * xDiff); - //resultX = (int)(t * xDiff); - } - else if(xDiff < 0){ - //Ex start 200 end 100 - resultX = start.X + (int)(t * xDiff); - } - else - { - resultX = 0; - } - if (yDiff > 0) - { - //Ex start 100 end 200 - resultY = - } - else if (yDiff < 0) - { - //Ex start 200 end 100 - resultY = start.Y + (int)(t * yDiff); - } - else - { - resultY = 0; - } - */ Point result = new Point(resultX,resultY); return result; } @@ -149,48 +119,50 @@ namespace Paint_2 { pointSize = new Size(Widths[i - 1], Widths[i - 1]); gr.FillEllipse(new SolidBrush(Colors[i - 1]), new Rectangle(new Point(points[i - 1].X - pointSize.Width / 2, points[i - 1].Y - pointSize.Height / 2), pointSize)); - - if (i % 4 == 0) + if(i >= 4) { - //Drawing the given sharp outline Point p1 = points[i - 4]; Point p2 = points[i - 3]; Point p3 = points[i - 2]; Point p4 = points[i - 1]; - gr.DrawLine(new Pen(Colors[i - 4], Widths[i - 4]), p1, p2); - gr.DrawLine(new Pen(Colors[i - 3], Widths[i - 3]), p2, p3); - gr.DrawLine(new Pen(Colors[i - 2], Widths[i - 2]), p3, p4); - - //float t = 0.5f; - float precision = 0.01f; - - for (float t = 0; t <= 1; t += precision) - { - //float t = 0.2f; - //Drawing the first step of the curve - Point p1_2 = Lerp(p1, p2, t); - Point p2_2 = Lerp(p2, p3, t); - Point p3_2 = Lerp(p3, p4, t); - - //gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p1_2, p2_2); - //gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p2_2, p3_2); - - //Drawing the second step of the curve - Point p1_3 = Lerp(p1_2, p2_2, t); - Point p2_3 = Lerp(p2_2, p3_2, t); - - //gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p1_3, p2_3); - - //Drawing the Bezier Point - Point p1_4 = Lerp(p1_3, p2_3, t); - - gr.FillEllipse(new SolidBrush(GetRandomColor()), new Rectangle(p1_4.X, p1_4.Y, Widths[0], Widths[0])); - } + BezierGenerator(gr,p1,p2,p3,p4,i); } } } } + private void BezierGenerator(Graphics gr,Point p1,Point p2,Point p3,Point p4,int i) + { + gr.DrawLine(new Pen(Colors[i - 4], Widths[i - 4]), p1, p2); + gr.DrawLine(new Pen(Colors[i - 3], Widths[i - 3]), p2, p3); + gr.DrawLine(new Pen(Colors[i - 2], Widths[i - 2]), p3, p4); + + //float t = 0.5f; + float precision = 0.01f; + + for (float t = 0; t <= 1; t += precision) + { + //Quadratic Bezier Curve creator + //Drawing the first step of the curve + Point p1_2 = Lerp(p1, p2, t); + Point p2_2 = Lerp(p2, p3, t); + Point p3_2 = Lerp(p3, p4, t); + + //gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p1_2, p2_2); + //gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p2_2, p3_2); + + //Drawing the second step of the curve + Point p1_3 = Lerp(p1_2, p2_2, t); + Point p2_3 = Lerp(p2_2, p3_2, t); + + //gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p1_3, p2_3); + + //Drawing the Bezier Point + Point p1_4 = Lerp(p1_3, p2_3, t); + + gr.FillEllipse(new SolidBrush(GetRandomColor()), new Rectangle(p1_4.X - Widths[0] / 2, p1_4.Y - Widths[0] / 2, Widths[0], Widths[0])); + } + } private Color GetRandomColor() { Random rnd = new Random();