![]() |
Матрица статей Список статей Всячина Контакты | ||||||||||||
|
Дерево Пифагора Пифагор, доказывая свою знаменитую теорему, построил фигуру, где на сторонах прямоугольного треугольника расположены квадраты. Если этот процесс продолжить, то и получится дерево Пифагора.
Приведём программу для построения, написанную на языке Pascal. program Pyth; uses CRT, Graph; procedure Draw(x, y, l, a: Real); procedure Rect(x1, y1, l: Integer; a1: Real); begin MoveTo(x1, y1); LineTo(x1 + Round(l * cos(a1)), y1 - Round(l * sin(a1))); LineTo(x1 + Round(l * sqrt(2) * cos(a1 + pi/4)), y1 - Round(l * sqrt(2) * sin(a1 + pi/4))); LineTo(x1 + Round(l * cos(a1 + pi/2)), y1 - Round(l * sin(a1 + pi/2))); LineTo(x1, y1); end; begin if l > 4 then begin Rect(Round(x), Round(y), Round(l), a); Draw(x - l*sin(a), y - l * cos(a), l / sqrt(2), a + pi / 4); Draw( x - l * sin(a) + l / sqrt(2) * cos(a + pi/4), y - l * cos(a) - l / sqrt(2) * sin(a + pi/4), l / sqrt(2), a - pi/4); end; end; var gd, gm: Integer; begin gd := detect; InitGraph(gd, gm, 'c:\bp\bgi'); Draw(280, 460, 100, 0); ReadKey; CloseGraph; end. Одним из свойств дерева Пифагора является то, что, если площадь первого квадрата равна единице, то на каждом уровне площадь квадратов тоже будет равна единице. Заметим также, что дерево Пифагора является разновидностью двоичного дерева. Если в классическом дереве Пифагора угол равен 45 градусам, то, как обобщение классического дерева Пифагора, можно строить обобщенное дерево Пифагора или, как его по-другому называют, обдуваемое ветром дерево Пифагора. Способ построения опять же виден из рисунка.
Другим интересным свойством этого дерева является то, что очертания веток образуют логарифмическую спираль. Можно также упростить дерево Пифагора и рисовать не квадраты, а только отрезки соединяющие "центры" треугольников. При этом сами треугольники не рисуются. Будем называть такой фрактал обнаженным деревом Пифагора.
program PythTree;
uses Graph, CRT;
const
max = 3;
var
gd, gm : Integer;
procedure LineTo1(x, y : Integer; l, u : Real);
begin
Line(x, y, Round(x + l * cos(u)), Round(y - l * sin(u)));
end;
procedure Draw(x, y : Integer; l, u : real);
begin
if KeyPressed then
exit;
if l > max then
begin
l := l * 0.7;
LineTo1(x, y, l, u);
x := Round(x + l * cos(u));
y := Round(y - l * sin(u));
Draw(x, y, l, u + pi / 4); {Угол поворота 1}
Draw(x, y, l, u - pi / 6); {Угол поворота 2}
end;
end;
begin
gd := Detect;
InitGraph(gd, gm, 'c:\bp\bgi');
Draw(320, 460, 200, pi/2);
ReadKey;
CloseGraph;
end.
Добавим немного цвета.
package ru.xaoc.fractalworld.pythagorastree;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
private static final int WIDTH = 1000;
private static final int HEIGHT = 500;
private static BufferedImage image;
private static Graphics2D graph;
private static final double DA = Math.PI / 6;
private static final int BASE_SIZE = 100;
private static final Color treeColor = new Color(0x712F26);
private static final Color leafColor = new Color(0x00FF00);
private static void drawPythagorasTree(double a, double size,
double x, double y) {
double dx = size * Math.sin(a);
double dy = size * Math.cos(a);
double x1 = x;
double y1 = y;
double x2 = x + dx;
double y2 = y - dy;
double x3 = x + dx - dy;
double y3 = y - dy - dx;
double x4 = x - dy;
double y4 = y - dx;
double x5 = x - dy + size * Math.cos(DA) * Math.sin(a - DA);
double y5 = y - dx - size * Math.cos(DA) * Math.cos(a - DA);
Path2D path = new Path2D.Double();
path.moveTo(x1, y1);
path.lineTo(x2, y2);
path.lineTo(x3, y3);
path.lineTo(x5, y5);
path.lineTo(x4, y4);
path.lineTo(x1, y1);
path.closePath();
graph.setColor(getBetweenColor(leafColor, treeColor,
Math.pow(size / BASE_SIZE, 0.2)));
graph.fill(path);
if (size > 1) {
drawPythagorasTree(
a - DA,
size * Math.cos(DA),
x4,
y4);
drawPythagorasTree(
a - DA + (Math.PI / 2),
size * Math.sin(DA),
x5,
y5);
}
}
private static Color getBetweenColor(Color startColor, Color endColor,
double p) {
return new Color(
(int) (startColor.getRed() +
(endColor.getRed() - startColor.getRed()) * p),
(int) (startColor.getGreen() +
(endColor.getGreen() - startColor.getGreen()) * p),
(int) (startColor.getBlue() +
(endColor.getBlue() - startColor.getBlue()) * p));
}
public static void main(String[] args) {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
graph = image.createGraphics();
graph.setColor(Color.WHITE);
graph.fill(new Rectangle2D.Double(0, 0, WIDTH, HEIGHT));
graph.setColor(Color.BLACK);
drawPythagorasTree(
Math.PI / 2,
BASE_SIZE,
WIDTH / 2 - BASE_SIZE / 2,
HEIGHT - 50);
JFrame frame = new JFrame();
frame.addNotify();
frame.setSize(frame.getInsets().left +
frame.getInsets().right + WIDTH,
frame.getInsets().top +
frame.getInsets().bottom + HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JPanel() {
@Override
public void paintComponent(Graphics g) {
Graphics2D G = (Graphics2D) g;
if (image != null) {
G.drawImage(image, 0, 0, null);
}
}
});
frame.setVisible(true);
}
}
Смотрите также: Ссылки:
|