teaching machines

Example case studies

January 16, 2012 by . Filed under mobile.

OpenGL Example

Installation needs:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

/* ------------------------------------------------------------------------- */

int width = 512;
int height = 512;
float theta = 0.0f;

/* ------------------------------------------------------------------------- */

void display(void) {
  glViewport(0, 0, width, height);

  glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT);

  glColor3f(0.0f, 0.0f, 0.0f);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(theta, 1.0f, 0.0f, 0.0f);
  glutWireTorus(0.3f, 0.7f, 10, 10);

  glutSwapBuffers();
}

/* ------------------------------------------------------------------------- */

void resize_window(int new_width, int new_height) {
  width = new_width;
  height = new_height;
}

/* ------------------------------------------------------------------------- */

void special_keys_handler(int key, int x, int y) {
  switch (key) {
    case GLUT_KEY_UP:
      theta += 5.0f;
      break;
    case GLUT_KEY_DOWN:
      theta -= 5.0f;
      break;
    default:
      break;
  }
  glutPostRedisplay();
}

/* ------------------------------------------------------------------------- */

int main(int argc, char **argv) {
  glutInitDisplayString("alpha rgba double");
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  glutInitWindowSize(width, height);
  glutInit(&argc, argv);

  glutCreateWindow("3-D Application");
  glutDisplayFunc(display);
  glutReshapeFunc(resize_window);
  glutSpecialFunc(special_keys_handler);

  glutMainLoop();

  return 0;
}

/* ------------------------------------------------------------------------- */

Android example

Installation needs:

Code:

public class Randaurant extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    TextView label = (TextView) findViewById(R.id.destination);
    label.setText(getRandomRestaurant());
    return true;
  }
  
  private static String getRandomRestaurant() {
    int random = new Random().nextInt(100) + 1;
    if (random < 10) {
      return "Hooligan's";
    } else if (random < 50) {
      return "Acoustic Cafe";
    } else {
      return "Fuji Steakhouse";
    }
  }
}