728x90

프랙탈 (Fractal)이란 ? 

일부 작은 조각이 전체와 비슷한 기하학적 형태를 말한다. 이런 특징을 자기 유사성이라고 한다. 즉, 자기 유사성을 갖는 기하학적 구조를 프랙탈 구조라고 한다. 프랙탈 구조는 자연물에서 뿐만 아니라 수학적 분석, 생태학적 계산, 위상 공간에 나타나는 운동모형 등 곳곳에서도 발견되어 자연이 가지는 기본적인 구조이다. 프랙탈은 수학적 도형으로도 연구되고 있다고 한다. 프랙탈 도형은 종종 컴퓨터 소프트웨어를 이용한 재귀적이거나 반복적인 작업에 의한 반복되는 패턴으로 만들어진다.

 

프랙탈 트리 (Fractal Tree)

프랙탈 구조가 나무의 모양인 프랙탈 트리를 Java를 이용해  재귀 함수로 그려볼 수 있다. 가지를 그리기 위해서 삼각함수를 이용한다. 

import java.awt.Frame;
import java.awt.Graphics;

/**
 * FractalTree.
*/

public class FractalTree extends Frame {
    int startX;
    int startY;
    int angle;
    int length;
    int rotate;
    int growth;
    int depth;

    /**
     * Constroctors.
     *
     * @param width width
     * @param height height
     * @param x x
     * @param y y
     * @param angle angle
     * @param length length
     * @param rotate rotate
     * @param growth growth
     */
    public FractalTree(int width, int height, int x, int y,
            int angle, int length, int rotate, int growth) {
        this.startX = x;
        this.startY = y;
        this.angle = angle;
        this.length = length;
        this.rotate = rotate;
        this.growth = growth;

        this.setSize(width, height);
    }

    /**
     * Paint branch.
     *
     * @param graphics graphics
     * @param startX startX
     * @param startY startY
     * @param degree degree
     * @param length length
     */
    public void branch(Graphics graphics, int startX, int startY, int degree, int length) {
        if (length > 1) {
            int endX = (int) (startX - length * Math.cos(Math.toRadians(degree)));
            int endY = (int) (startY - length * Math.sin(Math.toRadians(degree)));
            int branchLength = (int) (length * growth * 0.01);

            graphics.drawLine(startX, startY, endX, endY);
            branch(graphics, endX, endY, degree - rotate, branchLength);
            branch(graphics, endX, endY, degree + rotate, branchLength);
        }
    }

    @Override
    public void paint(Graphics graphics) {
        branch(graphics, startX, startY, this.angle, this.length);
    }

    public static void main(String[] args) {
        FractalTree tree = new FractalTree(500, 500, 250, 450, 90, 100, 30, 75);
        tree.setVisible(true);
    }
}

짠 !

 

 


Reference

 

프랙탈 - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. 프랙탈(영어: fractal) 또는 프랙털은 일부 작은 조각이 전체와 비슷한 기하학적 형태를 말한다. 이런 특징을 자기 유사성이라고 하며, 다시 말해 자기 유사성을

ko.wikipedia.org

 

728x90

'Log' 카테고리의 다른 글

백준 골드 달성 일지 v`_`v  (5) 2023.01.25
우분투 (Ubuntu)와 주피터 노트북 (Jupyter notebook)  (2) 2023.01.17
신기한 AI 세상 🤖  (2) 2022.09.08

+ Recent posts