Okay, first off, I know my code is atrocious. I am not a c++ programmer, but my OpenGL course requires it. My lack of c++ knowledge is most probably my biggest problem here. I am simply looking for quick fixes to my ignorant mistakes. I just need the algorithm to work so I can send it to OpenGL.

The code is to create a koch snowflake iteratively. I am not allowed to use recursion. This work is due tomorrow, so I am a little too deep to completely change my code(unless it is necessary of course). I have been a member here for a while, and I trust you guys to see things I dont.

I have pretty much narrowed down the bulk of the problem to

mylist.insert(it,returned[0][0]);
		mylist.insert(it,returned[0][1]);
		mylist.insert(it,returned[1][0]);
		mylist.insert(it,returned[1][1]);
		mylist.insert(it,returned[2][0]);
		mylist.insert(it,returned[2][1]);

Thanks in advance. And, as always, if you have any questions about my code or methods, just ask!

// Two-Dimensional Koch Snowflake    

#include "Angel.h"
#include "math.h"
#include <list>
#include <vector>
#include <iostream>
using namespace std;

vector<GLfloat> mylist;
vector<GLfloat>::iterator it;//,current,next;
GLfloat returnMe[3][2];
int current = 0;
int next =2;
const int splitNum = 2;
const int totalNew = 3*4*4*4; //add '4' for more splits. 4's should equal # of splitNum +1
vec2 points[totalNew];
int pointsPlace =0;

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


void swap(GLfloat * a, GLfloat * b) //switch the values
{
	GLfloat temp = *a;
	*a = *b;
	*b = temp;
}

void divideSnowflake(GLfloat *a, GLfloat *b,int n){
	
	
	
	int x =0;
	int y =1;
	GLfloat t[2][2]; //divide points [1][x] and [1][y] are the 1/3 mark and [2][x,y] = 2/3
	GLfloat slope[2]; //[x]and[y]
	GLfloat midpoint[2];//[x] and [y]
	GLfloat snowflake_point[2];//[x]and [y]	GLfloat y_magnitude;
	//GLfloat tempX;
	//GLfloat tempY;
	GLfloat y_magnitude;
	int j;
	
		/* Using Pythagorus */
		/* Special triangle has ratio of 1:sqrt(3):2 */
		y_magnitude = sqrt(3.0);
		/* Find a point a third of the way along the line */
		for ( j = 0; j < 2; j++) t[0][j] = a[j] +( b[j] - a[j]) /3.0;
		/* Find a point two thirds of the way along the line */
		for ( j = 0; j < 2; j++) t[1][j] = a[j] +( b[j] - a[j]) /3.0 * 2.0 ;
		/* Find the midpoint */
		for ( j = 0; j < 2; j++) midpoint[j] = a[j] +( b[j] - a[j]) / 2.0 ;
		/* Find the vector for the relative base of the triangle */
		for ( j = 0; j < 2; j++) slope[j] = (midpoint[j] - t[0] [j]); /* vector of slope */
		/* I know this is the relative unit vector as this is a case of
		 the special triangle 1:sqrt(3):2 */
		swap(&slope[x],&slope[y]); /* normal vector with outward
									direction is required. In order to
									find that I am required to swap the
									dx,dy of the vector and then set the
									dx to -dx.*/
		slope[x] = -slope[x];
		/* Since I am working with the special triangle 1:sqrt(3):2 I
		 know that relatively the magnitude of the vector will be
		 sqrt(3) * the unit vector. I add the vector to the midpoint
		 to locate the x,y position of the tip of the new koch
		 snowflake point. */
		for ( j = 0; j < 2; j++) snowflake_point[j] = midpoint[j] + y_magnitude * slope[j]; /* locate point */
		
		
		returnMe[0][0]=t[0][0];
		returnMe[0][1]=t[0][1];
		returnMe[1][0]=snowflake_point[0];
		returnMe[1][1]=snowflake_point[1];
		returnMe[2][0]=t[1][0];
		returnMe[2][1]=t[1][1];
		/*
			points[pointsPlace]= points[pointsPlace-1];
			pointsPlace++;
			points[pointsPlace]= vec2 (a[0],a[1]);
			pointsPlace++;
			points[pointsPlace]= vec2 (a[0],a[1]);
			pointsPlace++;
			points[pointsPlace]= vec2 (b[0],b[1]);
			pointsPlace++; */
		
}

void koch(GLfloat *a, GLfloat *b, GLfloat *c,int n){
	int done=0;
	GLfloat *returned[3];
	cout << "here1" << endl;
	while (done!=splitNum) {
		advance(it,current);
		a[0]=mylist[current];
		it++;
		a[1]=mylist[current+1];
		it++;
		b[0] =mylist[next];
		//it++;
		b[1]=mylist[next++];
		//it++;
		divideSnowflake(a,b,n);
		mylist.insert(it,returned[0][0]);
		mylist.insert(it,returned[0][1]);
		mylist.insert(it,returned[1][0]);
		mylist.insert(it,returned[1][1]);
		mylist.insert(it,returned[2][0]);
		mylist.insert(it,returned[2][1]);
		it++;
		advance(it,next);
		it++;
		cout << "here";
		if( it== mylist.end()){
			current=next;
			next=0;
			done++;
		}
		else{
			current = next;
			next = next+2;
		}
		
		
	}
	
}

void
init( void )
{
    GLfloat side = 0.5;
	GLfloat xstart = 0.0;
	GLfloat ystart = 0.0;
    // Specifiy the vertices for a triangle
    vec2 vertices[3] = {
        vec2( -1.0, -1.0 ), vec2( 0.0, 1.0 ), vec2( 1.0, -1.0 )
    };
	GLfloat vertex[3][2] = {{xstart,ystart},{xstart + side/2.0, ystart + side/2.0*sqrt(3)},{xstart + side , ystart}};
	
	mylist.push_back(vertex[0][0]);
	mylist.push_back(vertex[0][1]);
	mylist.push_back(vertex[1][0]);
	mylist.push_back(vertex[1][1]);
	mylist.push_back(vertex[2][0]);
	mylist.push_back(vertex[2][1]);
	
	koch(vertex[0], vertex[1], vertex[2], splitNum);
	
    
    // Create a vertex array object
    GLuint vao[1];
    glGenVertexArraysAPPLE( 1, vao );
    glBindVertexArrayAPPLE( vao[0] );
    

    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    
	glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );

    // Load shaders and use the resulting shader program
    GLuint program = InitShader( "vshader21.glsl", "fshader21.glsl" );
    glUseProgram( program );

    // Initialize the vertex position attribute from the vertex shader
    GLuint loc = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( loc );
    glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0,
                           BUFFER_OFFSET(0) );

    glClearColor( 0.5, 1.0, 1.0, 1.0 ); // white background
}

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

void
display( void )
{
    glClear( GL_COLOR_BUFFER_BIT );     // clear the window
    glDrawArrays( GL_LINES, 0, totalNew);    // draw the points
    glFlush();
}

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

void
keyboard( unsigned char key, int x, int y )
{
    switch ( key ) {
    case 033:
        exit( EXIT_SUCCESS );
        break;
    }
}

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

int
main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA );
    glutInitWindowSize( 512, 512 );

    glutCreateWindow( "KOch Snowflake" );

    init();

    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );

    glutMainLoop();
    return 0;
}

Edited 5 Years Ago by leftovas17: -found problem section

I have gotten nowhere on this. My last attempt to run the program resulted in a computer crash. The problem is coming from lines 105-110 most likely. Since that is where I stop getting output when i insert test messages via cout.

Any help would be appreciate. Thank you!

So, i changed all the

mylist.insert(it,returnMe[x][x];

to

mylist.insert(it, 1, returnMe[x][x]);

which seems to work. However, I can only do 3 inserts of any value before I get the error:

dlucas_hw2_iter(1776,0x7fff7003dcc0) malloc: *** error for object 0x12390a9f8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap

within the terminal. I have no Idea why the 4th insert causes this error. And it causes it no matter the value inserted.

This article has been dead for over six months. Start a new discussion instead.