The previous post looked at User Defined Functions (UDFs) for working with vectors, including examples of functions to find the area of polygons defined by coordinates. In this post the functions will be extended to find other section properties (such as first and second moment of area), and compared with the functions presented previously in Section Properties07.xlsb. The new functions (including full open source code) can be downloaded from: SecPropV.xlsb. The new function returns up to 14 values as an array function; refer to Using Array Formulas if you are not familiar with their use.
The code used in the new functions is shown below:
For i = 1 To Num1 - 1 j = i + 1 Vect1(i, 1) = Coords(i, 1) Vect1(i, 2) = Coords(i, 2) Vect2(i, 1) = Coords(j, 1) Vect2(i, 2) = Coords(j, 2) CrosspA(i) = Vect1(i, 1) * Vect2(i, 2) - Vect1(i, 2) * Vect2(i, 1) Next i k = 1 For i = 1 To Num1 - 1 Area = Area + CrosspA(i) Next i Area = -Area / 2
First Moment of Area (about X axis):
For i = 1 To Num1 - 1 AX = AX + CrosspA(i) * (Vect1(i, 2) + Vect2(i, 2)) Next i AX = -AX / 6
Second Moment of Area (about X axis):
For i = 1 To Num1 - 1 IX = IX + (Vect1(i, 2) ^ 2 + Vect2(i, 2) ^ 2 + Vect1(i, 2) * Vect2(i, 2)) * CrosspA(i) Next i IX = -IX / 12
The full list of properties returned by the function is:
The performance of the new function is compared below with the previous version in computing the section properties of a circle defined with 1 million points on the circumference:
It can be seen that the new function (red line) has very little advantage for the area function, but the calculation of first and second moment of area is substantially faster. For both functions the calculation of section properties relative to different axes is almost instantaneous.
Details of input are shown below:
Input XY coordinates must be listed in a 2 column range, with the last point being the same as the first. Points should be listed in a clockwise direction to return a positive area. As shown in the example, a hollow shape my be analysed by entering the outer boundary in the clockwise direction, then the inner boundary in the anti-clockwise direction, finally returning to the starting point on the outer boundary along the same line.
If not all the output values are required the number to be calculated may be specified in the optional NumOut argument. Alternatively, enter -1 list the symbols for each output value in sequence, or -2 for a description of each value.
Finally, if only the area is required, the algorithm taken from the Softsurfer site is considerably faster, at about 0.25 seconds for the 1 million segment circle. An example of the Area2D and Area3D functions is shown below:
Area = 0 For i = 2 To n - 1 j = i + 1 k = i - 1 Area = Area + PointVals(i, 1) * (PointVals(j, 2) - PointVals(k, 2)) Next i Area = Area + PointVals(i, 1) * (PointVals(1, 2) - PointVals(i - 1, 2)) Area = -Area / 2