VideoPhoto

27 July, 2023

Shapes Based On Parametric Equations: Hypotrochoids

Hypotrochoids are curves generated by a point selected on a circle of smaller radius rolling around the inside of a fixed circle of larger radius, and that selected point can be chosen at some distance from the center of the smaller circle. These are then 3-parametric curves.

You don't need any special programs, like e.g. Mathematica, to create hypotrochoids. They can be generated in Excel quite easily by using VBA macro like the one provided at the end of this post.

Here are some examples of charts with generated hypocycloid curves, both single and combined double curves:




 


 

You can try to use the following macro to plot your own attractive decorative hypotrochoid graphics. Just copy it to one of the modules in your workbook sheets and experiment with different settings and parameters.

Sub CreateHypotrochoidChart()
'Generates hypotrochoid scatter chart object, assigns calculated X and Y values to the chart series.
'The ReDim statements are used to size the arrays for X and Y values based on the desired
'number of data points.

Dim i As Integer, numPoints As Integer
Dim a As Integer, b As Integer, c As Integer
Dim a1 As Integer, b1 As Integer, c1 As Integer
Dim chartObj As ChartObject
Dim oChart As Chart
Dim xValues() As Double, yValues() As Double
Dim xvValues() As Double, yvValues() As Double
Dim tit As String
'Define the number of data points and calculate X and Y values
numPoints = 5000     'Change as needed
ReDim xValues(1 To numPoints)
ReDim yValues(1 To numPoints)
ReDim xvValues(1 To numPoints)
ReDim yvValues(1 To numPoints)
'Set random parameters a, b, c, a1, b1, c1 - within predefined or your own limits
'Change the limits of parameters as needed

a = WorksheetFunction.RandBetween(1, 30): b = WorksheetFunction.RandBetween(1, 20): c = WorksheetFunction.RandBetween(1, 25)
a1 = WorksheetFunction.RandBetween(1, 30): b1 = WorksheetFunction.RandBetween(1, 20): c1 = WorksheetFunction.RandBetween(1, 25)
'To set and use your own parameters for the curves, remove the apostrophe in the next line
'a = 15: b = 7.5: c = 2: a1 = 8: b1 = 4: c1 = 1
'Calculate X and Y values using your equations
For i = 1 To numPoints Step 2   'First series
    xValues(i) = (a - b) * Cos(WorksheetFunction.Radians(i)) + c * Cos(WorksheetFunction.Radians(((a - b) / b) * i))
    yValues(i) = (a - b) * Sin(WorksheetFunction.Radians(i)) - c * Sin(WorksheetFunction.Radians(((a - b) / b) * i))
Next i
For i = 1 To numPoints Step 2   'Second series
    xvValues(i) = (a1 - b1) * Cos(WorksheetFunction.Radians(i)) + c1 * Cos(WorksheetFunction.Radians(((a1 - b1) / b1) * i))
    yvValues(i) = (a1 - b1) * Sin(WorksheetFunction.Radians(i)) - c1 * Sin(WorksheetFunction.Radians(((a1 - b1) / b1) * i))
Next i
'Create a new chart object
Set chartObj = ActiveSheet.ChartObjects.Add(Top:=10, Left:=100, Width:=600, Height:=600)
Set oChart = chartObj.Chart
oChart.ChartType = xlXYScatter      'Set the chart type to scatter chart
'Set X and Y values for the chart series

    chartObj.Chart.SeriesCollection.NewSeries
    chartObj.Chart.SeriesCollection(1).Values = yValues
    chartObj.Chart.SeriesCollection(1).xValues = xValues
    chartObj.Chart.SeriesCollection.NewSeries
    chartObj.Chart.SeriesCollection(2).Values = yvValues
    chartObj.Chart.SeriesCollection(2).xValues = xvValues
'The code below formats the chart and the plot area; can be modified as needed
    chartObj.Activate
    ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
    ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
    ActiveChart.SetElement (msoElementPrimaryValueGridLinesNone)
    ActiveChart.SetElement (msoElementPrimaryCategoryGridLinesNone)
'Add chart title and title of the axes
    oChart.Axes(xlCategory).HasTitle = True
    oChart.Axes(xlCategory).AxisTitle.Caption = "X values"
    oChart.Axes(xlValue).HasTitle = True
    oChart.Axes(xlValue).AxisTitle.Caption = "Y values"
    oChart.HasTitle = True
    tit = "Hypotrochoid " & a & "-" & b & "-" & c & ", " & a1 & "-" & b1 & "-" & c1
    oChart.ChartTitle.Text = tit
    oChart.ChartTitle.Select
    With Selection.Font
        .Bold = msoTrue
        .Size = 14
    End With
    ActiveChart.ChartArea.Select
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 0.25
    End With
    ActiveChart.PlotArea.Select
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 200)
    End With
    ActiveChart.FullSeriesCollection(1).Select
    Selection.MarkerStyle = 8
    Selection.MarkerSize = 5
    Selection.Format.Fill.ForeColor.RGB = RGB(192, 0, 0)
    Selection.Format.Line.Visible = msoFalse
    ActiveChart.FullSeriesCollection(2).Select
    Selection.MarkerStyle = 8
    Selection.MarkerSize = 5
    Selection.Format.Fill.ForeColor.RGB = RGB(192, 0, 100)
    Selection.Format.Line.Visible = msoFalse
    ActiveChart.PlotArea.Select
    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 112, 192)
        .Weight = 1
    End With
    ActiveSheet.Range("A1").Select
End Sub
 




No comments:

Post a Comment

All comments are held for moderation. I reserve the right to edit, censor, delete and - if necessary - block comments.