STM32F746DISC开发板上有一个很大的液晶屏,可以显示各种图像。下面展示了用开发板显示奇妙的分形图形。
这里不介绍分形的理论了,大家可以网上搜索看看,它的计算公式是 Z = Z*Z + C,反复迭代计算,根据发散速度标明各点的颜色。
先看看实际效果,手机拍出来有些条纹,实际是没有的,液晶屏上显示的效果其实还不错。
下面是的程序代码。目前只实现了基本的显示,下一步将增加放大、缩小、改变颜色等功能。
- #include "mbed.h"
- #include "LCD_DISCO_F746NG.h"
- #include "TS_DISCO_F746NG.h"
- #define ITERATION 150
- #define BAILOUT 4
- LCD_DISCO_F746NG lcd;
- TS_DISCO_F746NG ts;
- uint16_t screen_width, screen_height;
- uint16_t calcMandelBrot(double x, double y)
- {
- uint16_t i;
- double xx, yy, tx, ty;
-
- xx = yy = 0;
- i = 0;
- while((i < ITERATION) && ((xx*xx + yy*yy) < BAILOUT))
- {
- tx = xx*xx - yy*yy + x;
- ty = 2*xx*yy + y;
- xx = tx;
- yy = ty;
- i++;
- }
- return i;
- }
- void rectMandelBrot(double x1, double y1, double x2, double y2)
- {
- double dx, dy;
- uint16_t i, j;
- uint32_t color;
-
- dx = (x2 - x1)/screen_width;
- dy = (y2 - y1)/screen_height;
-
- for(i = 0; i < screen_width; i++)
- {
- for(j = 0; j < screen_height; j++)
- {
- color = calcMandelBrot(x1 + dx*i, y1 + dy*j);
- lcd.DrawPixel(i, j, 0xFF000000 | (color<<2) | (color <<4));
- }
- }
-
- }
- int main()
- {
- lcd.Clear(LCD_COLOR_BLACK);
- screen_width = lcd.GetXSize() - 100;
- screen_height = lcd.GetYSize();
- rectMandelBrot(-2.5, -2, 2.5, 2);
-
- while(1)
- {
- }
- }
复制代码
fract.zip
(1.33 MB, 下载次数: 42)
|
mbed好处是简单,缺点是占用空间较大。