How to resolve the algorithm Fibonacci word/fractal step by step in the Java programming language
How to resolve the algorithm Fibonacci word/fractal step by step in the Java programming language
Table of Contents
Problem Statement
The Fibonacci word may be represented as a fractal as described here: (Clicking on the above website (hal.archives-ouvertes.fr) will leave a cookie.)
Create and display a fractal similar to Fig 1. (Clicking on the above website (hal.archives-ouvertes.fr) will leave a cookie.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fibonacci word/fractal step by step in the Java programming language
Java Code to Generate and Draw the Fibonacci Word Fractal:
Introduction:
This Java program creates a mesmerizing Fibonacci word fractal as a visual representation of the Fibonacci sequence, a renowned mathematical series where each number is the sum of the two preceding ones. The program generates a binary string (the Fibonacci word) and uses it to construct a fractal pattern that exhibits self-similarity at various scales.
Key Implementation Details:
FibonacciWordFractal Class:
- Initialization: This class extends JPanel and initializes various properties, including the preferred size, background color, and the Fibonacci word fractal string.
- wordFractal Method: Computes the Fibonacci word string iteratively based on the number of terms specified by the user.
- drawWordFractal Method: Responsible for drawing the Fibonacci word fractal using Graphics2D. It draws lines based on the binary values (0s and 1s) in the Fibonacci word string. The direction of the lines is determined by the value at each position.
- paintComponent Method: Overridden from JPanel to custom paint the Fibonacci word fractal. It calls the
drawWordFractal
method to generate the fractal.
Main Method:
- SwingUtilities.invokeLater: Launches the graphical user interface (GUI) on the Event Dispatch Thread (EDT).
- JFrame: Creates a JFrame window with a title, default close operation, and non-resizable property.
- add: Adds an instance of
FibonacciWordFractal
to the frame. - pack: Packs the frame to fit its preferred size.
- setLocationRelativeTo(null): Centers the frame on the screen.
- setVisible(true): Makes the frame visible.
Fractal Drawing:
The program constructs the fractal based on the Fibonacci word string. It draws lines in a specific direction (horizontal or vertical) depending on each binary digit. These lines are connected to form a continuous path. The fractal exhibits a recursive structure, where smaller versions of itself are embedded within larger patterns.
Conclusion:
This Java program offers a visual and engaging way to explore the intricacies of the Fibonacci sequence and fractal geometry through the Fibonacci word fractal. Users can input different values to witness the astonishing patterns and intricate self-similarity of this mathematical phenomenon.
Source code in the java programming language
import java.awt.*;
import javax.swing.*;
public class FibonacciWordFractal extends JPanel {
String wordFractal;
FibonacciWordFractal(int n) {
setPreferredSize(new Dimension(450, 620));
setBackground(Color.white);
wordFractal = wordFractal(n);
}
public String wordFractal(int n) {
if (n < 2)
return n == 1 ? "1" : "";
// we should really reserve fib n space here
StringBuilder f1 = new StringBuilder("1");
StringBuilder f2 = new StringBuilder("0");
for (n = n - 2; n > 0; n--) {
String tmp = f2.toString();
f2.append(f1);
f1.setLength(0);
f1.append(tmp);
}
return f2.toString();
}
void drawWordFractal(Graphics2D g, int x, int y, int dx, int dy) {
for (int n = 0; n < wordFractal.length(); n++) {
g.drawLine(x, y, x + dx, y + dy);
x += dx;
y += dy;
if (wordFractal.charAt(n) == '0') {
int tx = dx;
dx = (n % 2 == 0) ? -dy : dy;
dy = (n % 2 == 0) ? tx : -tx;
}
}
}
@Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
drawWordFractal(g, 20, 20, 1, 0);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Fibonacci Word Fractal");
f.setResizable(false);
f.add(new FibonacciWordFractal(23), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
You may also check:How to resolve the algorithm Vector products step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm LZW compression step by step in the Xojo programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Aime programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Lingo programming language
You may also check:How to resolve the algorithm Faces from a mesh step by step in the Lua programming language