#概要
arduinoでマンデルブロやってみた。
#写真
#サンプルコード
#include <Adafruit_GFX.h>
#include "Adafruit_TFTLCD.h"
#define XP 8
#define YP A3
#define XM A2
#define YM 9
#define TS_MINX 150 - 40
#define TS_MAXX 920 - 40
#define TS_MINY 120 - 40
#define TS_MAXY 940 - 40
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define MINPRESSURE 10
#define MAXPRESSURE 1000
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
const int MAX = 256;
float cx,
cy;
float zoom = 1.0f;
void mandelbrot(float x1, float y1, float x2, float y2) {
unsigned int i,
j;
uint16_t iter;
uint16_t color;
float sy = y2 - y1;
float sx = x2 - x1;
for(i = 0; i < tft.width(); i++)
{
for(j = 0; j < tft.height(); j++)
{
float cy = j * sy / tft.height() + y1;
float cx = i * sx / tft.width() + x1;
float x = 0.0f,
y = 0.0f,
xx = 0.0f,
yy = 0.0f;
for(iter = 0; iter <= MAX && (xx + yy) < 4.0f; iter++)
{
xx = x * x;
yy = y * y;
y = 2.0f * x * y + cy;
x = xx - yy + cx;
}
color = ((iter << 7 & 0xF8) << 8) | ((iter << 4 & 0xFC) << 3) | (iter >> 3);
tft.drawPixel(i, j, color);
}
}
}
void setup(void) {
int i;
tft.reset();
uint16_t identifier = 0x9341;
tft.begin(identifier);
tft.fillScreen(BLACK);
cx = -0.086f;
cy = 0.55f;
}
void loop() {
mandelbrot(-2.0f * zoom + cx, -1.5f * zoom + cy, 2.0f * zoom + cx, 1.5f * zoom + cy);
zoom *= 0.7f;
if (zoom <= 0.00001f)
zoom = 1.0f;
}
以上。