分形之螺线
把一条直线扭成螺旋上升,再把得到的螺旋直线当做一条直线扭成螺旋上升如此无限循环.从而生成一种分形螺线.
核心代码:
1 void CFractalHelix::FractalHelix(const Vector3& vStart, const Vector3& vEnd, Vector3* pVertices)
2 {
3 Vector3 vDir = vEnd - vStart;
4 float len = D3DXVec3Length(&vDir);
5 float radius = len*m_params[0];
6
7 float xz = sqrtf(vDir.x*vDir.x + vDir.z*vDir.z);
8 float angleY = atan2f(xz, vDir.y);
9 float angleXZ = atan2f(vDir.x, vDir.z);
10
11 float e = sin(angleY);
12 float f = cos(angleY);
13
14 float g = sin(angleXZ);
15 float h = cos(angleXZ);
16
17 for (int i = 0; i < CIRCLE_POINTS_COUNT; i++)
18 {
19 pVertices[i] = vStart + vDir*(i*1.0f/CIRCLE_POINTS_COUNT);
20
21 float x0 = m_vSinCos[i].y;
22 float y0 = - m_vSinCos[i].x*e;
23 float z0 = + m_vSinCos[i].x*f;
24
25 float x1 = x0*h + z0*g;
26 float y1 = y0;
27 float z1 = -x0*g + z0*h;
28
29 pVertices[i].x += x1*radius;
30 pVertices[i].y += y1*radius;
31 pVertices[i].z += z1*radius;
32 }
33 }
代码写得有点让人头晕,其实就是将一根线段掰弯,生成若干表示曲线的线段,然后不停迭代.不过代码还有点瑕疵,有的线段接缝过度不太顺畅.
软件截图: