From dec766984245776bac2c4618a477c09e7a5c68aa Mon Sep 17 00:00:00 2001 From: maxluli Date: Wed, 1 Jun 2022 14:57:51 +0200 Subject: [PATCH] The bezier curve generator is now fully functionnal (LETSGOOOOOOOOO) --- Paint_2/BezierPencil.cs | 75 ++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/Paint_2/BezierPencil.cs b/Paint_2/BezierPencil.cs index 64cf6f4..ce7f59b 100644 --- a/Paint_2/BezierPencil.cs +++ b/Paint_2/BezierPencil.cs @@ -41,8 +41,9 @@ namespace Paint_2 WidthsRedo = new List(); Name = name; - MessageBox.Show("Lerp test\nTest with start 20;20 and end 40;40 with 0.5f\n result : "+Lerp(new Point(20,20),new Point(40,40),0.5f)); - MessageBox.Show("Lerp test\nTest with start 40;40 and end 20;20 with 0.5f\n result : " + Lerp(new Point(40, 40), new Point(20, 20), 0.5f)); + //I KNOW I KNOW I LITTERALLY REINVENTED UNIT TEST BUT IF I CREATE SOME I WILL NEED TO DO THEM ALL STOP HARASSING ME + //MessageBox.Show("Lerp Test\nstart = 100;100\nend = 200;200\nt = 0.2\nExpected : 120;120 Actual : "+Lerp(new Point(100,100),new Point(200,200),0.2f)); + //MessageBox.Show("Lerp Test\nstart = 200;200\nend = 100;100\nt = 0.2\nExpected : 180;180 Actual : " + Lerp(new Point(200, 200), new Point(100, 100), 0.2f)); } public void Add(Point point) { @@ -90,9 +91,41 @@ namespace Paint_2 { //float xRatio = Math.Min((float)start.X,end.X) / Math.Max((float)start.X,end.X); //float yRatio = Math.Min((float)start.Y, end.Y) / Math.Max((float)start.Y, end.Y); - float xDiff = Math.Abs(start.X - end.X); - float yDiff = Math.Abs(start.Y -end.Y); - Point result = new Point(Math.Min(start.X,end.X) + (int)(t *xDiff),Math.Min(start.Y,end.Y) + (int)(t * yDiff)); + int xDiff = end.X - start.X; + 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; } public void Paint(Bitmap canvas) @@ -129,24 +162,40 @@ namespace Paint_2 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 t = 0.5f; + float precision = 0.01f; - //Drawing - Point p1_2 = Lerp(p1, p2, t); - Point p2_2 = Lerp(p2, p3, t); - Point p3_2 = Lerp(p3, p4, t); + 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.FillEllipse(new SolidBrush(Color.BlueViolet),new Rectangle(p1_2.X,p1_2.Y,10,10)); + //gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p1_2, p2_2); + //gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p2_2, p3_2); - gr.DrawLine(new Pen(Color.Pink,10),p1_2,p2_2); - gr.DrawLine(new Pen(Color.Red, 10), 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])); + } } } } } + private Color GetRandomColor() + { + Random rnd = new Random(); + return Color.FromArgb(rnd.Next(0,256), rnd.Next(0, 256), rnd.Next(0, 256)); + } public void Start(Color color, int width) { Color = color;