The source code of a procedure that generates fractals

Z PostgreSQL
Skočit na navigaci Skočit na vyhledávání
DELIMITER //

CREATE SCHEMA fractal //
USE fractal //

CREATE TABLE disp (
  id INT PRIMARY KEY,
  content VARCHAR(1000)
) //

CREATE TABLE shade (
  id INT PRIMARY KEY,
  value INT
) //

INSERT INTO shade (id,value) VALUES (0,ORD('')),(1,ORD('.')),(2,ORD(',')),(3,ORD('-')),
(4,ORD('~')),(5,ORD('*')),(6,ORD(':')),(7,ORD(';')),
(8,ORD('+')),(9,ORD('o')),(10,ORD('O')),(11,ORD('&')),
(12,ORD('%')),(13,ORD('@')) //

CREATE PROCEDURE mandelbrot (IN x_max INT, IN y_max INT)
BEGIN
  DECLARE x INT DEFAULT 0;
  DECLARE y INT DEFAULT 0;
  DECLARE xf0 FLOAT;
  DECLARE yf0 FLOAT;
  DECLARE xf1 FLOAT;
  DECLARE yf1 FLOAT;
  DECLARE tf FLOAT;
  DECLARE l INT;
  DECLARE l_max INT;
  DECLARE buffer VARCHAR(1000);
  DECLARE tint INT;
  TRUNCATE disp;
  SELECT MAX(id) INTO l_max FROM shade LIMIT 1;
  WHILE y<y_max DO
    SET buffer='';
    SET x=0;
    WHILE x<x_max DO
      SET xf0=x*4.0/x_max-2.0;
      SET yf0=y*4.0/y_max-2.0;
      SET xf1=xf0;
      SET yf1=yf0;
      SET l=0;
      WHILE l<l_max AND xf1*xf1+yf1*yf1<4.0 DO
        SET tf=xf1*xf1-yf1*yf1+xf0;
        SET yf1=2.0*xf1*yf1+yf0;
        SET xf1=tf;
        SET l=l+1;
      END WHILE;
      SELECT value INTO tint FROM shade WHERE id=l;
      SET buffer=CONCAT(CHAR(tint),buffer);
      SET x=x+1;
    END WHILE;
    INSERT INTO disp (id,content) VALUES (y,buffer);
    SET y=y+1;
  END WHILE;
  SELECT content FROM disp ORDER BY id ASC;
END //

CALL mandelbrot (50,20) //