Drawing Polygon C# OOP can't find Array - c#

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.

Related

How to make the previous figure on the box PictureBox not disappear?

This is the code that draws the shapes in the picture by coordinates.
I need to make it so that the previous forms are also saved. How can I do this?
List<Point> shadePoints = new List<Point>();
shadePoints.Add(new Point(0, pictureBox1.ClientSize.Height));
shadePoints.Add(new Point(pictureBox1.ClientSize.Width, 0));
shadePoints.Add(new Point(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height));
e.Graphics.FillPolygon(Brushes.LightGray, shadePoints.ToArray());
// scale the drawing
using (Matrix m = new Matrix())
{
m.Scale(1, 1);
e.Graphics.Transform = m;
List<Point> polyPoints = new List<Point>();
polyPoints.Add(new Point(508, 1231));
polyPoints.Add(new Point(509, 123));
polyPoints.Add(new Point(509, 124));
polyPoints.Add(new Point(508, 124));
using (SolidBrush br = new SolidBrush(Color.FromArgb(100, Color.Yellow)))
{
e.Graphics.FillPolygon(br, polyPoints.ToArray());
}
e.Graphics.DrawPolygon(Pens.DarkBlue, polyPoints.ToArray());
foreach (Point p in polyPoints)
{
e.Graphics.FillEllipse(Brushes.Red,
new Rectangle(p.X - 2, p.Y - 2, 4, 4));
}
}

Remove outliers from a list of points in C#

Problem
I have a list of points in C# that I draw on a panel on a Windows Forms object. The lists variables are two listA and listB. The points in listB are the points in listA except that they have gone through some transformation to deform it to resemble the shape formed by points in listA and then added some outliers to make them look different. If you can try these on your visual studio then this is the code...
class Form1 : Form
{
//declare the list to hold points for
//shapes
List<Point> listA = new List<Point>();
List<Point> listB = new List<Point>();
//this methods transforms,applies outliers and draws the shapes on panel1
private void button1_click(EventArgs e, object sender)
{
//clear the lists for initializing
listA.Clear();
listB.Clear();
Point p1a = new Point(20, 30);
Point p2a = new Point(120, 50);
Point p3a = new Point(160, 80);
Point p4a = new Point(180, 300);
Point p5a = new Point(100, 220);
Point p6a = new Point(50, 280);
Point p7a = new Point(20, 140);
//Hold the Points in an array
Point[] mypoints = new Point[] { p1a, p2a, p3a, p4a, p5a, p6a, p7a };
//add the points to the List with one call
listA.AddRange(mypoints);
//define a new Transformation
//that will translate shapeA to have a slightly different imageB
Transformation t2 = new Transformation();
t2.A = 1.05; t2.B = 0.05; t2.T1 = 15; t2.T2 = 22;
//assign the new translated points to listB
listB = applytransformation(t2, listA);
//Add outliers to listb by manipulating the values in the list
Shape2[2] = new Point(Shape2[2].X + 10, Shape2[2].Y + 3);
//create a new instance of pen
//for drawing imageA in blue
Pen penner = new Pen(Brushes.Blue, 3);
//Create a new instance of pen for
//drawing imageB in red
Pen mypen = new Pen(Brushes.Red, 3);
//get the graphic context
Graphics g = panel1.CreateGraphics();
//draw both shapes
DisplayShape(listA, penner, g);
DisplayShape(listB, mypen, g);
}
//the method below does the transformation of imagea into imageb by manipulating the points and the transformation
List<Point> applytransformation(Transformation x, List<Point> shape)
{
List<Point> Tlist = new List<Point>();
foreach (Point c in shape) {
double xprime = x.A * c.X + x.B * c.Y + x.T1;
double yprime = x.B * c.X * -1 + x.A * c.Y + x.T2;
Point ptrans = new Point((int)xprime, (int)yprime);
Tlist.Add(ptrans);
}
//it returns the points that will be used to draw imageB
return Tlist;
}
//this method draws the points on the panel
void DisplayShape(List<Point> Shp, Pen pen, Graphics G)
{
Point? prevPoint = null;//nullable
foreach (Point pt in Shp) {
G.DrawEllipse(pen, new Rectangle(pt.X - 2, pt.Y - 2, 4, 4));
if (prevPoint != null) {
G.DrawLine(pen, (Point)prevPoint, pt);
}
prevPoint = pt;
}
G.DrawLine(pen, Shp[0], Shp[Shp.Count - 1]);
}
}
public class Transformation
{
public double A { get; set; }
public double B { get; set; }
public double T1 { get; set; }
public double T2 { get; set; }
}
Goal
I want to remove all the outliers in imageB so that it resembles imageA even if it won't be perfect. All methods or algorithms are welcome ie RANSAC,minimum cost function. I have tried to find an authoritative source online that can guide or help me achieve this in C# with zero success. The code I have provided is a minimum reproducible example that can be replicated on any visual studio IDE.Please help, Thank You for your time and contribution.
Expected Output
I added an image to make it clear the result I want
If you have many points forming a cloud of points where the line defining the shape goes through, then you can remove outliers. As an example see Removing outliers. But in this case, every point in the list seems to be a vertex of the shape. Removing a point will alter the shape considerably.
Can you explain what these shapes represent? hat should happen if you remove an outlier? Should it be replaced by another point?
While this is not an answer to your question, here is an improved and simplified version of the code:
List<Point> listA, listB; // Initialization not required.
private void button1_click(EventArgs e, object sender)
{
// Simplify initialization with collection and object initializers.
listA = new List<Point> {
new Point(20, 30), new Point(120, 50),
new Point(160, 80), new Point(180, 300),
new Point(100, 220), new Point(50, 280),
new Point(20, 140)
};
var t2 = new Transformation { A = 1.05, B = 0.05, T1 = 15, T2 = 22 };
listB = ApplyTransformation(t2, listA);
// Simplify shifting point.
Shape2[2] += new Size(10, 3);
// Invalidate panel and let Panel1_Paint draw it.
// Never create your own Graphics object.
panel1.Invalidate();
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
if (listA != null && listB != null) {
// Use predefined pens instead of creating brushes.
DisplayShape(listA, Pens.Blue, e.Graphics);
DisplayShape(listB, Pens.Red, e.Graphics);
}
}
List<Point> ApplyTransformation(Transformation x, List<Point> shape)
{
// Prevent list resizing by specifying initial size.
var transformedList = new List<Point>(shape.Count);
foreach (Point c in shape) {
double xprime = x.A * c.X + x.B * c.Y + x.T1;
double yprime = x.B * c.X * -1 + x.A * c.Y + x.T2;
transformedList.Add(new Point((int)xprime, (int)yprime));
}
return transformedList;
}
void DisplayShape(List<Point> shape, Pen pen, Graphics g)
{
// By using "for" instead of "foreach" we have indexes we can use to
// simplify closing the shape, since we always have a previous point.
for (int i = 0; i < shape.Count; i++) {
Point prevPoint = i > 0 ? shape[i - 1] : shape[shape.Count - 1];
Point pt = shape[i];
// No need to create a rectangle,
// there is an overload accepting location and size.
g.DrawEllipse(pen, pt.X - 2, pt.Y - 2, 4, 4);
g.DrawLine(pen, prevPoint, pt);
}
}
Since C# 8.0 and in .NET Core projects we can also write shape[^1] to get the last point instead of shape[shape.Count - 1].

How to get Bounds of New Region made with PointsF?

I am trying some simple collisions like this:
if (Object1.bounds.intersectWith(Object2.bounds){ }
Now I am defining a new region for each object using pointsf like this :
using (GraphicsPath gp = new GraphicsPath())
{
// Create points that define polygon.
PointF point1 = new PointF(this.Width/2, 0);
PointF point2 = new PointF(this.Width, this.Height/2);
PointF point3 = new PointF(this.Width / 2, this.Height);
PointF point4 = new PointF(0, this.Height/2);
PointF[] curvePoints =
{
point1,
point2,
point3,
point4,
};
gp.AddLines(curvePoints.ToArray());
this.Region = new Region(gp);
}
But, the intersectionWith statement is only detecting the rectangle bounds. I try a code similar to this but its not working, how can I get the new region boundaries (triangle) in order to make it work?
if (Object1.region.Bounds.IntersectsWith(Object2.region.Bounds))
(This doesn't work obviously);
Thanks in advance

using graphic to draw pentagon

Currently i'm trying to draw a pentagon with graphic but to no avil as i'm unable to get the points. So far I had tried it to draw out a triangle which I had succeeded , code is as below;
SolidBrush sb = new SolidBrush(painting);
PointF point1 = new PointF(25, 350);
PointF point2 = new PointF(450, 350);
PointF point3 = new PointF(225, 50);
PointF[] curvePoints = { point1, point2, point3 };
g.FillPolygon(sb, curvePoints);
paintstart = false;
(using triangle value as base)i thought of adding two more points (since pentagon have 5 sides) and my code for pentagon goes like this
SolidBrush sb = new SolidBrush(painting);
PointF point1 = new PointF(25, 350);
PointF point2 = new PointF(450, 350);
PointF point3 = new PointF(225, 50);
PointF point4 = new PointF(10, 150);
PointF point5 = new Point(475, 150);
PointF[] curvePoints = { point1, point2, point3, point4,point5};
g.FillPolygon(sb, curvePoints);
paintstart = false;
after added the 5th point, the whole shape figure goes distorted while 4th point remain alright.
Any idea why after adding the 5th point, the whole shape goes distorted? I add + 15 value to the x value of the pentagon base(450)and y axis remained the same.
Any help is appreciated thanks !
The order of the points matters when you have more than three points:
PointF[] curvePoints = { point1, point2, point5, point3, point4 };

Draw multi-point Lines in C# WPF

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);
}

Categories

Resources