In the String Art Add-In v1.0 we put together a class called cRegularPolygon which exposes a DrawSectionByBothCornerPoints() method to allow an end-user to draw a polygon section. This method works very well but it’s a bit cumbersome for the end-user to have to calculate the centre and end points of the axes so, for the next installment of the add-in, we’re going to add a new method which will allow them to provide a more convenient set of information to draw the section. Ideally we would have a single method name such as DrawSection() into which the end users could pass different parameter sets but, since VBA doesn’t support overloading, we have to create separate methods with different names. Here’s the full code for this version of the add-in, some release notes and examples.

Edit 11th December: I just noticed that when I submitted the post, WordPress parsed a number of characters in the code so that it would not compile. For example, " had been parsed as &quot;. I’ve fixed it now and while I was doing it I took the opportunity to refactor part of the code so that it’s slightly cleaner to use.

String Art Add-In v1.1 Downloadable Version

This version of the add-in can be downloaded from here. To use the add-in, follow the instructions I gave for the v1.0 release.

String Art Add-In v1.1 Code

Module: mInterface

Option Explicit
'these functions allow outside projects to create instances of
'this project's classes
Public Function CreateRegularPolygon() As cRegularPolygon
Set CreateRegularPolygon = New cRegularPolygon
End Function
Public Function CreatePoint() As cPoint
Set CreatePoint = New cPoint
End Function

Class Module: cPoint

Instancing: PublicNotCreatable

Option Explicit
Private psngX As Single
Private psngY As Single
Public Property Let X(ByVal sngValue As Single)
psngX = sngValue
End Property
Public Property Get X() As Single
X = psngX
End Property
Public Property Let Y(ByVal sngValue As Single)
psngY = sngValue
End Property
Public Property Get Y() As Single
Y = psngY
End Property

Class Module: cPoints

Option Explicit
Private pcolPoints As Collection
Public Sub Add(ByRef Point As cPoint)
pcolPoints.Add Point
End Sub
Public Function Count() As Long
Count = pcolPoints.Count
End Function
'be able to loop through the collection with For Each
Public Function NewEnum() As IUnknown
'Attribute NewEnum.VB_UserMemID = -4 has been set in text editor
Set NewEnum = pcolPoints.[_NewEnum]
End Function
'default member of the class
Public Function Item(ByVal lngIndex As Long) As cPoint
'Attribute Value.VB_UserMemId = 0 has been set in text editor
Set Item = pcolPoints(lngIndex)
End Function
Private Sub Class_Initialize()
Set pcolPoints = New Collection
End Sub

Release Notes

DrawSectionByAxisLength

The new DrawSectionByAxisLength() method allows users to draw the section by defining the coordinates of the centre point, the length (r) of the axes, the angle (Theta) between the axes and the angle (Rho) of the first axis clockwise from 12 o’clock. Theta and Rho must be specified in radians, not degrees.

Trigonometry is then used within the class to calculate the axis end point coordinates. The general formulae to calculate the x and y coordinates of the end points are:

EndX = CentreX + r * Sin(Angle)EndY = CentreY - r * Cos(Angle)

The reason the change in Y is subtracted is because Y coordinates increase from top to bottom on an Excel worksheet.

mMath

The VBA trigonometry functions work with radians rather than degrees. It’s been at least fifteen years since I last did any trigonometry and picturing angles in radians just doesn’t come naturally to me. I know there are 90 degrees in a right-angle, but it’s not instinctive for me to say that there are Pi/2 radians in a right-angle. I’m probably not the only person out there who thinks this way, so I figured that it would be useful to have some methods to help ‘degree orientated’ end-users work in radians.

Examples

I decided to have a go at moving a parabolic polygon section around an Excel sheet and, with a few carefully placed DoEvents commands, the output isn’t too shabby. I never had moving shapes in mind when I built the add-in so the implementation is a little clunky, but nonetheless works. If string art animation proves to be popular then I’ll implement it properly in a future release.