…is always light

Tag Archives: Cantor’s Set

In my old article I have implementation for Cantor’s set without recursion. Now I implemented the Cantor’s set with recursion in JavaScript. This is better solution.

The definition of the Cantor’s set in the language of set theory is the following:

If this doesn’t mean anything to you, you might want to check the old post. 🙂

Below is a picture from the output of the program of this post:

 

Below is the JavaScript listing from Notepad++ in full as png-file:

 

cantor listing

Advertisements

The Cantor’s set is somewhat fascinating fractal, when one gets to more familiar with it.

The basic idea is to handle real number interval [0,1]. This is divided to three parts of same width removing the middle part. The remaining parts are again divided to three parts of same width removing always the middle part. This is continued infinitely.

The Cantor’s set consists of the points that are left in this process.

In the language of the set theory the Cantor’s set can be expressed as follows:

Cantorin joukko

The union tells what doesn’t belong to the set.

I have used this formula to implement the Cantor’s set in my program. The idea is to examine the union interval and draw a pixel, when the point does belong to the set.

Below is a picture from the program’s output:

Cantor set

 

Here’s the Java program in full:

import java.awt.*;
import javax.swing.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.lang.Math;

public class Cantor {

    private JFrame frame;
    private CantorPanel panel;
    
    public Cantor() {
        frame = new JFrame("The Cantor's Set");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initialize();
        frame.pack();
        frame.setVisible(true);
    }
    
    private void initialize() {
        panel = new CantorPanel();
        frame.getContentPane().add(panel);
    }
    
    public static void main(String args[]) {
        new Cantor();
    }
}
    
class CantorPanel extends JPanel {
    
    public CantorPanel() {
        
        setBackground(Color.lightGray);
        setPreferredSize(new Dimension(1024,100));
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        drawCantor(g, 7);
    }
    
    public void drawCantor(Graphics g, int maxIter) {
        
        int y = 17;
        
        for (int m = 1; m < maxIter + 1; m++) {

            for (int x = 0; x < 1024; x++) {
            
                if (belongsToSet(m,x) == true) g.drawOval(x,y,1,1);
            }
            y = y + 10;
        }
    }
    
    public boolean belongsToSet(int m, double x) {

        double line = 1024;
        double x1,x2;

        for(int mm = 1; mm < m ; mm++) {
                
            for (int k = 0; k < (int)((Math.pow(3,(mm-1))) - 1 + 1); k++) {

                x1 = (3.0*k + 1) / Math.pow(3.0,mm);
                x2 = (3.0*k + 2) / Math.pow(3.0,mm);
            
                if ((x > x1 * line) && (x < x2 * line)) return false;
    
            }

        }
        return true;
    }
    
}

Because of the calculation precision with “big” number of iterations drawing accuracy isn’t very good.

The program is free for personal and educational use.

On Georg Cantor