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

@@ -117,17 +117,52 @@ 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)
{
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);
}
*/
ContinuousBezierGenerator(gr, points, 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]));
}
}
}
@@ -224,5 +259,10 @@ namespace Paint_2
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);
}
}
public Bitmap Stop(Bitmap bmp)
{
return bmp;
}
}
}

View File

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

View File

@@ -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();

View File

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

View File

@@ -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)
{