Previous Post – Drawing in Excel 7
The previous post presented VBA routines to plot drawings in Excel from a series of coordinates, a list of connected points, and a table of formatting information. The drawings were scaled to fit the selected plot area.
This post covers a modification of these routines that allows the scale and centre of the plot to be changed, with lines and polylines being trimmed where necessary to fit inside the plot area. This is not as straightforward as it might seem because trimming polylines will often change the number of line segments; for instance in the plot of a triangle shown below increasing the scale factor to 1.6 increases the number of sides of the plot shape from 3 to 7.
The trimming of lines and polylines is carried out by the function IPBox(Line1, Box), where Line1 is a list of XY coordinates and Box is a 2 x 2 array listing the bottom left and top right coordinates of the plot area, to the same scale as the shape to be trimmed.
The procedure is as follows:
- For each node find the sector containing the node.
- For each line find any intersection points with the limits of the plot box
- Step through the nodes until the first inside the plot box
- For this and each subsequent line segment:
- If both ends are inside the plot box then save the start coordinates
- If the line crosses the plot box limits then save the intersection point coordinates
- For line ends outside the plot box, project to the nearest line, or nearest corner
- For the last line segment close the shape with the first node or intersection point
- Delete duplicate points from the list of saved coordinates
- Plot the resulting shape.
Examples of the output from this routine are shown below.