GIDForums  

Go Back   GIDForums > Computer Programming Forums > OpenGL Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2007, 19:51
jlw501 jlw501 is offline
New Member
 
Join Date: Nov 2007
Posts: 3
jlw501 is on a distinguished road
Talking

Crazy math


CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>    
#include <math.h>
#define SPACE 32
#define ESCAPE 27

GLint window;       
GLint window_height;
GLint Xsize=400;
GLint Ysize=500;
int zoom=40;
int grid[100][100];
int modulus = 1;
int flag;

void modulo(){
     int i , j, temp;
     for (i=0;i<100;i++){
          for (j=0;j<100;j++){
              if ((i<modulus)&&(j<modulus)){
                  temp = i*j;
                  while(temp>0){temp-=modulus;}
                  if(temp<0){temp+=modulus;}
                  grid[i][j] = temp;
              }
              else{grid[i][j]=0;}
          }
     }
}

GLvoid Transform(GLfloat Width, GLfloat Height)
{
  glViewport(0, 0, Width, Height);              /* Set the viewport */
  glMatrixMode(GL_PROJECTION);                  /* Select the projection matrix */
  glLoadIdentity();				                /* Reset the projection matrix */
  gluPerspective(45.0,Width/Height,-100.0,100.0);  /* Calculate the aspect ratio of the window */
  glMatrixMode(GL_MODELVIEW);                   /* Switch back to the modelview matrix */
  window_height = Height;
}

GLvoid InitGL(GLfloat Width, GLfloat Height)	
{

GLfloat LightAmbient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat LightDiffuse[] = { 1.0, 1.0, 1.0, 1.0 };  
GLfloat LightPosition[] = { 100.5, 100.5, 100.5, 0.0 };

  glClearColor(1.0, 1.0, 1.0, 1.0);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_NORMALIZE);
  glEnable(GL_COLOR_MATERIAL);
  glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); 
  glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
  glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); 
  glEnable(GL_LIGHT0);                           

  Transform( Width, Height );                  
}


GLvoid ReSizeGLScene(GLint Width, GLint Height)
{
  if (Height<Ysize)    Height=Ysize;                   /* Sanity checks */
  if (Width<Xsize)     Width=Xsize;
  Transform( Width, Height );                   /* Perform the transformation */
}


static void
DrawText(GLint x, GLint y, char* s, GLfloat r, GLfloat g, GLfloat b)
{
    int lines;
    char* p;

    glMatrixMode(GL_PROJECTION);
     glPushMatrix();
     glLoadIdentity();
     glOrtho(0.0, glutGet(GLUT_WINDOW_WIDTH), 0.0, glutGet(GLUT_WINDOW_HEIGHT), -1.0, 1.0);
     glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glLoadIdentity();
      glColor3f(r,g,b);
      glRasterPos2i(x, y);
      for(p = s, lines = 0; *p; p++) {
	  if (*p == '\n') {
	      lines++;
	      glRasterPos2i(x, y-(lines*12));
	  }
	  glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *p);
      }
      glPopMatrix();
     glMatrixMode(GL_PROJECTION);
     glPopMatrix();
     glMatrixMode(GL_MODELVIEW);
}


GLvoid DrawGLScene()
{
  int i , j, k;
  char txt[7];     
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	/* Clear The Screen And The Depth Buffer */

  glPushMatrix();
  glLoadIdentity();
  gluLookAt(0,0,(90+zoom), 0, 0, 0, 0, 1, 0);
  DrawText( 5 , window_height-15 , "The primes 2 , 3 , 5 and 7  extracted from MMOD(n, (0..n-1))\nr/shift+r - toggles on/off hammings\nSpace - Increments n\n" , 0 , 0 ,  0 ); 
  snprintf( txt, 7, "n = %d", modulus );   
  DrawText( 7 , 15 , txt , 0 , 0 ,  0 );
  
     for (i=0;i<modulus;i++){
         for (j=0;j<modulus;j++){

               glPushMatrix();
               glTranslatef(((2*i)-modulus),((2*j)-modulus),-2*modulus);
               switch (grid[i][j]){
               case 0:glColor3d(1,1,1);break;
               case 1:glColor3d(1,1,1);break;
               case 2:if(!flag){glColor3d(1,0,0);} else {glColor3d(1,1,1);} break;
               case 3:if(!flag){glColor3d(1,0,0);} else {glColor3d(1,1,1);} break;
               case 5:if(!flag){glColor3d(1,0,0);} else {glColor3d(1,1,1);} break;
               case 7:if(!flag){glColor3d(1,0,0);} else {glColor3d(1,1,1);} break;
               default: if (!flag){glColor3d(1,1,1);} else {if (grid[i][j]%2==0||
                                                                grid[i][j]%3==0||
                                                                grid[i][j]%5==0)
                                                                glColor3d(0,0,1);} break;
               }

               glutSolidCube(1.0);
               glPopMatrix(); 
         }         
     }
                        
  glPopMatrix();
  glFlush(); 

}

void NormalKey(GLubyte key, GLint x, GLint y) 
{
    switch ( key )    { 
     case ESCAPE :
        printf("escape pressed. exit.\n");
	    glutDestroyWindow(window);	
	    exit(0); 		                 
     break;				
     case SPACE :
          if(modulus<100){modulus++;}else{modulus=1;}
          modulo();
     break;
     case 'r' :
           flag=1;
     break;
     case 'R' :
          flag=0;
     break;
    }
    glutPostRedisplay();
}


/*************************** Main ***************************************************************/

int main(int argc, char **argv) 
{  



  glutInit(&argc, argv);              

  glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE|GLUT_DEPTH);    
  glutInitWindowSize(Xsize,Ysize);        
  glutInitWindowPosition(0,0);        
  window = glutCreateWindow("PMMOD"); 
  InitGL(Xsize,Ysize);
  glEnable(GL_DEPTH_TEST);                     
  glEnable(GL_LIGHTING);
  glutDisplayFunc(DrawGLScene);        
  glutReshapeFunc(ReSizeGLScene);
  glutKeyboardFunc(NormalKey);         
  glutMainLoop();                      
  return 1;
}


Has anyone ever see anything like this. How would you talk about these things?
Last edited by admin : 12-Nov-2007 at 06:26. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 11-Nov-2007, 22:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Crazy math


Quote:
Originally Posted by jlw501
CPP / C++ / C Code:
  char txt[7];     
.
.
.
  snprintf( txt, 7, "n = %d", modulus );  


There's a bug here: When the modulus is equal to 100 (before the next space resets it to one) the display shows n = 10, since there isn't enough room to hold the string that would print "n = 100"

Easily fixed by changing '7' to '8' in the two statements.

Other than that, what would you like to say about it? (And, by the way, there is a brand new OpenGL Programming section on gidforums; you might post there next time, not on the C++ forum.)

Regards,

Dave
Last edited by davekw7x : 11-Nov-2007 at 23:07.
  #3  
Old 12-Nov-2007, 05:57
jlw501 jlw501 is offline
New Member
 
Join Date: Nov 2007
Posts: 3
jlw501 is on a distinguished road

Re: Crazy math


Thanks for the bug fix

I was hoping a mathematician would come across it. I don't know what you can say about it. I noticed that certain 'classes' of patterns happen in sequences, like every 4 then 8 then 4 etc in hammings, a 'crosshair' appears and things like that. I was just wondering, if I was going to write about it, even it just being an article in a blog or something, how to approach the discussion.

Cheers
  #4  
Old 12-Nov-2007, 08:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Crazy math


Quote:
Originally Posted by jlw501
I was hoping a mathematician would come across it...
Maybe one will.

Regards,

Dave
  #5  
Old 12-Nov-2007, 12:05
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Crazy math


Moved thread to OpenGL forum.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #6  
Old 13-Nov-2007, 16:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Crazy math


Quote:
Originally Posted by jlw501
I was hoping a mathematician would come across it...
Having seen no responses from mathematicians in the two days since you posted, I will make a couple of comments.

First of all, are you assuming that any old mathematician will look at your OpenGL code and know at a glance what it is doing? Well, it could happen, but I think it's not such a good idea to assume that. In other words, describe what you are try to accomplish with the program.

Before inviting someone to make comments on patterns displayed by the program, perhaps you could give a brief overview of what is being displayed (what is being projected and with what properties)? What is the difference between the "hammings" display and the "non-hammings" display? Stuff like that.

Now, from the programming point of view:

1. Why the hokey and wasteful "subtracting-and-adding-back-on" stuff in the "modulo" function?

Since, presumably, modulus is always a positive integer, couldn't you just do something like the following? (And: if not, why not?)
CPP / C++ / C Code:
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            grid[i][j] = (i * j) % modulus;
        }
    }

2. If you are going to show your program to people who may not want to get out their OpenGL reference manual, maybe you could explain exactly the purpose and functionality (the effect) of the LightXxxx and color stuff thingies in InitGL().

3. To emphasize the program's implementation of the functionality that you have already described, maybe explain exactly what happens in the program as the "modulus" gets incremented from 1 to 100? (And what difference the value of flag makes.)

4. You have a printed comment that
Code:
The primes 2 , 3 , 5 and 7 extracted from MMOD(n, (0..n-1))

In case you haven't explained it in your overview, maybe you can comment on what the heck primes have to do with it. What the heck does MMOD(whatever,(0..whatever-1) mean. Stuff like that.

Regards,

Dave
  #7  
Old 13-Nov-2007, 17:49
jlw501 jlw501 is offline
New Member
 
Join Date: Nov 2007
Posts: 3
jlw501 is on a distinguished road

Re: Crazy math


LOL, yes I did write a very bad modulo. I didn't think much about the code. I was just knocking it off so I could see what I'd been trying to see in my head all day.

The initiation function just creates a light, enable smooth shading and reflective materials. I forget what normalize does, I haven't coded glut in donkies.

Sorry, I didn't realise I was being so esoteric. MMOD is where you mod the answers to times tables. Primes I'm sure know, and hammings are (2,4,6...) union (3,6,9...) union (5,10,15...). So, you can imagine my excitement to see such a complicated pattern emerge from such simple concepts.

As you increment n, the size of the domain increases, as does the modulus and the camera zooms out. It resets at 100.

The reasoning behind it was that I was attempting to find arithmetic functions that naturally produce given patterns; sudoku I was hoping for. I cames across MMOD while reading about irrigation in acient rome. They used the pattern from MMOD(5,0..4) to do crop roations. It just so happened that this pattern fitted soduko. So I was trying to see if MMOD(9,0.. also did this, but unluckily it didn't.

Hope that clears everything up.
  #8  
Old 13-Nov-2007, 18:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Crazy math


Quote:
Originally Posted by jlw501
...the initiation function just creates....

I didn't mean for you to describe everything to me; those were suggestions for thinking about what you might put in the blog or whatever...

Just off of the top of my head...


Regards,

Dave
  #9  
Old 13-Jun-2008, 01:08
steve.john6 steve.john6 is offline
New Member
 
Join Date: Jun 2008
Posts: 1
steve.john6 is an unknown quantity at this point

Re: Crazy math


Thanks buddy for the information you have posted ..... It had really helped me a lot.......... and specially that codes for c++
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ math problem Vordican C++ Forum 5 02-Oct-2007 13:25
Math problem: Total Noob Targsmom C Programming Language 3 20-Oct-2006 06:05
C++ Help with math alex18 C++ Forum 12 25-Sep-2006 12:24
Beginning C++ math prog Dariklar C++ Forum 6 10-Sep-2003 12:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.