How to calculate SVG path area C #

If anyone can help with calculating the SVG path region, I would be very grateful.

I have a function to get the total length, which works very well.

I saw a javascript method that converts a path to a polygon:

http://phrogz.net/SVG/convert_path_to_polygon.xhtml

Converting this to C # would be a good way, since I have a function that gets the polygon area with sufficient accuracy.

Hi

If there is any doubt about what is being asked here ...

If you know anything about the SVG file format, you will find out that SVG-Path shapes are defined by a coordinate bunch as follows:

d="M195.303,64.357 c6.57-6.684,11.9-15.514,10.057-25.154 c-3.689-19.298-27.854-13.204-27.309-0.549 c-0.545-12.655-23.619-18.749-27.309,0.549 c-1.844,9.651,3.502,18.401,10.082,25.087 c1.697-4.813,5.713-8.03,11.482-9.451 c-2.797,1.531-3.809,4.396-3.809,7.812 c0,4.983,4.58,8.4,9.553,8.4s9.553-3.417,9.553-8.4c0-3.416-1.012-6.28-3.807-7.812C189.584,56.263,193.613,59.52,195.303,64.357 z" 

c and c determine the bezier curves

(would post image)

I ask how to calculate the area for such a shape using coordinate / curve points.

A solution using IronPython and the Inkscape function works very well.

+6
source share
2 answers

I couldn't work out the math myself, so in the end I used IronPython to execute the python module from Inkscape, which measures any path accurately enough for what I use it for.

I will tell the details. If someone is looking for the same thing, but it is really quite simple and does not require much editing of Python files. You need to use .net framework v4.0.

0
source

allows you to consider the point of a closed polygon (x0, y0) ... (xn, yn), you can calculate the area by a simple loop through the point using this formula

 area += yi+y(i+1) * (x(i+1)-xi)/2 

Just for clarification with x (i + 1) and y (i + 1) I mean the next point in the sequence. The loop should start at 0 and stop at the n-1 vertex (so the next point is always valid), and when the last point is reached, consider the ptn-pt0 area should be taken as abs, otherwise you can use the sign to check if the polygon is by clockwise or counterclockwise. I'm just adding a drawing to explain how it works, so you can expand it to handle different types of curves. Let's look at this picture: enter image description here

The idea is to calculate the area of ​​the green polygon from the algebraic sum, the area of ​​each trapezoid related to the edge of the polygon and the x axis, in this case the trapezoid is:

 x0,y0 x1,y1 x1,0 x0,0 x1,y1 x2,y2 x2,0 x1,0 (-)x2,y2 x3,y3 x3,0 x2,0 (-)x3,y3 x4,y4 x4,0 x3,0 (-)x4,y4 x0,y0 x0,0 x4,0 

the first two are positive, the last three are negative, and, as you see, the difference is the green part, that is, the necessary area. I'm not so good at drawing, but I’ll try to understand that the result is due to an overlapping trapezoid that works in an additive and subtractive way. Even if I showed a convex polygon in the drawing, the algo also works on concave polygons. In case the edge is not a segment as the first solution, you need to convert it to a list of segments using the parametric equation on the curve and select it, you will get a decent approxymation. The best solution is to consider the parametric function of the non-linear segment and integrate it along the X axis (calculate the area between the curve and the X axis) and sum it when I sum the area of ​​the linear segments. This has some math involved, but you can find the help you need here . You just need to use the correct segment equation.

+7
source

Source: https://habr.com/ru/post/912240/


All Articles