Draw multi-point Lines in C# WPF - c#

Im new in C# WPF.
I want to create a Line in WPF C# with a Point array.
Like:
Point[] points =
{
new Point(3, 5),
new Point(1 , 40),
new Point(12, 30),
new Point(20, 2 )
};
Line myLine = new Line( points );
How can I do this?

if you want to draw it with Line, write a method, or you can use Polyline
public MainWindow()
{
InitializeComponent();
canvas.Children.Clear();
Point[] points = new Point[4]
{
new Point(0, 0),
new Point(300 , 300),
new Point(400, 500),
new Point(700, 100 )
};
DrawLine(points);
//DrawLine2(points);
}
private void DrawLine(Point[] points)
{
int i;
int count = points.Length;
for (i = 0; i < count - 1; i++)
{
Line myline = new Line();
myline.Stroke = Brushes.Red;
myline.X1 = points[i].X;
myline.Y1 = points[i].Y;
myline.X2 = points[i + 1].X;
myline.Y2 = points[i + 1].Y;
canvas.Children.Add(myline);
}
}
private void DrawLine2(Point[] points)
{
Polyline line = new Polyline();
PointCollection collection = new PointCollection();
foreach(Point p in points)
{
collection.Add(p);
}
line.Points = collection;
line.Stroke = new SolidColorBrush(Colors.Black);
line.StrokeThickness = 1;
canvas.Children.Add(line);
}

Related

DrawPolygon() erases the old polygon when drawing

I set the points and when the points of the same color form a square, I draw a polygon. But when a new square is formed, the old one disappears.
can you tell me how to make sure that when drawing a new polygon, the old one does not disappear?
in the checkpoint() function, I check whether there is a square of points of the same color and return e coordinates for drawing.
public partial class Form1 : Form
{
private Class1 Class1 = new Class1();
private CellState currentPlayer = CellState.Red;
public const int SIZE = 11;
public const int Icon_Size = 30;
public Form1()
{
InitializeComponent();
}
//ставит точки
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
var p = new Point((int)Math.Round(1f * e.X / Icon_Size), (int)Math.Round(1f * e.Y / Icon_Size));
if (Class1[p] == CellState.Empty)
{
Class1.SetPoint(p, currentPlayer);
currentPlayer = Class1.Inverse(currentPlayer);
Invalidate();
}
}
//рисуем
private void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.ScaleTransform(Icon_Size, Icon_Size);
//рисуем сеточку
using (var pen = new Pen(Color.Gainsboro, 0.1f))
{
for (int x = 1; x < SIZE; x++)
e.Graphics.DrawLine(pen, x, 1, x, SIZE - 1);
for (int y = 1; y < SIZE; y++)
e.Graphics.DrawLine(pen, 1, y, SIZE - 1, y);
}
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//рисуем точки
using (var brush = new SolidBrush(Color.White))
for (int x = 1; x < Form1.SIZE; x++)
for (int y = 1; y < Form1.SIZE; y++)
{
var p = new Point(x, y);
var cell = Class1[p];
if (cell != CellState.Empty)
{
brush.Color = StateToColor(cell);
e.Graphics.FillEllipse(brush, x - 0.2f, y - 0.2f, 0.4f, 0.4f);
}
}
using (var PenP = new Pen(Color.Black, 0.1f))
using (var brush = new SolidBrush(Color.White))
{
Class1.CheckPoint();
int i = Class1.CheckPoint()[0];
int j = Class1.CheckPoint()[1];
int cp = Class1.CheckPoint()[2];
if (cp == 1)
{
PenP.Color = Color.Red;
brush.Color = Color.IndianRed;
Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
e.Graphics.FillPolygon(brush, a);
e.Graphics.DrawPolygon(PenP, a);
}
if (cp == 2)
{
PenP.Color = Color.Blue;
brush.Color = Color.RoyalBlue;
Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
e.Graphics.FillPolygon(brush, a);
e.Graphics.DrawPolygon(PenP, a);
}
}
}
//условие смены цвета под ход игрока
Color StateToColor(CellState state, byte alpha = 255)
{
var res = state == CellState.Blue ? Color.Blue : Color.Red;
return Color.FromArgb(alpha, res);
}
}

C# UWP Render InkStrokes in InkCanvas Seperately

enter image description here
I have captured points data from bamboo slate,and converted them to Windows.UI.Input.Inking.InkStroke data.Then I put them in a InkPresenter.StrokeContainer rendered like this image above.Strokes sticked to each other,how can I seperate them?
This is my code below.
private void DataDisplay()
{
List<InkPoint> list = new List<InkPoint>();
List<InkStroke> strokes = new List<InkStroke>();
InkDrawingAttributes drawingAttributes1 = new InkDrawingAttributes();
drawingAttributes1.Color = Colors.Black;
drawingAttributes1.Size = new Size(1, 1);
InkStrokeBuilder builder = new InkStrokeBuilder();
builder.SetDefaultDrawingAttributes(drawingAttributes1);
inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes1);
inkCanvas.InkPresenter.IsInputEnabled = true;
foreach (var item in data.Stroke)
{
string[] strArray = item.Split(',');
for (int i = 9; i <= strArray.Length - 5; i += 5)
{
float x = float.Parse(strArray[i]) / 30;
float y = float.Parse(strArray[i + 1]) / 30;
float pressure = float.Parse(strArray[i + 2]) / 1000;
Point point = new Point(x, y);
InkPoint ip = new InkPoint(point, pressure);
list.Add(ip);
}
Matrix3x2 matrix3X2 = new Matrix3x2(1, 0, 0, 1, 0, 0);
InkStroke newStroke = builder.CreateStrokeFromInkPoints(list, matrix3X2);
strokes.Add(newStroke);
}
inkCanvas.InkPresenter.StrokeContainer.AddStroke(strokes);
}

Custom Calendar through Graphics & Paint Event

This is a little project for myself, just learning graphics for WinForms and so on.
The goal is to program my own Calendar control which will meet some very specific requirements.
Here's how I handle the Paint Event (very hacky atm):
private void XCalendar_Paint(object sender, PaintEventArgs e)
{
// Pen
var pen_border = new Pen(BorderColorGrid);
var pen_row_border = new Pen(BorderColorGridRow);
var pen_column_border = new Pen(BorderColorGridColumn);
//var grid_pen = new Pen(Color.Black);
var bez_pen = new Pen(Color.FromArgb(192, 241, 231));
var description_row_height = 30;
var description_column_width = 65;
// Calculate Full
int full_width = this.Width - 1 - description_column_width;
int full_height = this.Height - 1 - description_row_height;
if (this.Users.Count == 0)
{
var s = "Keine Benutzer gefunden.";
var f = new Font(new FontFamily("Trebuchet MS"), 12f);
var s_size = e.Graphics.MeasureString(s, f);
var p = new Point((int)(full_width / 2) - (int)(s_size.Width / 2) + 30, 30);
e.Graphics.DrawString(s, f, new SolidBrush(Color.FromArgb(180, 180, 180)), p);
return;
}
// Calculate Rows
int row_count = 0;
switch (_calendarView)
{
case CalendarViews.Day:
row_count = 1;
break;
case CalendarViews.Week:
row_count = 7;
break;
case CalendarViews.Month:
row_count = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
break;
}
int row_height = full_height / row_count;
// Calculate Columns
int col_count = Users.Count;
int col_width = full_width / col_count;
// Draw Description Column BackColor
var desc_col_rect = new Rectangle(new Point(1, description_row_height + 1), new Size(description_column_width - 1, full_height - 1));
e.Graphics.FillRectangle(new SolidBrush(bez_pen.Color), desc_col_rect);
// Draw Border
var rect = new Rectangle(new Point(0, 0), new Size(this.Width - 1, this.Height - 1));
e.Graphics.DrawRectangle(pen_border, rect);
var desc_font = new Font(new FontFamily("Trebuchet MS"), 9f);
var desc_brush = new SolidBrush(Color.Black);
// Draw Columns
var col_x = description_column_width;
for (int x = 0; x < col_count; x++)
{
var rect_col = new Rectangle(new Point(col_x, 1), new Size(col_width, description_row_height));
var user = Users[x];
var short_name = user.Shortname;
var desc_size = e.Graphics.MeasureString(short_name, desc_font);
e.Graphics.FillRectangle(new SolidBrush(user.Color), rect_col);
e.Graphics.DrawLine(pen_column_border, new Point(col_x, 0), new Point(col_x, full_height + description_row_height));
e.Graphics.DrawString(short_name, desc_font, desc_brush, new Point(col_x + (int)(col_width / 2 - desc_size.Width / 2), (int)(description_row_height / 2 - desc_size.Height / 2)));
col_x += col_width;
}
// Draw Rows
var row_y = description_row_height;
DateTime dt = this.StartDateTime == null ? DateTime.Now : StartDateTime;
switch (this._calendarView)
{
case CalendarViews.Week:
dt = DateTimeExtensions.StartOfWeek(dt, DayOfWeek.Monday);
break;
case CalendarViews.Month:
dt = new DateTime(dt.Year, dt.Month, 1);
break;
}
for (int x = 0; x < row_count; x++)
{
var rect_row = new Rectangle(new Point(1, row_y), new Size(description_column_width - 1, row_height));
var s = new DateTimeFormatInfo().GetShortestDayName(dt.DayOfWeek) + ", " + dt.ToString("dd.MM");
var cur_row_fill_color = Color.Transparent;
if (dt.Date == DateTime.Now.Date)
{
cur_row_fill_color = this.FillColorCurrentDateRow;
}
else if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
{
cur_row_fill_color = this.FillColorWeekendDays;
}
var s_size = e.Graphics.MeasureString(s, desc_font);
e.Graphics.FillRectangle(new SolidBrush(cur_row_fill_color), rect_row);
e.Graphics.DrawLine(pen_row_border, new Point(0, row_y), new Point(full_width + description_column_width, row_y));
e.Graphics.DrawString(s, desc_font, desc_brush, new Point((int)(description_column_width / 2 - s_size.Width / 2), row_y + 3));
dt = dt.AddDays(1);
row_y += row_height;
}
}
Which works absolutely fine (design-wise):
The calendar will handle multiple users:
Now of course, this is just a simple grid and some rectangles. Unfortunately I cannot use any event with rectangles since it is solely a painted component.
I was hoping to get some input here on how I could handle clickable calendar items (like appointments). Putting Panels or UserControls everywhere sounds pretty damn inefficient to me...? Any input will be apreciated!

Multiple Plots In Accord.NET Framework

I have a data set , and hypothesis function, but cant combine them into one, any advices?
i used
ScatterplotBox.Show("Training data",x,y);
ScatterplotBox.Show("Training data",area.ToArray(),h);
You can use ScatterplotView instead, but you're also going to need a Form to hold it. Take a look:
static void Main(string[] args)
{
Random r = new Random();
int max = 15;
double[] x = new double[max];
double[] y1 = new double[max];
double[] y2 = new double[max];
for (int i = 0; i < max; i++)
{
x[i] = i;
y1[i] = r.Next(0, 50);
y2[i] = r.Next(50, 100);
}
ScatterplotView spv = new ScatterplotView();
spv.Dock = DockStyle.Fill;
spv.LinesVisible = true;
spv.Graph.GraphPane.AddCurve("Curve 1", x, y1, Color.Red, SymbolType.Circle);
spv.Graph.GraphPane.AddCurve("Curve 2", x, y2, Color.Blue, SymbolType.Diamond);
spv.Graph.GraphPane.AxisChange();
Form f1 = new Form();
f1.Width = 600;
f1.Height = 400;
f1.Controls.Add(spv);
f1.ShowDialog();
Console.ReadLine();
}

Drawing Polygon C# OOP can't find Array

I am trying to draw an Polygon in my WFA but it can't find the "curvePoints"in my class which are definetly there
class Driehoek : Figuur
{
Pen blackPen = new Pen(Color.Black, 3);
public void driehoek(Point p)
{
//this.x = 120;
//this.y = 50;
//this.width = 100;
//this.height = 100;
Point point1 = new Point(100, 150);
Point point2 = new Point(150, 100);
Point point3 = new Point(200, 150);
Point[] curvePoints =
{
point1,
point2,
point3,
};
}
public override void Teken(Graphics g)
{
g.DrawPolygon(blackPen, curvePoints);
// Error here is: The name 'curvePoints' does not exist in the current context
}
}
Create a new Point[] in your class, right after you make the Pen:
class Driehoek
{
Pen blackPen = new Pen(Color.Black, 3);
Point[] curvePoints;
}
Then, modify your function slightly so that you assign an array to the existing array, instead of creating a new one:
public void driehoek(Point p)
{
//this.x = 120;
//this.y = 50;
//this.width = 100;
//this.height = 100;
Point point1 = new Point(100, 150);
Point point2 = new Point(150, 100);
Point point3 = new Point(200, 150);
//Changed Point[] curvePoints to just curvePoints
curvePoints =
{
point1,
point2,
point3,
};
}
which are definetly there
It's a lie! Your array is in another method (out of "Teken" scope), not in class.

Categories

Resources