Added a continuous Bezier generator

This commit is contained in:
2022-06-03 15:07:15 +02:00
parent 8cdfc0acfa
commit fbfec9eccf
6 changed files with 110 additions and 33 deletions

View File

@@ -95,7 +95,7 @@ namespace Paint_2
int yDiff = end.Y - start.Y; int yDiff = end.Y - start.Y;
int resultX = start.X + (int)(t * xDiff); int resultX = start.X + (int)(t * xDiff);
int resultY = start.Y + (int)(t * yDiff); int resultY = start.Y + (int)(t * yDiff);
Point result = new Point(resultX,resultY); Point result = new Point(resultX, resultY);
return result; return result;
} }
public void Paint(Bitmap canvas) public void Paint(Bitmap canvas)
@@ -117,21 +117,56 @@ namespace Paint_2
for (int i = 1; i <= points.Count; i++) 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)); 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 p1 = points[i - 4];
Point p2 = points[i - 3]; Point p2 = points[i - 3];
Point p3 = points[i - 2]; Point p3 = points[i - 2];
Point p4 = points[i - 1]; 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<Point> points, int i)
{
if (points.Count >= 4 && points.Count % 2 == 0)
{
float precision = 0.01f;
for (float t = 0; t <= 1; t += precision)
{
List<Point> DumpList = new List<Point>();
List<Point> WorkingList = new List<Point>(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<Point>(DumpList);
DumpList = new List<Point>();
}
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 - 4], Widths[i - 4]), p1, p2);
gr.DrawLine(new Pen(Colors[i - 3], Widths[i - 3]), p2, p3); gr.DrawLine(new Pen(Colors[i - 3], Widths[i - 3]), p2, p3);
@@ -166,7 +201,7 @@ namespace Paint_2
private Color GetRandomColor() private Color GetRandomColor()
{ {
Random rnd = new Random(); 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) public void Start(Color color, int width)
{ {
@@ -224,5 +259,10 @@ namespace Paint_2
WidthsRedo.RemoveAt(WidthsRedo.Count - 1); WidthsRedo.RemoveAt(WidthsRedo.Count - 1);
} }
} }
public Bitmap Stop(Bitmap bmp)
{
return bmp;
}
} }
} }

View File

@@ -179,5 +179,10 @@ namespace Paint_2
WidthsRedo.RemoveAt(WidthsRedo.Count - 1); WidthsRedo.RemoveAt(WidthsRedo.Count - 1);
} }
} }
public Bitmap Stop(Bitmap bmp)
{
return bmp;
}
} }
} }

View File

@@ -61,7 +61,7 @@ namespace Paint_2
{ {
sketch.AddDrawingPoint(MousePositionToCanvasPosition()); sketch.AddDrawingPoint(MousePositionToCanvasPosition());
drawing = false; drawing = false;
sketch.StopDrawing(); canvas.Image = sketch.StopDrawing((Bitmap)canvas.Image);
RefreshUi(); RefreshUi();
} }

View File

@@ -25,7 +25,7 @@ namespace Paint_2
string Name { get; set; } string Name { get; set; }
void Start(Color color, int width); void Start(Color color, int width);
void Stop(); Bitmap Stop(Bitmap bmp);
void Add(Point point); void Add(Point point);
void Undo(); void Undo();
void Redo(); void Redo();

View File

@@ -90,33 +90,64 @@ namespace Paint_2
Widths = new List<int>(); Widths = new List<int>();
WidthsRedo = new List<int>(); WidthsRedo = new List<int>();
} }
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<Point> Drawing = Drawings[Drawings.Count - 1]; List<Point> Drawing = Drawings[i];
Drawings[Drawings.Count - 1] = PostProcessing(Drawing); //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<Point> points, int width)
{
if (points.Count >= 4 && points.Count % 2 == 0)
{
float precision = 0.01f;
for (float t = 0; t <= 1; t += precision)
{
List<Point> DumpList = new List<Point>();
List<Point> WorkingList = new List<Point>(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<Point>(DumpList);
DumpList = new List<Point>();
}
gr.FillEllipse(new SolidBrush(GetRandomColor()), new Rectangle(WorkingList[0].X - width / 2, WorkingList[0].Y - width / 2, width, width));
}
} }
} }
private List<Point> PostProcessing(List<Point> Drawing) private Point Lerp(Point start, Point end, float t)
{ {
List<Point> NewDrawing = new List<Point>(); int xDiff = end.X - start.X;
Point previous = new Point(-1, -1); int yDiff = end.Y - start.Y;
//Implementing a post processing int resultX = start.X + (int)(t * xDiff);
foreach (Point point in Drawing) int resultY = start.Y + (int)(t * yDiff);
{ Point result = new Point(resultX, resultY);
if (previous != new Point(-1, -1)) return result;
{ }
NewDrawing.Add(new Point((previous.X + point.X)/2,(previous.Y + point.Y)/2)); private Bitmap PostProcessing(List<Point> Drawing,Bitmap bmp,int width)
previous = point; {
} Graphics gr = Graphics.FromImage(bmp);
else ContinuousBezierGenerator(gr, Drawing,width);
{ return bmp;
previous = point;
}
NewDrawing.Add(point);
}
return NewDrawing;
} }
public List<Color> GetLastColors(int colorNumber) public List<Color> GetLastColors(int colorNumber)
{ {

View File

@@ -92,9 +92,10 @@ namespace Paint_2
{ {
CurrentTool.Start(CurrentTool.Color, width); 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) public void AddDrawingPoint(Point location)
{ {