Added a utils class to greatly simplify the Paint Tools source code

This commit is contained in:
2022-06-03 16:40:00 +02:00
parent fbfec9eccf
commit edb119072c
5 changed files with 139 additions and 268 deletions

View File

@@ -13,6 +13,7 @@ namespace Paint_2
{
private List<List<Point>> _drawings;
private List<List<Point>> _drawingsRedo;
private PaintToolUtils Utils;
private List<Color> _colors;
private List<Color> _colorsRedo;
private List<int> _widths;
@@ -41,9 +42,7 @@ namespace Paint_2
WidthsRedo = new List<int>();
Name = name;
//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));
Utils = new PaintToolUtils(this);
}
public void Add(Point point)
{
@@ -55,48 +54,12 @@ namespace Paint_2
public void Clear()
{
Drawings = new List<List<Point>>();
DrawingsRedo = new List<List<Point>>();
Colors = new List<Color>();
ColorsRedo = new List<Color>();
Widths = new List<int>();
WidthsRedo = new List<int>();
Utils.StandartClear();
}
public List<Color> GetLastColors(int colorNumber)
{
List<Color> result = new List<Color>();
if (Colors.Count <= colorNumber)
{
//We need to fill with black color
for (int i = Colors.Count; i > 0; i--)
{
result.Add(Colors[(Colors.Count) - i]);
}
for (int i = colorNumber - Colors.Count; i > 0; i--)
{
result.Add(Color.FromArgb(0x00, 0x00, 0x00));
}
}
else
{
for (int i = colorNumber; i > 0; i--)
{
result.Add(Colors[(Colors.Count) - i]);
}
}
return result;
}
private Point Lerp(Point start, Point end, float t)
{
//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);
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;
return Utils.SandartGetLastColors(colorNumber);
}
public void Paint(Bitmap canvas)
{
@@ -120,16 +83,6 @@ namespace Paint_2
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);
}
}
@@ -150,14 +103,12 @@ namespace Paint_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));
DumpList.Add(Utils.Lerp(WorkingList[pos], WorkingList[pos + 1], t));
}
}
else
{
DumpList.Add(Lerp(WorkingList[0], WorkingList[1], precision));
DumpList.Add(Utils.Lerp(WorkingList[0], WorkingList[1], precision));
}
WorkingList = new List<Point>(DumpList);
DumpList = new List<Point>();
@@ -177,23 +128,21 @@ namespace Paint_2
for (float t = 0; t <= 1; t += precision)
{
//Quadratic Bezier Curve creator
//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);
Point p1_2 = Utils.Lerp(p1, p2, t);
Point p2_2 = Utils.Lerp(p2, p3, t);
Point p3_2 = Utils.Lerp(p3, p4, t);
//gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), p1_2, p2_2);
//gr.DrawLine(new Pen(GetRandomColor(), Widths[0]), 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);
Point p1_3 = Utils.Lerp(p1_2, p2_2, t);
Point p2_3 = Utils.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);
Point p1_4 = Utils.Lerp(p1_3, p2_3, t);
gr.FillEllipse(new SolidBrush(GetRandomColor()), new Rectangle(p1_4.X - Widths[0] / 2, p1_4.Y - Widths[0] / 2, Widths[0], Widths[0]));
}
@@ -205,18 +154,12 @@ namespace Paint_2
}
public void Start(Color color, int width)
{
Color = color;
Width = width;
List<Point> points = new List<Point>();
Drawings.Add(points);
Colors.Add(Color);
Widths.Add(Width);
Utils.StandartStart(color,width);
}
public void Stop()
{
//Implement the bezier curve algorythm (if there are enough points)
//Empty
}
public override string ToString()
@@ -226,38 +169,12 @@ namespace Paint_2
public void Undo()
{
int last = Drawings.Count - 1;
if (Drawings.Count > 1)
{
DrawingsRedo.Add(Drawings[last]);
Drawings.RemoveAt(Drawings.Count - 1);
ColorsRedo.Add(Colors[last]);
Colors.RemoveAt(Colors.Count - 1);
WidthsRedo.Add(Widths[last]);
Widths.RemoveAt(Widths.Count - 1);
}
else
{
DrawingsRedo.Add(Drawings[0]);
Drawings.Clear();
ColorsRedo.Add(Colors[0]);
Colors.Clear();
WidthsRedo.Add(Widths[0]);
Widths.Clear();
}
Utils.StandartUndo();
}
public void Redo()
{
if (DrawingsRedo.Count > 0 && WidthsRedo.Count > 0 && ColorsRedo.Count > 0)
{
Drawings.Add(DrawingsRedo[DrawingsRedo.Count - 1]);
DrawingsRedo.RemoveAt(DrawingsRedo.Count - 1);
Colors.Add(ColorsRedo[ColorsRedo.Count - 1]);
ColorsRedo.RemoveAt(ColorsRedo.Count - 1);
Widths.Add(WidthsRedo[WidthsRedo.Count - 1]);
WidthsRedo.RemoveAt(WidthsRedo.Count - 1);
}
Utils.StandartRedo();
}
public Bitmap Stop(Bitmap bmp)

View File

@@ -18,6 +18,7 @@ namespace Paint_2
private List<List<Point>> _drawings;
private List<List<Point>> _drawingsRedo;
private PaintToolUtils Utils;
private List<Color> _colors;
private List<Color> _colorsRedo;
private List<int> _widths;
@@ -45,6 +46,8 @@ namespace Paint_2
Widths = new List<int>();
WidthsRedo = new List<int>();
Name = name;
Utils = new PaintToolUtils(this);
}
public void Add(Point point)
{
@@ -70,21 +73,11 @@ namespace Paint_2
}
public void Start(Color color, int width)
{
Color = color;
Width = width;
List<Point> points = new List<Point>();
Drawings.Add(points);
Colors.Add(Color);
Widths.Add(Width);
Utils.StandartStart(color,width);
}
public void Clear()
{
Drawings = new List<List<Point>>();
DrawingsRedo = new List<List<Point>>();
Colors = new List<Color>();
ColorsRedo = new List<Color>();
Widths = new List<int>();
WidthsRedo = new List<int>();
Utils.StandartClear();
}
public void Stop()
{
@@ -117,27 +110,7 @@ namespace Paint_2
}
public List<Color> GetLastColors(int colorNumber)
{
List<Color> result = new List<Color>();
if (Colors.Count <= colorNumber)
{
//We need to fill with black color
for (int i = Colors.Count; i > 0; i--)
{
result.Add(Colors[(Colors.Count) - i]);
}
for (int i = colorNumber - Colors.Count; i > 0; i--)
{
result.Add(Color.FromArgb(0x00, 0x00, 0x00));
}
}
else
{
for (int i = colorNumber; i > 0; i--)
{
result.Add(Colors[(Colors.Count) - i]);
}
}
return result;
return Utils.SandartGetLastColors(colorNumber);
}
public override string ToString()
{
@@ -146,38 +119,12 @@ namespace Paint_2
public void Undo()
{
int last = Drawings.Count - 1;
if (Drawings.Count > 1)
{
DrawingsRedo.Add(Drawings[last]);
Drawings.RemoveAt(Drawings.Count - 1);
ColorsRedo.Add(Colors[last]);
Colors.RemoveAt(Colors.Count - 1);
WidthsRedo.Add(Widths[last]);
Widths.RemoveAt(Widths.Count - 1);
}
else
{
DrawingsRedo.Add(Drawings[0]);
Drawings.Clear();
ColorsRedo.Add(Colors[0]);
Colors.Clear();
WidthsRedo.Add(Widths[0]);
Widths.Clear();
}
Utils.StandartUndo();
}
public void Redo()
{
if (DrawingsRedo.Count > 0 && WidthsRedo.Count > 0 && ColorsRedo.Count > 0)
{
Drawings.Add(DrawingsRedo[DrawingsRedo.Count - 1]);
DrawingsRedo.RemoveAt(DrawingsRedo.Count - 1);
Colors.Add(ColorsRedo[ColorsRedo.Count - 1]);
ColorsRedo.RemoveAt(ColorsRedo.Count - 1);
Widths.Add(WidthsRedo[WidthsRedo.Count - 1]);
WidthsRedo.RemoveAt(WidthsRedo.Count - 1);
}
Utils.StandartRedo();
}
public Bitmap Stop(Bitmap bmp)

104
Paint_2/PaintToolUtils.cs Normal file
View File

@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Paint_2
{
internal class PaintToolUtils
{
private PaintTool Target;
public PaintToolUtils(PaintTool target)
{
Target = target;
}
public void StandartStart(Color color, int width)
{
Target.Color = color;
Target.Width = width;
List<Point> points = new List<Point>();
Target.Drawings.Add(points);
Target.Colors.Add(Target.Color);
Target.Widths.Add(Target.Width);
}
public void StandartClear()
{
Target.Drawings = new List<List<Point>>();
Target.DrawingsRedo = new List<List<Point>>();
Target.Colors = new List<Color>();
Target.ColorsRedo = new List<Color>();
Target.Widths = new List<int>();
Target.WidthsRedo = new List<int>();
}
public void StandartUndo()
{
int last = Target.Drawings.Count - 1;
if (Target.Drawings.Count > 1 && Target.Widths.Count > 1 && Target.Colors.Count > 1)
{
Target.DrawingsRedo.Add(Target.Drawings[last]);
Target.Drawings.RemoveAt(Target.Drawings.Count - 1);
Target.ColorsRedo.Add(Target.Colors[last]);
Target.Colors.RemoveAt(Target.Colors.Count - 1);
Target.WidthsRedo.Add(Target.Widths[last]);
Target.Widths.RemoveAt(Target.Widths.Count - 1);
}
else if (Target.Drawings.Count == 1 && Target.Widths.Count == 1 && Target.Colors.Count == 1)
{
Target.DrawingsRedo.Add(Target.Drawings[0]);
Target.Drawings.Clear();
Target.ColorsRedo.Add(Target.Colors[0]);
Target.Colors.Clear();
Target.WidthsRedo.Add(Target.Widths[0]);
Target.Widths.Clear();
}
}
public void StandartRedo()
{
if (Target.DrawingsRedo.Count > 0 && Target.WidthsRedo.Count > 0 && Target.ColorsRedo.Count > 0)
{
Target.Drawings.Add(Target.DrawingsRedo[Target.DrawingsRedo.Count - 1]);
Target.DrawingsRedo.RemoveAt(Target.DrawingsRedo.Count - 1);
Target.Colors.Add(Target.ColorsRedo[Target.ColorsRedo.Count - 1]);
Target.ColorsRedo.RemoveAt(Target.ColorsRedo.Count - 1);
Target.Widths.Add(Target.WidthsRedo[Target.WidthsRedo.Count - 1]);
Target.WidthsRedo.RemoveAt(Target.WidthsRedo.Count - 1);
}
}
public List<Color> SandartGetLastColors(int colorNumber)
{
List<Color> result = new List<Color>();
if (Target.Colors.Count <= colorNumber)
{
//We need to fill with black color
for (int i = Target.Colors.Count; i > 0; i--)
{
result.Add(Target.Colors[(Target.Colors.Count) - i]);
}
for (int i = colorNumber - Target.Colors.Count; i > 0; i--)
{
result.Add(Color.FromArgb(0x00, 0x00, 0x00));
}
}
else
{
for (int i = colorNumber; i > 0; i--)
{
result.Add(Target.Colors[(Target.Colors.Count) - i]);
}
}
return result;
}
public 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;
}
}
}

View File

@@ -55,6 +55,7 @@
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="PaintTool.cs" />
<Compile Include="PaintToolUtils.cs" />
<Compile Include="Pencil.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -18,6 +18,7 @@ namespace Paint_2
private List<List<Point>> _drawings;
private List<List<Point>> _drawingsRedo;
private PaintToolUtils Utils;
private List<Color> _colors;
private List<Color> _colorsRedo;
private List<int> _widths;
@@ -45,6 +46,9 @@ namespace Paint_2
Widths = new List<int>();
WidthsRedo = new List<int>();
Name = name;
Utils = new PaintToolUtils(this);
}
public void Add(Point point)
{
@@ -74,21 +78,11 @@ namespace Paint_2
}
public void Start(Color color, int width)
{
Color = color;
Width = width;
List<Point> points = new List<Point>();
Drawings.Add(points);
Colors.Add(Color);
Widths.Add(Width);
Utils.StandartStart(color,width);
}
public void Clear()
{
Drawings = new List<List<Point>>();
DrawingsRedo = new List<List<Point>>();
Colors = new List<Color>();
ColorsRedo = new List<Color>();
Widths = new List<int>();
WidthsRedo = new List<int>();
Utils.StandartClear();
}
public Bitmap Stop(Bitmap bmp)
{
@@ -99,79 +93,13 @@ namespace Paint_2
}
return bmp;
}
private Color GetRandomColor()
private Bitmap PostProcessing(List<Point> Drawing,Bitmap bmp,int width,Color color)
{
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 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)
{
List<Color> result = new List<Color>();
if (Colors.Count <= colorNumber)
{
//We need to fill with black color
for (int i = Colors.Count; i > 0; i--)
{
result.Add(Colors[(Colors.Count) - i]);
}
for (int i = colorNumber - Colors.Count; i > 0; i--)
{
result.Add(Color.FromArgb(0x00, 0x00, 0x00));
}
}
else
{
for (int i = colorNumber; i > 0; i--)
{
result.Add(Colors[(Colors.Count) - i]);
}
}
return result;
return Utils.SandartGetLastColors(colorNumber);
}
public override string ToString()
{
@@ -180,38 +108,12 @@ namespace Paint_2
public void Undo()
{
int last = Drawings.Count - 1;
if (Drawings.Count > 1)
{
DrawingsRedo.Add(Drawings[last]);
Drawings.RemoveAt(Drawings.Count - 1);
ColorsRedo.Add(Colors[last]);
Colors.RemoveAt(Colors.Count - 1);
WidthsRedo.Add(Widths[last]);
Widths.RemoveAt(Widths.Count - 1);
}
else
{
DrawingsRedo.Add(Drawings[0]);
Drawings.Clear();
ColorsRedo.Add(Colors[0]);
Colors.Clear();
WidthsRedo.Add(Widths[0]);
Widths.Clear();
}
Utils.StandartUndo();
}
public void Redo()
{
if (DrawingsRedo.Count > 0 && WidthsRedo.Count > 0 && ColorsRedo.Count > 0)
{
Drawings.Add(DrawingsRedo[DrawingsRedo.Count - 1]);
DrawingsRedo.RemoveAt(DrawingsRedo.Count - 1);
Colors.Add(ColorsRedo[ColorsRedo.Count - 1]);
ColorsRedo.RemoveAt(ColorsRedo.Count - 1);
Widths.Add(WidthsRedo[WidthsRedo.Count - 1]);
WidthsRedo.RemoveAt(WidthsRedo.Count - 1);
}
Utils.StandartRedo();
}
}
}