如何使用pictureBox计算多边形区域 [英] How calculate polygon area on using pictureBox

查看:104
本文介绍了如何使用pictureBox计算多边形区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。



我想计算多边形的面积,例如

http://s24.postimg.org/cm8pw6fdh/malamapa.jpg [ ^ ]



现在我只有一个计算多边形的示例应用程序区域,但背景中没有pictureBox。



如果有人可以相应地修改项目,我将非常感激。

http://www.csharphelper.com/examples/howto_polygon_geometry.zip [ ^ ]





如何修改以下代码?



Form1.cs

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

        // Draw the polygon.
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);

            // Draw the lines.
            if (m_Points.Count >= 3)
            {
                // Draw the polygon.
                e.Graphics.DrawPolygon(Pens.Blue, m_Points.ToArray());
            }
            else if (m_Points.Count == 2)
            {
                // Draw the line.
                e.Graphics.DrawLines(Pens.Blue, m_Points.ToArray());
            }

            // Draw the points.
            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);
                }
            }

            // Enable menu items appropriately.
            EnableMenus();
        }

        // Enable menu items appropriately.
        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;
        }

        // Remove all points.
        private void mnuTestsClear_Click(object sender, EventArgs e)
        {
            m_Points = new List<PointF>();
            EnableMenus();
            this.Invalidate();
        }

        // Save a new point.
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            m_Points.Add(new PointF(e.X, e.Y));

            // Redraw.
            this.Invalidate();
        }

        // See if the polygon is convex.
        private void mnuTestsConvex_Click(object sender, EventArgs e)
        {
            // Make a Polygon.
            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);
            }
        }

        // See if the mouse's current position is inside the polygon.
        private void mnuTestsPointInPolygon_Click(object sender, EventArgs e)
        {
            // Get the current mouse position.
            Point pt = Cursor.Position;

            // Convert into form coordinates.
            pt = this.PointToClient(pt);

            Rectangle rect = new Rectangle(pt.X - 3, pt.Y - 3, 7, 7);
            using (Graphics gr = this.CreateGraphics())
            {
                // Make a Polygon.
                Polygon pgon = new Polygon(m_Points.ToArray());

                // See if the point is in the polygon.
                if (pgon.PointInPolygon(pt.X, pt.Y))
                {
                    gr.FillEllipse(Brushes.Lime, rect);
                }
                else
                {
                    gr.FillEllipse(Brushes.Red, rect);
                }
                gr.DrawEllipse(Pens.Black, rect);
            }
        }

        // Find the polygon's area.
        private void mnuTestsArea_Click(object sender, EventArgs e)
        {
            // Make a Polygon.
            Polygon pgon = new Polygon(m_Points.ToArray());

            MessageBox.Show("Area: " + pgon.PolygonArea().ToString(), "Area",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Display the centroid.
        private void mnuTestsCentroid_Click(object sender, EventArgs e)
        {
            // Make a Polygon.
            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);
            }
        }

        // See if the polygon is oriented clockwise or counter clockwise.
        private void mnuTestsOrientation_Click(object sender, EventArgs e)
        {
            // Make a Polygon.
            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);
            }
        }

        // Reverse the polygon's orientation.
        private void mnuTestsReverse_Click(object sender, EventArgs e)
        {
            m_Points.Reverse();
        }

        // Triangulate the polygon.
        private void mnuTestsTriangulate_Click(object sender, EventArgs e)
        {
            // Make a Polygon.
            Polygon pgon = new Polygon(m_Points.ToArray());

            // Triangulate.
            List<Triangle> triangles = pgon.Triangulate();

            // Draw the triangles.
            using (Graphics gr = this.CreateGraphics())
            {
                foreach (Triangle tri in triangles)
                {
                    gr.DrawPolygon(Pens.Red, tri.Points);
                }

                // Redraw the polygon.
                if (m_Points.Count >= 3)
                {
                    // Draw the polygon.
                    gr.DrawPolygon(Pens.Blue, m_Points.ToArray());
                }
            }
        }

        // Find the polygon's bounding rectangle.
        private void mnuTestsBoundingRectangle_Click(object sender, EventArgs e)
        {
            // Make a Polygon.
            Polygon pgon = new Polygon(m_Points.ToArray());

            // Make sure it's oriented counter-clockwise.
            if (pgon.PolygonIsOrientedClockwise())
            {
                Array.Reverse(pgon.Points);
            }

            // Find the polygon's bounding rectangle.
            PointF[] pts = pgon.FindSmallestBoundingRectangle();

            // Draw it.
            using (Graphics gr = this.CreateGraphics())
            {
                gr.DrawPolygon(Pens.Orange, pts);
            }
        }
    }
}

推荐答案

所以,你找到了一些东西做了你想要的一些,但不是试图自己理解它,以便你可以进行所需的改变,你宁愿发布你在这里找到的代码并让我们为你做。



它不是那样的工作。

我们不为你工作。

如果你想有人写你的代码,你必须支付 - 我建议你去VWorker.com并在那里问。



但要注意:你得到的是你付出的代价。支付花生,买猴子





我强烈建议你重新考虑如何处理任务 - 因为在某些时候你会在外面广阔的世界,这种行为通常不受欢迎,也无法提升你的职业前景。
So, you have found something that does some of what you want, but rather than try to understand it yourself so you can make the changes you need, you would rather just post the code you found here and get us to do it for you.

It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to VWorker.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys


I would strongly suggest that you rethink how you approach tasks - because at some point you will be out in the big wide world where that kind of behavior is generally frowned upon and will not enhance your career prospects.


这篇关于如何使用pictureBox计算多边形区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆