cantor set Algorithm
The Cantor Set Algorithm is a recursive technique used to generate a fractal known as the Cantor Set, which is a set of points on a line segment that is obtained by repeatedly removing the middle third of each remaining sub-segment. The Cantor Set is a notable example in mathematics due to its peculiar properties, such as being uncountable, yet having a total length of zero. The construction of the Cantor Set is an iterative process that starts with a line segment and successively removes the middle third of each sub-segment, continuing infinitely.
In the first step of the Cantor Set Algorithm, a line segment is drawn, usually represented as the closed interval [0, 1]. In the second step, the middle third of the line segment is removed, resulting in two disjoint line segments, represented by the intervals [0, 1/3] and [2/3, 1]. In each subsequent step, the algorithm continues by removing the middle third of each remaining sub-segment. This process is repeated infinitely, and at each step, the number of sub-segments doubles, and their length decreases by a factor of 3. The Cantor Set is formed by the points that are never removed from the line segment during this infinite process.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct contour
{
double start;
double end;
struct contour* next;
};
typedef struct contour Contour;
Contour *head;
void startList(double start_num, double end_num)
{
if(head==NULL)
{
head = (Contour*)malloc(sizeof(Contour));
head -> start = start_num;
head -> end = end_num;
head -> next = NULL;
}
}
void propagate(Contour *head)
{
Contour *temp = head;
if(temp!=NULL)
{
Contour *newNode = (Contour*)malloc(sizeof(Contour));
double diff = ( ((temp->end)-(temp->start)) / 3);
newNode->end = temp->end;
temp->end = ((temp->start)+diff);
newNode->start = (newNode->end)-diff;
newNode->next = temp->next;
temp->next=newNode;
propagate(temp->next->next);
}
else
return;
}
void print(Contour *head)
{
Contour *temp = head;
while(temp!=NULL)
{
printf("\t");
printf("[%lf] -- ", temp->start);
printf("[%lf]", temp->end);
temp=temp->next;
}
printf("\n");
}
int main(int argc, char const *argv[])
{
head=NULL;
int start_num, end_num, levels;
if (argc < 2)
{
printf("Enter 3 arguments: start_num \t end_num \t levels\n");
scanf("%d %d %d", &start_num, &end_num, &levels);
}
else
{
start_num = atoi(argv[1]);
end_num = atoi(argv[2]);
levels = atoi(argv[3]);
}
startList(start_num, end_num);
for (int i = 0; i < levels; i++)
{
printf("Level %d\t", i);
print(head);
propagate(head);
printf("\n");
}
printf("Level %d\t", levels);
print(head);
return 0;
}