This project has moved. For the latest updates, please go here.

Get a Hatch area.

Nov 15, 2013 at 4:24 PM
Edited Jan 29, 2014 at 2:46 PM
Hey, guys!

I'm trying to get a area of the a Hatch element.
however, i'm not having success!

Can you help me?

Thanks!

Some one?
Jan 29, 2014 at 2:47 PM
Some one can help me?
Coordinator
Jan 29, 2014 at 6:31 PM
If the hatch is a single closed polygon made exclusively of lines, I have some code that I could share, the way to do it is through integration of every single line component. But this is far from all possibilities that a hatch entity could have. If you start to have multiples contours, you will have to decide which ones to add or subtract from the actual hatch area.

A hatch contour can also be made of circle arcs, ellipse arcs and splines edges, in these cases you will have to previously convert to a polygon, or try to integrate them by yourself.

I don't know if this might be good enough for your requirements.

Daniel
Sep 5, 2014 at 5:14 PM
Hi Daniel,

I also need to get Hatch area. Can you please share your sollution with me.

Thank you very much,

Pedro
Coordinator
Sep 6, 2014 at 6:19 PM
This is the code I was talking about, it is a very simple operation:
        private static double AreaOfPolygon(IList<Vector2> vertexes, out Vector2 centroid)
        {
            if (vertexes == null)
                throw new NullReferenceException("The vertexes list cannot be null.");
            if (vertexes.Count < 3)
                throw new ArgumentException("The number of vertexes must be greater than two.");

            double x0 = vertexes[vertexes.Count-1].X;
            double y0 = vertexes[vertexes.Count-1].Y;
            double x1 = vertexes[0].X;
            double y1 = vertexes[0].Y;

            double area = (x0 - x1) * (y0 + y1) * 0.5;
            double xb = (y1 - y0) * (x0 * x0 + x0 * x1 + x1 * x1) * 0.1666666666666666;
            double yb = (x0 - x1) * (y0 * y0 + y0 * y1 + y1 * y1) * 0.1666666666666666;

            for (int i = 1; i < vertexes.Count; i++)
            {
                x0 = vertexes[i - 1].X;
                y0 = vertexes[i - 1].Y;
                x1 = vertexes[i].X;
                y1 = vertexes[i].Y;

                area += (x0 - x1) * (y0 + y1) * 0.5;
                xb += (y1 - y0) * (x0 * x0 + x0 * x1 + x1 * x1) * 0.1666666666666666;
                yb += (x0 - x1) * (y0 * y0 + y0 * y1 + y1 * y1) * 0.1666666666666666;
            }

            centroid = new Vector2(xb / area, yb / area);
            return Math.Abs(area);
        }
Keep in mind what I wrote in my previous post. This code only calculates the area of a polygon given its vertexes, a Hatch entity is a lot more complicated than that. And if you do not need the centroid just delete the code that calculates it to avoid a few unnecessary operations.

Daniel