This commit is contained in:
meelstorm
2025-10-15 13:13:47 +02:00
parent 97ac18eac6
commit dec036dbcf
16 changed files with 534 additions and 678 deletions

View File

@@ -15,95 +15,6 @@ using Point = System.Drawing.Point;
namespace Lindt.Candybox.Demo
{
public enum EditMode
{
Brush,
DrawLine,
RemoveLine,
MoveLine,
MoveVertices
}
public class VectorLine
{
public Point StartPoint { get; set; }
public Point EndPoint { get; set; }
public bool Selected { get; set; }
public VectorLine()
{
}
public VectorLine Scale(float sX, float sY)
{
return new VectorLine(new Point((int)(StartPoint.X * sX), (int)(StartPoint.Y * sY)),
new Point((int)(EndPoint.X * sX), (int)(EndPoint.Y * sY)));
}
public VectorLine(Point start, Point end)
{
StartPoint = start;
EndPoint = end;
Selected = false;
}
public bool IsPointOnLine(Point point, int tolerance = 10)
{
// Calculate distance from point to line segment
double A = point.X - StartPoint.X;
double B = point.Y - StartPoint.Y;
double C = EndPoint.X - StartPoint.X;
double D = EndPoint.Y - StartPoint.Y;
double dot = A * C + B * D;
double lenSq = C * C + D * D;
if (lenSq == 0) return Math.Sqrt(A * A + B * B) <= tolerance;
double param = dot / lenSq;
double xx, yy;
if (param < 0)
{
xx = StartPoint.X;
yy = StartPoint.Y;
}
else if (param > 1)
{
xx = EndPoint.X;
yy = EndPoint.Y;
}
else
{
xx = StartPoint.X + param * C;
yy = StartPoint.Y + param * D;
}
double dx = point.X - xx;
double dy = point.Y - yy;
return Math.Sqrt(dx * dx + dy * dy) <= tolerance;
}
public bool IsPointNearVertex(Point point, out bool isStartVertex, int tolerance = 15)
{
isStartVertex = false;
double distToStart = Math.Sqrt(Math.Pow(point.X - StartPoint.X, 2) + Math.Pow(point.Y - StartPoint.Y, 2));
double distToEnd = Math.Sqrt(Math.Pow(point.X - EndPoint.X, 2) + Math.Pow(point.Y - EndPoint.Y, 2));
if (distToStart <= tolerance)
{
isStartVertex = true;
return true;
}
if (distToEnd <= tolerance)
{
isStartVertex = false;
return true;
}
return false;
}
}
public partial class MaskControl : UserControl
{
private Bitmap _backBuffer;
@@ -282,12 +193,21 @@ namespace Lindt.Candybox.Demo
private Pen _vectorLinePen = new Pen(Color.DeepPink, 12);
private Brush _vertexBrush = new SolidBrush(Color.Orange);
protected override void OnPaint(PaintEventArgs e)
{
OnPaintInternal(e);
}
void OnPaintInternal(PaintEventArgs e)
{
var mat = Mask.ToMat().CvtColor(ColorConversionCodes.BGR2GRAY);
var contours = mat.FindContoursAsArray(RetrievalModes.External, ContourApproximationModes.ApproxNone);
LastContours = contours;
using (Graphics g = Graphics.FromImage(_backBuffer))
{
g.Clear(Color.Transparent);
@@ -307,11 +227,11 @@ namespace Lindt.Candybox.Demo
foreach (var line in _vectorLines)
{
// Convert coordinates back to display coordinates
var startDisplay = new Point(line.StartPoint.X , line.StartPoint.Y );
var endDisplay = new Point(line.EndPoint.X , line.EndPoint.Y );
var startDisplay = new Point(line.StartPoint.X, line.StartPoint.Y);
var endDisplay = new Point(line.EndPoint.X, line.EndPoint.Y);
g.DrawLine(_vectorLinePen, startDisplay, endDisplay);
// Draw vertices when in move vertices mode
if (_currentMode == EditMode.MoveVertices)
{
@@ -323,8 +243,8 @@ namespace Lindt.Candybox.Demo
// Draw temporary line when drawing
if (_currentMode == EditMode.DrawLine && _isDrawingLine)
{
var startDisplay = new Point(_lineStartPoint.X , _lineStartPoint.Y );
var endDisplay = new Point(_lastMouse.X , _lastMouse.Y );
var startDisplay = new Point(_lineStartPoint.X, _lineStartPoint.Y);
var endDisplay = new Point(_lastMouse.X, _lastMouse.Y);
using (var tempPen = new Pen(Color.DeepPink, 12) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dash })
{
g.DrawLine(tempPen, startDisplay, endDisplay);
@@ -334,11 +254,11 @@ namespace Lindt.Candybox.Demo
// Draw mouse cursor indicator for brush mode
if (_currentMode == EditMode.Brush)
{
var cursorDisplay = new Point(_lastMouse.X , _lastMouse.Y );
var cursorDisplay = new Point(_lastMouse.X, _lastMouse.Y);
g.DrawEllipse(Pens.Gray, cursorDisplay.X - 15, cursorDisplay.Y - 15, 30, 30);
}
}
e.Graphics.DrawImage(_backBuffer, 0, 0, Width, Height);
}
@@ -355,4 +275,115 @@ namespace Lindt.Candybox.Demo
base.OnResize(e);
}
}
public enum EditMode
{
Brush,
DrawLine,
RemoveLine,
MoveLine,
MoveVertices
}
public class VectorLine
{
public Point StartPoint { get; set; }
public Point EndPoint { get; set; }
public bool Selected { get; set; }
public VectorLine()
{
}
public VectorLine Scale(float sX, float sY)
{
return new VectorLine(new Point((int)(StartPoint.X * sX), (int)(StartPoint.Y * sY)),
new Point((int)(EndPoint.X * sX), (int)(EndPoint.Y * sY)));
}
public VectorLine Translate(int tX, int tY)
{
return new VectorLine(new Point(StartPoint.X + tX, StartPoint.Y + tY),
new Point(EndPoint.X + tX, EndPoint.Y + tY));
}
public VectorLine Translate(Point point)
{
return Translate(point.X, point.Y);
}
public VectorLine Sub(int tX, int tY)
{
return new VectorLine(new Point(StartPoint.X - tX, StartPoint.Y - tY),
new Point(EndPoint.X - tX, EndPoint.Y - tY));
}
public VectorLine Sub(Point point)
{
return Sub(point.X, point.Y);
}
public VectorLine(Point start, Point end)
{
StartPoint = start;
EndPoint = end;
Selected = false;
}
public bool IsPointOnLine(Point point, int tolerance = 10)
{
// Calculate distance from point to line segment
double A = point.X - StartPoint.X;
double B = point.Y - StartPoint.Y;
double C = EndPoint.X - StartPoint.X;
double D = EndPoint.Y - StartPoint.Y;
double dot = A * C + B * D;
double lenSq = C * C + D * D;
if (lenSq == 0) return Math.Sqrt(A * A + B * B) <= tolerance;
double param = dot / lenSq;
double xx, yy;
if (param < 0)
{
xx = StartPoint.X;
yy = StartPoint.Y;
}
else if (param > 1)
{
xx = EndPoint.X;
yy = EndPoint.Y;
}
else
{
xx = StartPoint.X + param * C;
yy = StartPoint.Y + param * D;
}
double dx = point.X - xx;
double dy = point.Y - yy;
return Math.Sqrt(dx * dx + dy * dy) <= tolerance;
}
public bool IsPointNearVertex(Point point, out bool isStartVertex, int tolerance = 15)
{
isStartVertex = false;
double distToStart = Math.Sqrt(Math.Pow(point.X - StartPoint.X, 2) + Math.Pow(point.Y - StartPoint.Y, 2));
double distToEnd = Math.Sqrt(Math.Pow(point.X - EndPoint.X, 2) + Math.Pow(point.Y - EndPoint.Y, 2));
if (distToStart <= tolerance)
{
isStartVertex = true;
return true;
}
if (distToEnd <= tolerance)
{
isStartVertex = false;
return true;
}
return false;
}
}
}