diff --git a/Paint_2/BezierPencil.cs b/Paint_2/BezierPencil.cs index bf277e8..b06ce44 100644 --- a/Paint_2/BezierPencil.cs +++ b/Paint_2/BezierPencil.cs @@ -95,7 +95,7 @@ namespace Paint_2 int yDiff = end.Y - start.Y; int resultX = start.X + (int)(t * xDiff); int resultY = start.Y + (int)(t * yDiff); - Point result = new Point(resultX,resultY); + Point result = new Point(resultX, resultY); return result; } public void Paint(Bitmap canvas) @@ -117,21 +117,56 @@ namespace Paint_2 for (int i = 1; i <= points.Count; i++) { - pointSize = new Size(Widths[i - 1], Widths[i - 1]); + + pointSize = new Size(Widths[0], Widths[0]); 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) + /* + if (i >= 4) { Point p1 = points[i - 4]; Point p2 = points[i - 3]; Point p3 = points[i - 2]; Point p4 = points[i - 1]; - - BezierGenerator(gr,p1,p2,p3,p4,i); + BezierGenerator(gr, p1, p2, p3, p4, i); } + */ + ContinuousBezierGenerator(gr, points, i); } } } - private void BezierGenerator(Graphics gr,Point p1,Point p2,Point p3,Point p4,int i) + private void ContinuousBezierGenerator(Graphics gr, List points, int i) + { + if (points.Count >= 4 && points.Count % 2 == 0) + { + float precision = 0.01f; + for (float t = 0; t <= 1; t += precision) + { + List DumpList = new List(); + List WorkingList = new List(points); + + while (WorkingList.Count != 1) + { + if (WorkingList.Count > 2) + { + for (int pos = 0; pos < WorkingList.Count - 1; pos++) + { + //gr.DrawLine(new Pen(Color.Red), WorkingList[pos], WorkingList[pos + 1]); + //DumpList.Add(Lerp(WorkingList[pos], WorkingList[pos + 1], 0.5f)); + DumpList.Add(Lerp(WorkingList[pos], WorkingList[pos + 1], t)); + } + } + else + { + DumpList.Add(Lerp(WorkingList[0], WorkingList[1], precision)); + } + WorkingList = new List(DumpList); + DumpList = new List(); + } + gr.FillEllipse(new SolidBrush(GetRandomColor()), new Rectangle(WorkingList[0].X - Widths[0] / 2, WorkingList[0].Y - Widths[0] / 2, Widths[0], Widths[0])); + } + } + } + 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); @@ -166,7 +201,7 @@ namespace Paint_2 private Color GetRandomColor() { Random rnd = new Random(); - return Color.FromArgb(rnd.Next(0,256), rnd.Next(0, 256), rnd.Next(0, 256)); + return Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); } public void Start(Color color, int width) { @@ -224,5 +259,10 @@ namespace Paint_2 WidthsRedo.RemoveAt(WidthsRedo.Count - 1); } } + + public Bitmap Stop(Bitmap bmp) + { + return bmp; + } } } diff --git a/Paint_2/DotPencil.cs b/Paint_2/DotPencil.cs index 46aff74..e488fc7 100644 --- a/Paint_2/DotPencil.cs +++ b/Paint_2/DotPencil.cs @@ -179,5 +179,10 @@ namespace Paint_2 WidthsRedo.RemoveAt(WidthsRedo.Count - 1); } } + + public Bitmap Stop(Bitmap bmp) + { + return bmp; + } } } diff --git a/Paint_2/Form1.cs b/Paint_2/Form1.cs index ee1b53b..a1b0ad0 100644 --- a/Paint_2/Form1.cs +++ b/Paint_2/Form1.cs @@ -61,7 +61,7 @@ namespace Paint_2 { sketch.AddDrawingPoint(MousePositionToCanvasPosition()); drawing = false; - sketch.StopDrawing(); + canvas.Image = sketch.StopDrawing((Bitmap)canvas.Image); RefreshUi(); } diff --git a/Paint_2/PaintTool.cs b/Paint_2/PaintTool.cs index cfb1170..dc7d37d 100644 --- a/Paint_2/PaintTool.cs +++ b/Paint_2/PaintTool.cs @@ -25,7 +25,7 @@ namespace Paint_2 string Name { get; set; } void Start(Color color, int width); - void Stop(); + Bitmap Stop(Bitmap bmp); void Add(Point point); void Undo(); void Redo(); diff --git a/Paint_2/Pencil.cs b/Paint_2/Pencil.cs index a2c12dd..fc925c2 100644 --- a/Paint_2/Pencil.cs +++ b/Paint_2/Pencil.cs @@ -90,33 +90,64 @@ namespace Paint_2 Widths = new List(); WidthsRedo = new List(); } - public void Stop() + public Bitmap Stop(Bitmap bmp) { - for (int i = 0; i < POST_PROCESSING_PRECISION; i++) + for (int i = 0; i < Drawings.Count; i++) { - List Drawing = Drawings[Drawings.Count - 1]; - Drawings[Drawings.Count - 1] = PostProcessing(Drawing); + List Drawing = Drawings[i]; + //bmp = PostProcessing(Drawing, bmp, Widths[i]); + } + return bmp; + } + private Color GetRandomColor() + { + Random rnd = new Random(); + return Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + } + private void ContinuousBezierGenerator(Graphics gr, List points, int width) + { + if (points.Count >= 4 && points.Count % 2 == 0) + { + float precision = 0.01f; + for (float t = 0; t <= 1; t += precision) + { + List DumpList = new List(); + List WorkingList = new List(points); + + while (WorkingList.Count != 1) + { + if (WorkingList.Count > 2) + { + for (int pos = 0; pos < WorkingList.Count - 1; pos++) + { + DumpList.Add(Lerp(WorkingList[pos], WorkingList[pos + 1], t)); + } + } + else + { + DumpList.Add(Lerp(WorkingList[0], WorkingList[1], precision)); + } + WorkingList = new List(DumpList); + DumpList = new List(); + } + gr.FillEllipse(new SolidBrush(GetRandomColor()), new Rectangle(WorkingList[0].X - width / 2, WorkingList[0].Y - width / 2, width, width)); + } } } - private List PostProcessing(List Drawing) + private Point Lerp(Point start, Point end, float t) { - List NewDrawing = new List(); - Point previous = new Point(-1, -1); - //Implementing a post processing - foreach (Point point in Drawing) - { - if (previous != new Point(-1, -1)) - { - NewDrawing.Add(new Point((previous.X + point.X)/2,(previous.Y + point.Y)/2)); - previous = point; - } - else - { - previous = point; - } - NewDrawing.Add(point); - } - return NewDrawing; + 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); + Point result = new Point(resultX, resultY); + return result; + } + private Bitmap PostProcessing(List Drawing,Bitmap bmp,int width) + { + Graphics gr = Graphics.FromImage(bmp); + ContinuousBezierGenerator(gr, Drawing,width); + return bmp; } public List GetLastColors(int colorNumber) { diff --git a/Paint_2/Sketch.cs b/Paint_2/Sketch.cs index 9de5019..4417a95 100644 --- a/Paint_2/Sketch.cs +++ b/Paint_2/Sketch.cs @@ -92,9 +92,10 @@ namespace Paint_2 { CurrentTool.Start(CurrentTool.Color, width); } - public void StopDrawing() + public Bitmap StopDrawing(Bitmap bmp) { - CurrentTool.Stop(); + bmp = CurrentTool.Stop(bmp); + return bmp; } public void AddDrawingPoint(Point location) {