VideoPhoto

23 July, 2023

Curves Based On Equations: Lemniscate of Bernoulli

The Lemniscate curve is based on specific algebraic polynomial equation. It has a shape similar to the numeral 8 and to the infinity symbol (called also a ribbon). You can create it with Excel VBA code and use it as one of the elements of graphic design.

Here are two examples of the curve created with the macro code listed below them.

In this code, instead of using data from a worksheet, the X and Y values are calculated using equations within the VBA code. The ReDim statement is used to size the arrays for X and Y values based on the desired number of data points. The code then creates a scatter chart object, sets the chart type, and assigns the calculated X and Y values to the chart series.

Sub CreateScatterChart()
'Creates Lemniscate of Bernoulli chart (example)
Dim i As Integer, numPoints As Integer
Dim chartObj As ChartObject
Dim oChart As Chart
Dim xValues() As Double, yValues() As Double
Dim xvValues() As Double, yvValues() As Double
'Set the number of data points and calculate X and Y values; change as needed
numPoints = 360
ReDim xValues(1 To numPoints)
ReDim yValues(1 To numPoints)
ReDim xvValues(1 To numPoints)
ReDim yvValues(1 To numPoints)
'Calculate X and Y values using the equations
For i = 1 To numPoints Step 2   'First series (horizontal)
            xValues(i) = Cos(WorksheetFunction.Radians(i)) / (1 + Sin(WorksheetFunction.Radians(i)) ^ 2)             ^ 0.5
            yValues(i) = Cos(WorksheetFunction.Radians(i)) * Sin(WorksheetFunction.Radians(i)) / (1 +                Sin(WorksheetFunction.Radians(i)) ^ 2) ^ 0.5
Next i
For i = 1 To numPoints Step 2   'Second series (vertical)
          yvValues(i) = Cos(WorksheetFunction.Radians(i)) / (1 + Sin(WorksheetFunction.Radians(i)) ^ 2)           ^ 0.5
          xvValues(i) = Cos(WorksheetFunction.Radians(i)) * Sin(WorksheetFunction.Radians(i)) /  _(1 +           Sin(WorksheetFunction.Radians(i)) ^ 2) ^ 0.5
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 can be modified to add or remove some chart elements
    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
    oChart.ChartTitle.Text = "Lemniscate of Bernoulli"
    oChart.ChartTitle.Select
    With Selection.Format.TextFrame2.TextRange.Characters(1, 23).Font
        .Bold = msoTrue
        .Size = 14
    End With
'Delete legend, titles of the chart axes
  ActiveChart.ChartArea.Select
    ActiveChart.Legend.Select
    Selection.Delete
    ActiveChart.Axes(xlCategory).AxisTitle.Select
    Selection.Delete
    ActiveChart.Axes(xlValue).AxisTitle.Select
    Selection.Delete
'Format some elements of the chart and plot area
    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, 0)
    Selection.Format.Line.Visible = msoFalse
    ActiveChart.PlotArea.Select
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 0.25
    End With
    Range("A1").Select
End Sub

If you don't remember how to enter and run the macro in Excel, here's short reminder:

  • Open the Visual Basic Editor (VBE): Go to the Developer tab and click on Visual Basic.
  • In the VBE, you'll see the Project Explorer on the left-hand side. If it's not visible, press Ctrl+R to show it.
  • In the Project Explorer, expand the project for the workbook you want to add the macro to.
  • Right-click on the workbook project and select Insert > Module. This will insert a new module.
  • Double-click on the module to open it in the code window.
  • Paste or write your VBA macro code in the module.
  • To run the macro, you have several options. One of them is: press Alt+F8 to open the Macros dialog box. Select your macro from the list and click on Run.

No comments:

Post a Comment

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