不能旋转科赫雪花

7

编辑:我不确定这是否应该是一个新问题,因此现在我只是更新这个问题。除非我改变原始坐标,否则雪花图案现在能够正确生成。例如,如果我的原始三角形像图片1一样,进行5次迭代后的结果如图片2所示:

enter image description here enter image description here

然而,如果我的原始三角形有任何不同,例如图片3,结果会有偏差:

enter image description here enter image description here

我再次认为问题出在法线上,但我真的很迷惑。我已经试了几个小时来找出正确的公式,但我没有取得任何进展。由于新三角形经常被翻转,我怀疑 atan 在应该给我正值时给了我负值。有没有数学方法可以解决这个问题?非常感谢您的帮助!

我的代码(去掉了openGL部分,因为我认为它们不是问题):

const int NumTimesToSubdivide = 5;
const int NumSegments = 3072;  // 3x4^n lines generated {3, 12, 48, 192, 768, 3072..}
const int NumVertices = NumSegments * 2; // 2 vertices for each segment

vec2 arrayA[NumVertices];
vec2 arrayB[NumVertices];

//----------------------------------------------------------------------------
void koch( const vec2 &a, const vec2 &b, vec2 * verts) {        

/* Calculate new segments.
 *         v1      
 *         /\
 *        /  \
 *       /    \
 * a----/      \----b
 *     v0      v2
 */
vec2 v0;
vec2 v1;
vec2 v2;

GLfloat distance(sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y,2)));
GLfloat deltaX = abs(b.x - a.x);
GLfloat deltaY = abs(b.y - a.y);
GLfloat normalX((b.x - a.x) / deltaX);
GLfloat normalY((b.y - a.y) / deltaY);
GLfloat theta = atan2(b.y - a.y, b.x - a.x) - M_PI / 3.0;
cout << " theta = " << theta << endl;

/*************************
 * Find trisection points
 *************************/
// horizontal line _____________
if(a.y == b.y) {
    vec2 temp0(a.x + (deltaX * normalX / 3) , a.y);
    vec2 temp2(a.x + (deltaX * normalX * 2/3) , a.y);
    vec2 temp1((a.x + b.x) / 2, a.y + distance * sin(theta) / 3);
    v0 = temp0;
    v2 = temp2;
    v1 = temp1;
}

//                 |
// vertical line   |
//                 |
else if(a.x == b.x){
    vec2 temp0(a.x , (a.y + (deltaY * normalY / 3)));
    vec2 temp2(a.x , (a.y + (deltaY * normalY * 2/3)));
    vec2 temp1(a.x + distance * cos(theta) / 3 , (a.y + b.y) / 2);
    v0 = temp0;
    v2 = temp2;
    v1 = temp1;
}

// slope != 0 && slope != 1
else {
    vec2 temp0(a.x + (deltaX * normalX / 3), a.y + (deltaY * normalY / 3));
    vec2 temp2(a.x + (deltaX * normalX * 2/3), a.y + (deltaY * normalY * 2/3));
    // Andrew is the greatest!
    vec2 temp1(temp0.x + distance * cos(theta) / 3, 
               temp0.y + distance * sin(theta) / 3);        
    v0 = temp0;
    v2 = temp2;
    v1 = temp1;
}

verts[0] = a;
verts[1] = v0;
verts[2] = v0;
verts[3] = v1;
verts[4] = v1;
verts[5] = v2;
verts[6] = v2;
verts[7] = b;

}

//----------------------------------------------------------------------------

void divide_line( const vec2& a, const vec2& b, const vec2& c, int n )
{ 

// arrayA = {a, b, b, c, c, a} i.e., the sides of the initial triangle
arrayA[0] = a;
arrayA[1] = b;
arrayA[2] = b;
arrayA[3] = c;
arrayA[4] = c;
arrayA[5] = a;

// If at least one iteration:
if(n > 0) {
    // The current iteration, starting with 0
    int currentIteration = 1;
    // Do for every iteration from 0 - n:
    while (currentIteration <= n) {
        int i;
        int j = 0;
        int size = 3 * 2 * (pow(4,currentIteration-1));
        // Call koch() for each pair of vertices in arrayA
        for(i = 0; i < size; i = i+2) {
            vec2 verts[8];
            koch(arrayA[i], arrayA[i+1], verts);
            // Store each vertex in arrayB
            int k;
            for(k = 0; k <= 7; k++)
                arrayB[j++] = verts[k];
        }
        // Copy arrayB to arrayA for next iteration.
        size = 3 * 2 * pow(4, currentIteration);
        for(i = 0; i < NumVertices; i++) {
            arrayA[i] = arrayB[i];
        }
        // Increase count of currentIteration.
        currentIteration++;
    }
} else
    printf("The number of iterations must be >= 0.\n");
}

我目前正在尝试用C++实现Koch曲线。我已经基本实现了它。我需要解决的问题基本上有三种情况:需要将要断成四段的线段是水平的、垂直的或其他情况。

问题在于,当程序计算出线段的新顶点时,可能存在两种可能性:三角形可以面向“上”或者三角形可以面向“下”。我试图通过标准化矢量来考虑这一点,但我可能做错了,或者还有其他一些小错误。下面的图片是两个例子;两个例子都有 3个迭代的分形。

非水平和非垂直线段拆分的代码如下,因为三角形似乎对于垂直/水平线段面向正确的方向:

if(a.x == b.x) {
  ...
}
else if (a.y == b.y) {
  ...
}
// slope != 0 && slope != 1
else {
    GLfloat deltaX = abs(b.x - a.x);
    GLfloat deltaY = abs(b.y - a.y);
    vec2 temp0(a.x + (deltaX * normalX / 3), a.y + (deltaY * normalY / 3));
    vec2 temp2(a.x + (deltaX * normalX * 2/3), a.y + (deltaY * normalY * 2/3));
    GLfloat dist(sqrt(pow(temp2.x - temp0.x, 2) + pow(temp2.y - temp0.y,2)));
    GLfloat theta = (a.x - b.x)/ (b.y - a.y);
    vec2 temp1((a.x + b.x) / 2 + dist * cos(atan(theta)) ,
               (a.y + b.y) / 2 + dist * sin(atan(theta)));
    v0 = temp0;
    v2 = temp2;
    v1 = temp1;
}

a和b是线段的向量。normalX和normalY是:

GLfloat normalX((b.x - a.x) / (abs(b.x - a.x)));
GLfloat normalY((b.y - a.y) / (abs(b.y - a.y)));

有什么想法可以解决这个问题吗?

这里的'a'和'b'是什么?如果您发布更多的代码,将会更容易理解。 - sinelaw
GLfloat normalX((b.x - a.x) / deltaX); GLfloat normalY((b.y - a.y) / deltaY);应该改为:GLfloat normalX((b.x - a.x) / distance); GLfloat normalY((b.y - a.y) / distance);这样你的值将会是+1或-1。 - Ray Tayek
请参考Eric在Stack Overflow上的回答: https://stackoverflow.com/questions/15367165/finding-coordinates-of-koch-curve - Sihat Afnan
2个回答

11

你的问题可能是 atan(theta)。它的定义域太小,无法用于通用角度计算。相反,你应该使用atan2(y,x)

GLfloat theta = atan2(b.y - a.y, a.x - b.x);
vec2 temp1((a.x + b.x) / 2 + dist * cos(theta) ,
           (a.y + b.y) / 2 + dist * sin(theta));

(我并没有测试这段代码,所以可能存在一些标志误差。)

阅读更多: Atan2 (维基百科)


编辑:

我的猜测是你传入的坐标顺序是错误的。如果你从起点朝着终点看一条线段,曲线将始终向右延伸。如果你按相反的顺序给出坐标,则曲线将被镜像。


你是对的!问题出在tan上,现在它完美地工作了。非常感谢! - A D
这个答案解决了我的原始问题,但是出现了一个新的问题(如我在上面的编辑中详细说明的):( - A D
谢谢你!你是对的。很有趣,一个如此简单的事情竟会引起这么多烦恼 :) - A D

1

请按逆时针顺序提供所有初始顶点,它应该按您的期望工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接