diff --git a/Paint_2/BezierPencil.cs b/Paint_2/BezierPencil.cs index 2879d17..64cf6f4 100644 --- a/Paint_2/BezierPencil.cs +++ b/Paint_2/BezierPencil.cs @@ -40,6 +40,9 @@ namespace Paint_2 Widths = new List(); 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)); } public void Add(Point point) { @@ -83,15 +86,14 @@ namespace Paint_2 } return result; } - private Point LerpY(Point start, Point end, int x) + private Point Lerp(Point start, Point end, float t) { - int newY = start.Y + (int)((float)(x - start.X) * (float)(((float)end.Y - (float)start.Y) / ((float)end.X - (float)start.X))); - return new Point(x, newY); - } - private Point LerpX(Point start, Point end, int y) - { - int newX = start.X + (int)((float)(y - start.Y) * (((float)end.Y - (float)start.Y) / ((float)end.X - (float)start.X))); - return new Point(newX, y); + //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)); + return result; } public void Paint(Bitmap canvas) { @@ -117,7 +119,7 @@ namespace Paint_2 if (i % 4 == 0) { - //Drawing the first level of curve + //Drawing the given sharp outline Point p1 = points[i - 4]; Point p2 = points[i - 3]; Point p3 = points[i - 2]; @@ -127,42 +129,20 @@ 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); - //Drawing the second level of curve + float t = 0.5f; - // Calculate the lowest x change - int range = Math.Min(Math.Abs(p4.X - p3.X), Math.Min(Math.Abs(p2.X - p1.X), Math.Abs(p3.X - p2.X))); - float firstOffset = Math.Abs(p2.X - p1.X) / range; - float secondOffset = Math.Abs(p3.X - p2.X) / range; - float thirdOffset = Math.Abs(p4.X - p3.X) / range; + //Drawing + Point p1_2 = Lerp(p1, p2, t); + Point p2_2 = Lerp(p2, p3, t); + Point p3_2 = Lerp(p3, p4, t); - for (int i = 0;i < range;i++) - { + gr.FillEllipse(new SolidBrush(Color.BlueViolet),new Rectangle(p1_2.X,p1_2.Y,10,10)); - } + gr.DrawLine(new Pen(Color.Pink,10),p1_2,p2_2); + gr.DrawLine(new Pen(Color.Red, 10), p2_2,p3_2); - Point p1_2 = LerpY(p1, p2, p1.X +(int)(range / 2.0 * firstOffset)); - Point p2_2 = LerpY(p2, p3, p2.X +(int)(range / 2.0 * secondOffset)); - Point p3_2 = LerpY(p3, p4, p3.X +(int)(range / 2.0 * thirdOffset)); - gr.DrawLine(new Pen(Colors[i - 1], Widths[i - 1]), p1_2, p2_2); - gr.DrawLine(new Pen(Colors[i - 1], Widths[i - 1]), p2_2, p3_2); - //Drawing the thirs level of curve - range = Math.Min(Math.Abs(p2_2.X - p1_2.X), Math.Abs(p3_2.X - p2_2.X)); - firstOffset = Math.Abs(p2_2.X - p1_2.X) / range; - secondOffset = Math.Abs(p3_2.X - p2_2.X) / range; - - Point p1_3 = LerpY(p1_2, p2_2, p1_2.X + (int)(range / 2.0 * firstOffset)); - Point p2_3 = LerpY(p2_2, p3_2, p2_2.X + (int)(range / 2.0 * secondOffset)); - - gr.DrawLine(new Pen(Color.Pink, 5), p1_3, p2_3); - - //Drawing the bezier point - range = Math.Abs(p2_3.X - p1_3.X); - firstOffset = Math.Abs(p2_3.X - p1_3.X) / range; - - Point p1_4 = LerpY(p1_3, p2_3, p1_3.X + (int)(range / 2.0 * firstOffset)); - gr.FillEllipse(new SolidBrush(Color.AliceBlue), new Rectangle(new Point(p1_4.X - pointSize.Width / 2, p1_4.Y - pointSize.Height / 2), pointSize)); } } }