208 lines
7.3 KiB
C#
208 lines
7.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
//[DEBUG]
|
|
using System.Windows.Forms;
|
|
|
|
namespace Paint_2
|
|
{
|
|
internal class BezierPencil : PaintTool
|
|
{
|
|
private List<List<Point>> _drawings;
|
|
private List<List<Point>> _drawingsRedo;
|
|
private List<Color> _colors;
|
|
private List<Color> _colorsRedo;
|
|
private List<int> _widths;
|
|
private List<int> _widthsRedo;
|
|
private Color _color;
|
|
private string _name;
|
|
private int _width;
|
|
|
|
public List<List<Point>> Drawings { get => _drawings; set => _drawings = value; }
|
|
public List<List<Point>> DrawingsRedo { get => _drawingsRedo; set => _drawingsRedo = value; }
|
|
public List<Color> Colors { get => _colors; set => _colors = value; }
|
|
public List<Color> ColorsRedo { get => _colorsRedo; set => _colorsRedo = value; }
|
|
public List<int> Widths { get => _widths; set => _widths = value; }
|
|
public List<int> WidthsRedo { get => _widthsRedo; set => _widthsRedo = value; }
|
|
public Color Color { get => _color; set => _color = value; }
|
|
public string Name { get => _name; set => _name = value; }
|
|
public int Width { get => _width; set => _width = value; }
|
|
|
|
public BezierPencil(string name)
|
|
{
|
|
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>();
|
|
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)
|
|
{
|
|
if (Drawings[Drawings.Count - 1].Count == 0)
|
|
{
|
|
Drawings[Drawings.Count - 1].Add(point);
|
|
}
|
|
}
|
|
|
|
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>();
|
|
}
|
|
|
|
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);
|
|
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)
|
|
{
|
|
Graphics gr = Graphics.FromImage(canvas);
|
|
int drawingCounter = 0;
|
|
Size pointSize;
|
|
|
|
List<Point> points = new List<Point>();
|
|
foreach (List<Point> drawing in Drawings)
|
|
{
|
|
if (drawing.Count > 0)
|
|
{
|
|
points.Add(drawing[0]);
|
|
}
|
|
}
|
|
if (points.Count > 0)
|
|
{
|
|
|
|
for (int i = 1; i <= points.Count; i++)
|
|
{
|
|
pointSize = new Size(Widths[i - 1], Widths[i - 1]);
|
|
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 == 0)
|
|
{
|
|
//Drawing the given sharp outline
|
|
Point p1 = points[i - 4];
|
|
Point p2 = points[i - 3];
|
|
Point p3 = points[i - 2];
|
|
Point p4 = points[i - 1];
|
|
|
|
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 - 2], Widths[i - 2]), p3, p4);
|
|
|
|
float t = 0.5f;
|
|
|
|
//Drawing
|
|
Point p1_2 = Lerp(p1, p2, t);
|
|
Point p2_2 = Lerp(p2, p3, t);
|
|
Point p3_2 = Lerp(p3, p4, t);
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
//Implement the bezier curve algorythm (if there are enough points)
|
|
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return Name;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|