Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:01:29+00:00 2026-06-07T07:01:29+00:00

I am trying to implement Koch’s Snowflake. I have made a generic list, for

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:01:31+00:00Added an answer on June 7, 2026 at 7:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement AVAudioplayer and get some metering data of the played music, but
Just trying to implement mobFox into my app, but having trouble to make it
Im trying to implement a destructor for the objects of linked-list Iv created. I
I trying to implement some relatively simple 2D sprite batching in OpenGL ES 2.0
Trying to implement some nested loops that are spitting out good old nested html
Trying to implement a function that will return a list of ints the represent
Trying to implement NSCopying for the first time, and I have a question about
I have this weird kind of error. I am trying implement basic Euclidean algorithm
Trying to implement what I thought was a simple concept. I have a user
Im trying to implement some code i found on a website which duplicates a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.