I have the following problem.
I would like to calculate area of polygon, for example
http://s24.postimg.org/cm8pw6fdh/malamapa.jpg[
^]
Now I have only example application which calculate polygon area, but without pictureBox in background.
I would be very grateful if someone could modify the project accordingly.
http://www.csharphelper.com/examples/howto_polygon_geometry.zip[
^]
Or
How do I modify the code below?
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace howto_polygon_geometry
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int PT_RAD = 2;
const int PT_WID = PT_RAD * 2 + 1;
private List<PointF> m_Points = new List<PointF>();
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(this.BackColor);
if (m_Points.Count >= 3)
{
e.Graphics.DrawPolygon(Pens.Blue, m_Points.ToArray());
}
else if (m_Points.Count == 2)
{
e.Graphics.DrawLines(Pens.Blue, m_Points.ToArray());
}
if (m_Points.Count > 0)
{
foreach (PointF pt in m_Points)
{
e.Graphics.FillRectangle(Brushes.White, pt.X - PT_RAD, pt.Y - PT_RAD, PT_WID, PT_WID);
e.Graphics.DrawRectangle(Pens.Black, pt.X - PT_RAD, pt.Y - PT_RAD, PT_WID, PT_WID);
}
}
EnableMenus();
}
private void EnableMenus()
{
bool enabled = (m_Points.Count >= 3);
mnuTestsConvex.Enabled = enabled;
mnuTestsPointInPolygon.Enabled = enabled;
mnuTestsArea.Enabled = enabled;
mnuTestsCentroid.Enabled = enabled;
mnuTestsOrientation.Enabled = enabled;
mnuTestsReverse.Enabled = enabled;
mnuTestsTriangulate.Enabled = enabled;
mnuTestsBoundingRectangle.Enabled = enabled;
}
private void mnuTestsClear_Click(object sender, EventArgs e)
{
m_Points = new List<PointF>();
EnableMenus();
this.Invalidate();
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
m_Points.Add(new PointF(e.X, e.Y));
this.Invalidate();
}
private void mnuTestsConvex_Click(object sender, EventArgs e)
{
Polygon pgon = new Polygon(m_Points.ToArray());
if (pgon.PolygonIsConvex())
{
MessageBox.Show("The polygon is convex", "Convex",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("The polygon is not convex", "Not Convex",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void mnuTestsPointInPolygon_Click(object sender, EventArgs e)
{
Point pt = Cursor.Position;
pt = this.PointToClient(pt);
Rectangle rect = new Rectangle(pt.X - 3, pt.Y - 3, 7, 7);
using (Graphics gr = this.CreateGraphics())
{
Polygon pgon = new Polygon(m_Points.ToArray());
if (pgon.PointInPolygon(pt.X, pt.Y))
{
gr.FillEllipse(Brushes.Lime, rect);
}
else
{
gr.FillEllipse(Brushes.Red, rect);
}
gr.DrawEllipse(Pens.Black, rect);
}
}
private void mnuTestsArea_Click(object sender, EventArgs e)
{
Polygon pgon = new Polygon(m_Points.ToArray());
MessageBox.Show("Area: " + pgon.PolygonArea().ToString(), "Area",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void mnuTestsCentroid_Click(object sender, EventArgs e)
{
Polygon pgon = new Polygon(m_Points.ToArray());
PointF pt = pgon.FindCentroid();
Rectangle rect = new Rectangle((int)pt.X - 3, (int)pt.Y - 3, 7, 7);
using (Graphics gr = this.CreateGraphics())
{
gr.FillEllipse(Brushes.Yellow, rect);
gr.DrawEllipse(Pens.Black, rect);
}
}
private void mnuTestsOrientation_Click(object sender, EventArgs e)
{
Polygon pgon = new Polygon(m_Points.ToArray());
if (pgon.PolygonIsOrientedClockwise())
{
MessageBox.Show("Clockwise", "Clockwise",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Counter clockwise", "Counter Clockwise",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void mnuTestsReverse_Click(object sender, EventArgs e)
{
m_Points.Reverse();
}
private void mnuTestsTriangulate_Click(object sender, EventArgs e)
{
Polygon pgon = new Polygon(m_Points.ToArray());
List<Triangle> triangles = pgon.Triangulate();
using (Graphics gr = this.CreateGraphics())
{
foreach (Triangle tri in triangles)
{
gr.DrawPolygon(Pens.Red, tri.Points);
}
if (m_Points.Count >= 3)
{
gr.DrawPolygon(Pens.Blue, m_Points.ToArray());
}
}
}
private void mnuTestsBoundingRectangle_Click(object sender, EventArgs e)
{
Polygon pgon = new Polygon(m_Points.ToArray());
if (pgon.PolygonIsOrientedClockwise())
{
Array.Reverse(pgon.Points);
}
PointF[] pts = pgon.FindSmallestBoundingRectangle();
using (Graphics gr = this.CreateGraphics())
{
gr.DrawPolygon(Pens.Orange, pts);
}
}
}
}