Recursion Visualized

  • Finn Carpenter
  • XCHART Library

Important Note

  • If you hit the X button to close the window it breaks the kernel
  • Two Options
    • Have a bunch of those windows, when done the close
    • Keep refreshing kernel by switching to python and then back to java

Basic X Chart Code

%maven org.knowm.xchart:xchart:3.5.2

import org.knowm.xchart.*;

public class Example0 {
 
    public static void main(String[] args) throws Exception {
        
        // these vars hold your X and Y values
        double[] xData = new double[] { 0.0, 1.0, 2.0 };
        double[] yData = new double[] { 2.0, 1.0, 0.0 };
   
        // Create Chart
        XYChart chart = QuickChart.getChart("Sample Chart", "X", "Y", "y(x)", xData, yData);
        
        // Show it
        new SwingWrapper(chart).displayChart();
    }
  }

  Example0.main(null);

X Chart Graphs with recursion

  • What is the shape of the graph going to look like when the recursive function is done
  • The equation would be __
private static void graph(double[] xData, double[] yData, int index, double x, int maxIndex, double stepSize) {
    if (index > maxIndex) {
        return;
    }

    xData[index] = x;
    yData[index] = x * x;

    graph(xData, yData, index + 1, x + stepSize, maxIndex, stepSize);
}
%maven org.knowm.xchart:xchart:3.5.2

import org.knowm.xchart.*;

public class recursiveGraph {

    public static void main(String[] args) throws Exception {
        int numPoints = 100;
        double[] xData = new double[numPoints];
        double[] yData = new double[numPoints];

        plotParabola(xData, yData, 0, -5.0, numPoints - 1, 0.1);

        // Create Chart
        XYChart chart = QuickChart.getChart("Parabola", "X", "Y", "y(x)", xData, yData);

        // Show it
        new SwingWrapper(chart).displayChart();
    }

    private static void plotParabola(double[] xData, double[] yData, int index, double x, int maxIndex, double stepSize) {
        if (index > maxIndex) {
            return;
        }

        xData[index] = x;
        yData[index] = x * x;

        plotParabola(xData, yData, index + 1, x + stepSize, maxIndex, stepSize);
    }
}


RecursiveGraph.main(null);

XChart 2

  • What is the shape of the graph going to look like when the recursive function is done
  • The equation would be __
private static void plot(double[] xData, double[] yData, int index, double x, int maxIndex, double stepSize, double base) {
    if (index > maxIndex) {
        return;
    }

    xData[index] = x;
    yData[index] = Math.pow(base, x);

    plot(xData, yData, index + 1, x + stepSize, maxIndex, stepSize, base);
}
%maven org.knowm.xchart:xchart:3.5.2

import org.knowm.xchart.*;

public class recursiveGraph2 {

    public static void main(String[] args) throws Exception {
        int numPoints = 100;
        double[] xData = new double[numPoints];
        double[] yData = new double[numPoints];

        plotExponential(xData, yData, 0, -5.0, numPoints - 1, 0.1, 2.0); // You can adjust the base as needed (e.g., 2.0 for 2^x)

        // Create Chart
        XYChart chart = QuickChart.getChart("Mati Yapping Graph", "Seconds of Mati Yapping", "My Anger", "y(x)", xData, yData);

        // Show it
        new SwingWrapper(chart).displayChart();
    }

    private static void plotExponential(double[] xData, double[] yDawta, int index, double x, int maxIndex, double stepSize, double base) {
        if (index > maxIndex) {
            return;
        }

        xData[index] = x;
        yData[index] = Math.pow(base, x);

        plotExponential(xData, yData, index + 1, x + stepSize, maxIndex, stepSize, base);
    }
}

recursiveGraph2.main(null);

Hacks

  • Finish all popcorn hacks for the lesson
  • Follow the directions bellow for the XChart Hacks

Example of Cool Function for the Hacks

  • If you are having trouble with thinking of a cool equation to put into a recursion form, follow these tips
    • Look up the shape/symbol you would like to put into the graph
    • Try to split the equation up into what math methods you will need
    • Ask the friend who know most about coding (wink wink)
  • Make sure to take a screenshot of the graph and display it next to it’s respective code block
%maven org.knowm.xchart:xchart:3.5.2

import org.knowm.xchart.*;

public class HeartShapeGraph {

    public static void main(String[] args) throws Exception {
        int numPoints = 100;
        double[] xData = new double[numPoints];
        double[] yData = new double[numPoints];

        plotHeartShape(xData, yData, 0, 0, numPoints - 1);

        // Create Chart
        XYChart chart = QuickChart.getChart("Heart Shape", "X", "Y", "y(x)", xData, yData);

        // Show it
        new SwingWrapper(chart).displayChart();
    }

    private static void plotHeartShape(double[] xData, double[] yData, int index, double t, int maxIndex) {
        if (index > maxIndex) {
            return;
        }

        //Chat GPT Math
        xData[index] = 16 * Math.pow(Math.sin(t), 3);
        yData[index] = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t);

        plotHeartShape(xData, yData, index + 1, t + (2 * Math.PI) / maxIndex, maxIndex);
    }
}

HeartShapeGraph.main(null);