3D_demo项目之一,一个关于5个神奇吸引子的程序,分别是:
1.洛伦茨吸引子
2.陈氏吸引子
3.吕氏吸引子
4.Russler吸引子
5.Thomas吸引子
食用方法:wasd 空格 shift 以及鼠标的移动
代码:
#include <stdio.h>
#include <iostream>
#include <graphics.h>
#include <math.h>
#include <time.h>
#include "Tpot.h"
using namespace std;
float sj = 200.0;//透视视角
const int w = 640,h = 480,pixn = 30570;//屏幕长宽,所有点的数量
float mx=0,my=0,mz=0,spd = 1.03;//视角位置,移动速度
void put3dpixel(VECTOR3D v1,int colorz) {//GY自创的恶心的透视算法
if (v1.z < 1)return;
float v1z = v1.z/sj;
int x1=(v1.x)/v1z+w/2,y1=(v1.y)/v1z+h/2;
putpixel(x1,y1,colorz);
}
struct pix {
VECTOR3D pix;
color_t col;
void drawpix() {//画点
VECTOR3D wz = (VECTOR3D) {
mx,my,mz
};
pix+=wz;
put3dpixel(pix,col);
}
void rotatex(float rad) {//绕x轴旋转
rotate_point3d_x(&pix,rad);
}
void rotatey(float rad) {//绕y轴旋转
rotate_point3d_y(&pix,rad);
}
void rotatez(float rad) {//绕z轴旋转
rotate_point3d_z(&pix,rad);
}
void addv(VECTOR3D v) {//点的位置加上位置,额
pix += v;
}
} pixs[2000001];
void initpixs() {//初始化 吸引子
int dw=pixn/5;
double a=10.000000;
double b=28.000000;
double c=2.666667;
double t=0.005000;
double x=-10.000000;
double y=10.000000;
double z=25.000000;
for (int i = 0; i <= dw; ++i) {
pixs[i].col = 0x001100;
double u=a*(y - x);
double v=b*x - y - x*z;
double w=x*y - c*z;
x=x+u*t;
y=y+v*t;
z=z+w*t;
pixs[i].pix.x = x*30.0;
pixs[i].pix.y = y*30.0;
pixs[i].pix.z = z*30.0;
}
a=40.000000;
b=3.000000;
c=28.000000;
double g=1.000000;
double h=5.000000;
t=0.005000;
x=-0.100000;
y=0.500000;
z=-0.600000;
for (int i = dw; i <= dw*2; ++i) {
pixs[i].col = 0x111100;
double f=g*z-h*sin(z);
double u=a*(y - x);
double v=(c-a)*x - x*f + c*y;
double w=x*y - b*z;
x=x+u*t;
y=y+v*t;
z=z+w*t;
pixs[i].pix.x = x*30.0+3000;
pixs[i].pix.y = y*30.0;
pixs[i].pix.z = z*30.0;
}
a=36.000000;
b=3.000000;
c=19.335405;
x=1.000000;
y=1.000000;
z=1.000000;
double r=5.000000;
t=0.005000;
for (int i = dw*2; i <= dw*3; ++i) {
pixs[i].col = 0x000011;
double s;
if (z > 0)s = 1;
if (z == 0)s = 0;
if (z < 0)s = -1;
double u=a*(y - x);
double v=c*y-x*z*s;
double w=x*y*s - b*z;
x=x+u*t;
y=y+v*t;
z=z+w*t;
pixs[i].pix.x = x*30.0+6000;
pixs[i].pix.y = y*30.0;
pixs[i].pix.z = z*30.0;
}
a=0.200000;
b=0.200000;
c=5.700000;
x=1.000000;
y=1.000000;
z=1.000000;
t=0.007000;
for (int i = dw*3; i <= dw*4; ++i) {
pixs[i].col = 0x000011;
double u=-(y + z);
double v=x+a*y;
double w=b+z*(x - c);
x=x+u*t;
y=y+v*t;
z=z+w*t;
pixs[i].pix.x = x*30.0+9000;
pixs[i].pix.y = y*30.0;
pixs[i].pix.z = z*30.0;
}
b=0.099000;
x=1.000000;
y=1.000000;
z=0.000000;
t=0.021000;
for (int i = dw*4; i <= dw*5; ++i) {
pixs[i].col = 0x000011;
double u=-b*x + sin(y);
double v=-b*y + sin(z);
double w=-b*z + sin(x);
x=x+u*t;
y=y+v*t;
z=z+w*t;
pixs[i].pix.x = x*30.0+12000;
pixs[i].pix.y = y*30.0;
pixs[i].pix.z = z*30.0;
}
}
void updatexy() {
if (GetAsyncKeyState('W'))
mz-=0.3;
if (GetAsyncKeyState('S'))
mz+=0.3;
if (GetAsyncKeyState('D'))
mx-=0.3;
if (GetAsyncKeyState('A'))
mx+=0.3;
if (GetAsyncKeyState(VK_SHIFT))
my-=0.3;
if (GetAsyncKeyState(VK_SPACE))
my+=0.3;
}
void drawpos(int x,int y) {
setcolor (0x00ff00);
line (w/2-7,h/2,w/2+7,h/2);
line (w/2,h/2-7,w/2,h/2+7);
putpixel (x,y,0xff0000);
putpixel (x-1,y,0xff0000);
putpixel (x,y-1,0xff0000);
putpixel (x+1,y,0xff0000);
putpixel (x,y+1,0xff0000);
}
int main() {
srand(time(NULL));
setinitmode (2);
initgraph(w,h);
setbkcolor (0xccddff);
showmouse(0);
initpixs();
MOUSEMSG msg;
HWND wdhw = getHWnd();
for (int rt = 0;; rt++,delay_fps(60)) {
mx/=spd;
my/=spd;
mz/=spd;
if (rt%8==0) {
RECT rect;
GetWindowRect(wdhw,&rect);
SetCursorPos(rect.left+w/2+3,rect.top+h/2+25);
}
while(mousemsg()) {
msg = GetMouseMsg();
}
if (msg.x!=w/2) {
float ry = (w/2-msg.x)/710.0;
for (int i = 0; i <= pixn; ++i)
pixs[i].rotatey(ry);
}
if (msg.y!=h/2) {
float rx = (msg.y-h/2)/710.0;
for (int i = 0; i <= pixn; ++i)
pixs[i].rotatex(rx);
}
cleardevice();
for (int i = 0; i <= pixn; ++i)
pixs[i].drawpix();
drawpos(msg.x,msg.y);
updatexy();
msg.x = w/2;
msg.y = h/2;
if (GetAsyncKeyState(VK_ESCAPE))return 0;
setcolor (0x442233);
xyprintf (0,h-16,"fps:%.5f -={gy314}=-",getfps());
}
}/*水得一批*/