Jump to content


- - - - -

Sierpinsky Triangle Fractal ....Not too fractal


4 replies to this topic

#1 Chococat

    New Member

  • Members
  • Pip
  • 2 posts

Posted 11 April 2008 - 08:16 AM

I Want to draw the Sierpinsky Triangle Fractal

I followed Wikipedia article with the called "chaos" game

Quote

.....Or more simply:
1. Take 3 points in a plane, and form a triangle
2. Randomly select any point inside the triangle and move half the distance from that point to any of the 3 vertex points. Plot the current position.
3. Repeat from step 2.
http://en.wikipedia....pinski_triangle

when running the program i get something like this:
Posted Image

whats bad with it?

the drawing code is:
#include <stdlib.h>
#include <gl/gl.h>
#include <math.h>
#include "sierpin.h"

point Sierpin::GetRandPoint() {
	  /*Thanks to the people that helped
	  in the thread of the
	  uniform random point inside a triangle problem at:
	  http://www.devmaster.net/forums/showthread.php?t=10469
	  */
		float a,b;
	    do {
		 a=((float) rand()) / ((float)RAND_MAX);
		 b=((float) rand()) / ((float)RAND_MAX);
	 	} while ((a+:) >1);

		float c=1-a-b;
		return point( (points[0]*a + points[1]*b + points[2]*c));
};

void Sierpin::Draw(void) {
	 glColor3ub(255,100,0); // pen color to orange vomit
	 
	 int sel=rand() % 3; // get a random number between 0 and 2
	 point p=points[sel]; // saving a random vertex of the three possible of the triangle
	 point t=GetRandPoint();//gettin a valid random point in the triangle
	 
	 for (int k=0; k < iter; k++) {
	 glBegin(GL_POINTS);
		 //draw the point between the random trangle vertex and
		 //the random inner point
		 glVertex2f((t.x+p.x)/2,(t.y+p.y)/2);
	 glEnd();
	 //getting new points
	 t=GetRandPoint();
	 sel=rand() % 3;
	 p=points[sel];
	 //and repeat
	 };

};

int Sierpin::iter = 8000; //

thanks...

#2 marek-knows.com

    Valued Member

  • Members
  • PipPipPip
  • 189 posts
  • LocationOntario, Canada

Posted 11 April 2008 - 11:52 AM

I didn't look at your code very closely but I think I know what may be happening.

If you are drawing all your triangles one on top of the another then you are going to cause the triangles to "fight". This means your video card will need to figure out which triangle is on top of what and you will get strange rendering.

Might I suggest this. Draw the first set of triangles that are of a specific size. Then when you draw the next set of triangles that are of a smaller size, offset them just a little in the z direction so they are not laying right on top of the previous one.

does this help?
3D OpenGL, C++ Game Development Video Tutorials @
www.marek-knows.com

#3 marek-knows.com

    Valued Member

  • Members
  • PipPipPip
  • 189 posts
  • LocationOntario, Canada

Posted 11 April 2008 - 11:54 AM

Oh wait a minute, you are not drawing triangles, you are drawing points:

GL_POINTS

What do you get when you lower iter value to something small like 2 or 3?
3D OpenGL, C++ Game Development Video Tutorials @
www.marek-knows.com

#4 hovermonkey

    Member

  • Members
  • PipPip
  • 38 posts

Posted 11 April 2008 - 02:03 PM

I think Wikipedia is wrong in this case. AFAIK it should say this:

1. take 3 points in a plane and form a triangle.
2. Randomly select any point inside the triangle.
3. move half the distance from that point to any of the 3 vertex points. Plot the current position.
4. repeat from step 3, not 2, moving from the point you just plotted.

see http://en.wikipedia....wiki/Chaos_game as a generalization of this algorithm.

#5 Chococat

    New Member

  • Members
  • Pip
  • 2 posts

Posted 11 April 2008 - 09:32 PM

Thank you hovermonkey.
I should dont have blind confidence on wikipedia articles.

But i also i think i didnt read the algorithm correctly in first place.

Posted Image

The correct code is:
void Sierpin::Draw(void) {
	 glColor3ub(255,100,0); // pen color to orange vomit
	 
	 int sel=rand() % 3; // get a random number between 0 and 2
	 point p=points[sel]; // saving a random vertex of the three possible of the triangle
	 point t=GetRandPoint();//gettin a valid random point in the triangle
	 
	 for (int k=0; k < iter; k++) {
	 glBegin(GL_POINTS);
		 //draw the point between the random trangle vertex and
		 //the random inner point
		 glVertex2f((t.x+p.x)/2,(t.y+p.y)/2);
	 glEnd();
	 //getting new points MODIFIED
	 t.x=(t.x+p.x)/2; //saving the point x coord
	 t.y=(t.y+p.y)/2; // saving the point  y coord
	 sel=rand() % 3;
	 p=points[sel];
	 //and repeat
	 };

};






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users