# 1/30/04

I showed another example of the gasket program where we made the gasket through recursive division of the edges of the triangles by half and connected the midpoints. Here is the new version of the gasket program:

/* Recursive subdivision of triangle to form Sierpinski gasket */

//This is the program from the text-book with a very little modification by RT

#include <GL/glut.h>

typedef float point2[2];

/* initial triangle */
//point2 v[]={{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}};
point2 v[3]={{0, 0}, {250, 500}, {500.0, 0}};

int n;

void triangle( point2 a, point2 b, point2 c)

/* display one triangle */
{
glBegin(GL_TRIANGLES);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
glEnd();
}

void divide_triangle(point2 a, point2 b, point2 c, int m)
{

/* triangle subdivision using vertex numbers */

point2 v0, v1, v2;
int j;
if(m>0)
{
for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
divide_triangle(a, v0, v1, m-1);
divide_triangle(c, v1, v2, m-1);
divide_triangle(b, v2, v0, m-1);
}
else(triangle(a,b,c)); /* draw triangle at end of recursion */
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
divide_triangle(v[0], v[1], v[2], n);
glFlush();
}

void myinit()
{
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 500, 0, 500);

glMatrixMode(GL_MODELVIEW);
glClearColor (0, 0.0, 1.0, 1.0);
glColor3f(1.0,1.0,1.0);
}

int main(int argc, char **argv)
{
n = 1; /* number of iterations */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 500);
glutDisplayFunc(display);
myinit();
glutMainLoop();

return 0;
}

if you run this program you will get a screen that looks like this:

We talked about the background necessary to complete assignment 2. In particular, you need to know how to rotate a point with respect to a coordinate system, or to rotate the coordinate axes with respect to the point. We started with the cosine and sine sum formulas: cos(a + b) = cos(a)cos(b) - sin(a)sin(b), sin(a+b) = sin(a)cos(b) + sin(b)cos(a). Then we derived the formulas for rotating the x and y axes through an angle t and calculating the new coordinates of a point (x,y). Here's the result:

` x' =   cos(t)*x  +  sin(t)*y y' =  -sin(t)*x  +  cos(t)*y`

If we wish to rotate the point instead of the axes (this is what we usually do and you need for the assignment 2), we get

It's all a matter of perspective: if we rotate the axes towards the point, then it's a positive rotation from the axes' perspective but a negative one from the point's perspective. Translation is similar to rotation: I can describe moving an object away from a viewer as positive movement of the object or a negative movement of the viewer.

We discussed the translation and scale transformations. Scaling can be described:

And translation:

We discussed the recursion which you will have to use to draw the Koch Snowflake in assignment 2. Recursion is a powerful technique which shows up often in graphics. Ray tracing is an example of how a little code gets powerful leverage with recursion. We looked at the recursive code for drawing the Sierpinski Gasket more efficiently than with the iterative approach we took earlier. The code is described in section 2.8 of our text. This is where you should be in the reading.