# Golden Ratio and Aspect Ratio

I have the following problem from Hill’s textbook about opengl 2nd edition. Draw a rectangle with the fout corners (0, 0), (0, 1), (phi, 0), (phi, 1). Take out a square on the left of size 1x1 and then the resulting rectangle is also a golden rectangle but turned 90 degrees. Take out the upper square 1/phi x 1/phi. And you are left with a golden rectangle same orientation as the first one. Now take out the square on the right instead of left, and you are left with a golden rectangle. Take out the bottom square (this fourth rectangle is like the 2nd rotated 90 degrees). And so on… According to Hill this should converge to the point of interesction of 2 diagonals (0,1)-(phi,0) of first rectangle and (phi,1)-(1,0) of second rectangle.
Basic math says this point is x=1.17082, y=0.2763932.

I wrote an opengl program to verify that and I got these 2 numbers on 2 different runs and I do not know how to interpret that, any interpretation is appreciated…

Here is the relevant part of the setup() function in the program:

``````	glViewport(0, 0, 500, 500);

lft = bttom = 0.0;
rght = 1.7;
tp = 1.7;

// Compute the MVP matrix
glm::mat4 ProjectionMatrix = glm::ortho(lft, rght, bttom, tp, -1.0, 1.0);
glm::mat4 ViewMatrix = glm::mat4(1.0f);
glm::mat4 ModelMatrix = glm::mat4(1.0f);//glm::scale(glm::mat4(1.0), glm::vec3(0.5f));
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

glUseProgram(programID);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
g_vertex_buffer_data.push_back(glm::vec2(0.0, 0.0));
g_vertex_buffer_data.push_back(glm::vec2(0.0, 1.0));
g_vertex_buffer_data.push_back(glm::vec2(phi, 0.0));
g_vertex_buffer_data.push_back(glm::vec2(phi, 1.0));
num_strips+=1;
first.push_back(0);
count.push_back(4);
split = V;

// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
``````

and here is the part of generating the rectangles:

``````int flip = 0;
while (len(g_vertex_buffer_data[g_vertex_buffer_data.size()-4], g_vertex_buffer_data[g_vertex_buffer_data.size()-1]) > 0.00005) {
//while (cc <7) {
if (!flip) {
if (split == V) {
glm::vec2 ll = g_vertex_buffer_data[g_vertex_buffer_data.size()-4];
glm::vec2 ur = g_vertex_buffer_data[g_vertex_buffer_data.size()-1];
//cerr << "V:\t" << ll[0] << ", " << ll[1]<< "::\t" << ur[0] << ", " << ur[1]<< "\n";
g_vertex_buffer_data.push_back(glm::vec2(ll[0]+(ur[0]-ll[0])/phi, ll[1]));
g_vertex_buffer_data.push_back(glm::vec2(ll[0]+(ur[0]-ll[0])/phi, ur[1]));
g_vertex_buffer_data.push_back(glm::vec2(ur[0], ll[1]));
g_vertex_buffer_data.push_back(ur);
num_strips+=1;
first.push_back(first[first.size()-1]+4);
count.push_back(4);
split = H;
}
else {
glm::vec2 ll = g_vertex_buffer_data[g_vertex_buffer_data.size()-4];
glm::vec2 ur = g_vertex_buffer_data[g_vertex_buffer_data.size()-1];
//cerr << "H:\t" << ll[0] << ", " << ll[1]<< "::\t" << ur[0] << ", " << ur[1]<< "\n";
g_vertex_buffer_data.push_back(ll);
g_vertex_buffer_data.push_back(glm::vec2(ll[0], ll[1]+(ur[1]-ll[1])*(1.0-phi1)));
g_vertex_buffer_data.push_back(glm::vec2(ur[0], ll[1]));
g_vertex_buffer_data.push_back(glm::vec2(ur[0], ll[1]+(ur[1]-ll[1])*(1.0-phi1)));
num_strips+=1;
first.push_back(first[first.size()-1]+4);
count.push_back(4);
split = V;
}
}
else {
if (split == V) {
glm::vec2 ll = g_vertex_buffer_data[g_vertex_buffer_data.size()-4];
glm::vec2 ur = g_vertex_buffer_data[g_vertex_buffer_data.size()-1];
//cerr << "V:\t" << ll[0] << ", " << ll[1]<< "::\t" << ur[0] << ", " << ur[1]<< "\n";
g_vertex_buffer_data.push_back(ll);
g_vertex_buffer_data.push_back(glm::vec2(ll[0], ur[1]));
g_vertex_buffer_data.push_back(glm::vec2(ll[0]+(ur[0]-ll[0])*phi1/(1+phi1), ll[1]));
g_vertex_buffer_data.push_back(glm::vec2(ll[0]+(ur[0]-ll[0])*phi1/(1+phi1), ur[1]));
num_strips+=1;
first.push_back(first[first.size()-1]+4);
count.push_back(4);
split = H;
}
else {
glm::vec2 ll = g_vertex_buffer_data[g_vertex_buffer_data.size()-4];
glm::vec2 ur = g_vertex_buffer_data[g_vertex_buffer_data.size()-1];
//cerr << "H:\t" << ll[0] << ", " << ll[1]<< "::\t" << ur[0] << ", " << ur[1]<< "\n";
g_vertex_buffer_data.push_back(glm::vec2(ll[0], ll[1]+(ur[1]-ll[1])/(1.0+phi)));
g_vertex_buffer_data.push_back(glm::vec2(ll[0], ur[1]));
g_vertex_buffer_data.push_back(glm::vec2(ur[0], ll[1]+(ur[1]-ll[1])/(1.0+phi)));
g_vertex_buffer_data.push_back(ur);
num_strips+=1;
first.push_back(first[first.size()-1]+4);
count.push_back(4);
split = V;
}
}
if (split == V) flip = !flip;
cc++;
}

cerr << g_vertex_buffer_data[g_vertex_buffer_data.size()-1][0] << "\t" << g_vertex_buffer_data[g_vertex_buffer_data.size()-1][1] << "\n";
// Draw the points
glBufferStorage( GL_ARRAY_BUFFER, g_vertex_buffer_data.size()*sizeof(glm::vec2), &g_vertex_buffer_data[0], 0);
``````

and the main line in display is:

glMultiDrawArrays(GL_TRIANGLE_STRIP, &first[0], &count[0], num_strips); // indices starting at 0

I forgot to mention that as is, I get the output:
1.17082 0.191016
that is the correct x.

when in the setup I flip the x’s and y’s of original rectangle and start with split = H, I get:
0.276394 0.30903
that is the correct y as x value.

that is the side that has length 1 always yield a right value. What about the other value does it have to do with the aspect ratio (dpi) of my display but it is not proportional to the proper value with the same ratio in both cases

Never mind, I caught my fault, I had the wrong coordinates calculations for y. Here are the working formulas if anybody is curious (it gives the right values for x and y:

void generate_points(void) {

``````int cc=0;
int flip = 0;
while (len(g_vertex_buffer_data[g_vertex_buffer_data.size()-4], g_vertex_buffer_data[g_vertex_buffer_data.size()-1]) > 0.00005) {
//while (cc <7) {
if (!flip) {
if (split == V) {
glm::vec2 ll = g_vertex_buffer_data[g_vertex_buffer_data.size()-4];
glm::vec2 ur = g_vertex_buffer_data[g_vertex_buffer_data.size()-1];
//cerr << "V:\t" << ll[0] << ", " << ll[1]<< "::\t" << ur[0] << ", " << ur[1]<< "\n";
g_vertex_buffer_data.push_back(glm::vec2(ll[0]+(ur[0]-ll[0])/phi, ll[1]));
g_vertex_buffer_data.push_back(glm::vec2(ll[0]+(ur[0]-ll[0])/phi, ur[1]));
g_vertex_buffer_data.push_back(glm::vec2(ur[0], ll[1]));
g_vertex_buffer_data.push_back(ur);
num_strips+=1;
first.push_back(first[first.size()-1]+4);
count.push_back(4);
split = H;
}
else {
glm::vec2 ll = g_vertex_buffer_data[g_vertex_buffer_data.size()-4];
glm::vec2 ur = g_vertex_buffer_data[g_vertex_buffer_data.size()-1];
//cerr << "H:\t" << ll[0] << ", " << ll[1]<< "::\t" << ur[0] << ", " << ur[1]<< "\n";
g_vertex_buffer_data.push_back(ll);
g_vertex_buffer_data.push_back(glm::vec2(ll[0], ll[1]+(ur[1]-ll[1])*phi1*phi1));//(1.0-phi1)));
g_vertex_buffer_data.push_back(glm::vec2(ur[0], ll[1]));
g_vertex_buffer_data.push_back(glm::vec2(ur[0], ll[1]+(ur[1]-ll[1])*phi1*phi1));//(1.0-phi1)));
num_strips+=1;
first.push_back(first[first.size()-1]+4);
count.push_back(4);
split = V;
}
}
else {
if (split == V) {
glm::vec2 ll = g_vertex_buffer_data[g_vertex_buffer_data.size()-4];
glm::vec2 ur = g_vertex_buffer_data[g_vertex_buffer_data.size()-1];
//cerr << "V:\t" << ll[0] << ", " << ll[1]<< "::\t" << ur[0] << ", " << ur[1]<< "\n";
g_vertex_buffer_data.push_back(ll);
g_vertex_buffer_data.push_back(glm::vec2(ll[0], ur[1]));
g_vertex_buffer_data.push_back(glm::vec2(ll[0]+(ur[0]-ll[0])*phi1/(1+phi1), ll[1]));
g_vertex_buffer_data.push_back(glm::vec2(ll[0]+(ur[0]-ll[0])*phi1/(1+phi1), ur[1]));
num_strips+=1;
first.push_back(first[first.size()-1]+4);
count.push_back(4);
split = H;
}
else {
glm::vec2 ll = g_vertex_buffer_data[g_vertex_buffer_data.size()-4];
glm::vec2 ur = g_vertex_buffer_data[g_vertex_buffer_data.size()-1];
//cerr << "H:\t" << ll[0] << ", " << ll[1]<< "::\t" << ur[0] << ", " << ur[1]<< "\n";
g_vertex_buffer_data.push_back(glm::vec2(ll[0], ll[1]+(ur[1]-ll[1])/phi));//(1.0+phi)));
g_vertex_buffer_data.push_back(glm::vec2(ll[0], ur[1]));
g_vertex_buffer_data.push_back(glm::vec2(ur[0], ll[1]+(ur[1]-ll[1])/phi));//(1.0+phi)));
g_vertex_buffer_data.push_back(ur);
num_strips+=1;
first.push_back(first[first.size()-1]+4);
count.push_back(4);
split = V;
}
}
if (split == V) flip = !flip;
cc++;
}

cerr << g_vertex_buffer_data[g_vertex_buffer_data.size()-1][0] << "\t" << g_vertex_buffer_data[g_vertex_buffer_data.size()-1][1] << "\n";
// Draw the points
glBufferStorage( GL_ARRAY_BUFFER, g_vertex_buffer_data.size()*sizeof(glm::vec2), &g_vertex_buffer_data[0], 0);
//glBufferStorage( GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, 0);
``````

}