0

I am trying to implement Koch's Snowflake. I have made a generic list, for practice sake, but I am having some issues.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gl/glut.h>


template <typename T> class Node {
public:
    T data;
    Node<T> *next;
    Node<T>(T d) {
        next = NULL;
        data = d;
    }
};
template <typename T> class List {
    Node<T> *head;
    int size;
public:
    List() {
        head = NULL;
        size = 0;
    }
    void append(T data){
        if(head == NULL) {
            head = new Node<T>(data);
        } else {
            Node<T> *n = head;
            while( n->next != NULL ) {
                n = n->next;
            }
            n->next = new Node<T>(data);
        }
        size++;
    }
    void appendAll(List<T> data) {
        if(data.getHead() == NULL)
            return;
        Node<T> *n = data.getHead();
        append(n->data);
        while(n->next != NULL){
            append(n->next->data);
            n = n->next;
        }   
    }
    Node<T>* getHead(){ return head; }
};
void myinit();
void display();
void draw_snowflake();
List<GLfloat[2]> divide_snowflake(GLfloat A[2], GLfloat B[2], int n);

GLfloat tri[3][2] = {{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}};
List<GLfloat[2]> snow;
int n;


int main(int argc, char **argv) {
    n = 0;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutCreateWindow("Koch Snowflake");
    glutDisplayFunc(display);
    myinit();
    glutMainLoop();

    return EXIT_SUCCESS;
}

void myinit(){
    // Initialize OpenGL
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
    glMatrixMode(GL_MODELVIEW);
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glColor3f(0.0,0.0,0.0);

    // Initialize list of line_loop
    snow.append(tri[0]);
    snow.append(tri[1]);
    snow.append(tri[2]);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_LINE_LOOP);
    draw_snowflake();
    glEnd();
    glFlush();
}

void draw_snowflake(){
    List<GLfloat[2]> temp;
    temp.append(snow.getHead()->data);
    Node<GLfloat[2]> *curr = snow.getHead();
    while(curr->next != NULL) {
        temp.appendAll(divide_snowflake(curr->data, curr->next->data, n));
        temp.append(curr->next->data);
        curr = curr->next;
    }
    temp.appendAll(divide_snowflake(curr->data, snow.getHead()->data, n));

    Node<GLfloat[2]> *ptr = temp.getHead();
    printf("\n>Drawing %f, %f", ptr->data[0], ptr->data[1]);
    glVertex2fv(ptr->data);
    while(ptr->next != NULL) {
        printf("\n>Drawing %f, %f", ptr->next->data[0], ptr->next->data[1]);
        glVertex2fv(ptr->next->data);
        ptr = ptr->next;
    }
}

List<GLfloat[2]> divide_snowflake(GLfloat A[2], GLfloat B[2], int n) {
    GLfloat A_Mid[2] = {A[0] + (B[0] - A[0]) / 3,
                        A[1] + (B[1] - A[1]) / 3};
    GLfloat Mid[2] = {A[0] + (B[0] - A[0]) / 2,
                      A[1] + (B[1] - A[1]) / 2};
    GLfloat B_Mid[2] = {B[0] - (B[0] - A[0]) / 3,
                        B[1] - (B[1] - A[1]) / 3};
    GLfloat Peak[2] = {Mid[0] + (Mid[1] - B_Mid[1]) * sqrt(3.0),
                       Mid[1] + (Mid[0] - A_Mid[0]) * sqrt(3.0)};

    List<GLfloat[2]> temp;
    if(n > 0) temp.appendAll(divide_snowflake(A, A_Mid, n-1));
    temp.append(A_Mid);
    if(n > 0) temp.appendAll(divide_snowflake(A_Mid, Peak, n-1));
    temp.append(Peak);
    if(n > 0) temp.appendAll(divide_snowflake(Peak, B_Mid, n-1));
    temp.append(B_Mid);
    if(n > 0) temp.appendAll(divide_snowflake(B_Mid, B, n-1));
    return temp;
}

Here is the error I am getting:

Error 1 error C2440: '=' : cannot convert from 'GLfloat []' to 'float [2]'  13

When I was just initializing as List<GLfloat*> it would only set node's data as a single value; whereas, I want points. For practice purposes, I want to continue to use a generic list.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 2
    Why don't you use `std::list`? It's a generic list, and it works. – mfontanini Jul 10 '12 at 22:48
  • @mfontanini: It says at the very top "for practice". If every programmer defaulted to libraries from day 1 who would be skilled enough to continue writing libraries in the future? – Ed S. Jul 10 '12 at 22:49
  • @EdS. oh, I always miss that :/. Yes, I've created my own structures too. I just thought he was creating a list only because he wanted a "generic list". – mfontanini Jul 10 '12 at 22:49
  • It would make more sense to write the list and the Koch Snowflake separately, though, and then put them together once at least the list was bug free. As it stands, more than half of this code is irrelevant. – Cactus Golov Jul 10 '12 at 23:56

1 Answers1

1

Let's consider what the code would be if you used a non-generic list: namely, a list that worked with GLFloat[2]s. Here's your node code:

class Node {
public:
    GLFloat[2] data;
    Node *next;
    Node(GLFloat[2] d) {
        next = NULL;
        data = d;
    }
};

An important think to note now is that Node's constructor doesn't actually take an array: it takes a GLFloat*. That's just the way C++ works in this regard. (Strangely to me, this is also the way it works when you let the argument type depend on a template parameter: apparently, an array is also treated as a pointer then.)

You are now trying, by doing data = d;, to assign a GLFloat* to a GLFloat[2]. This isn't anything meaningful: you can't just take a pointer and put its value into an array. You could explicitly put d[0] into data[0] and d[1] into data[1], but this wouldn't be very general, as your list would no longer work with non-array types.

What can be done? Well, one solution would be to specialise your list for arrays. The code would look something like...

template <typename T, std::size_t N>
class Node<T[N]> {
public:
    T data;
    Node<T> *next;
    Node<T>(T d) {
        next = NULL;
        for (std::size_t i = 0; i < N; ++i)
            data[i] = d[i];
    }
};

However, this may lead to a lot of code duplication, and leads to further issues like how you can return elements.

My advice would be to use std::pair<GLFloat, GLFloat> instead. This should work with your list as it is. Alternatively, you can use std::array if you want to store an arbitrary number of elements.

And, while I'm commenting on all this anyway: you may want to move the Node class template into the List class template, as it is an implementation detail. You also should definitely not be providing a getHead() function -- it does not make sense to break encapsulation like that. Provide an iterator class and a pair of begin() and end() functions for iteration. One possible implementation would be along the lines of:

struct iterator {
    friend List;
  private:
    Node* current;
    iterator(Node* c) : current(c) {}
  public:
    iterator& operator++() {
        current = current->next;
        return *this;
    }

    friend bool operator==(iterator const& lhs, iterator const& rhs) {
        return lhs.current == rhs.current;
    }

    T& operator*() {
        return current->data;
    }

    T* operator->() {
        return &current->data;
    }
};

This would provide a sufficient interface for everything you've done so far, and would be much cleaner. Implementing a more complete interface unfortunately requires a lot of boilerplate; at that point, you may wish to switch to a simple std::list.

Cactus Golov
  • 3,474
  • 1
  • 21
  • 41
  • Note that `T& d` would work, resulting in `T = GLfloat[2]`. That's because _arrays_ don't have value semantics (they can't be _copied_). – K-ballo Jul 11 '12 at 00:26
  • `T& d` would mean that the `Node` constructor would take an array by reference, but assigning it would still not work. – Cactus Golov Jul 11 '12 at 00:39