APPENDIX: Why using 3D and How
Although visualizing a problem in 2D gives us a
decent overview,
adding a 3rd dimension makes it sometimes clearer.
Most of
us have no problem drawing accurate points in 2D.
But when
it comes to drawing points in 3D, it becomes very
hard to
do it manually on a piece of paper. In order to see
the depth,
we want to be able a rotate around the points to see
where
they are in the 3D space which is simply impossible
on a piece
of paper. Therefore, we believe that adding the 3rd
dimension
in a program such as this one really can gives us a
better
feel of a specific problem.
In order to draw points in a 3D environment, we
decided
to use OpenGL since it is very standard cross
platform 3D
API and one of us was already very familiar with it.
There
are many implementations of OpenGL for Java but most
of them
require that the user installs some library on the
client
computer to allow the 3D program to be accelerated
by a potential
3D card. But if an applet requires to install some
program,
most users will ignore the applet because they don't
want to
spend 30 minutes downloading and installing some
program to
run an applet for just a few moments.
We are using JavaGL
(jGL) created by Robin Bing-Yu Chen which is a
software
implementation (no 3D card acceleration) of OpenGL
in Java.
An applet using this library will work on most
computers and
doesn't require any other installation. One cannot
create
a large 3D world because it would be too slow, but
it is perfect
to draw 3D points to show mathematical functions in
3D or to
show our point set matching in 3D.
jGL support a large number of OpenGL functions, you
may see which
function are supported her
e.
Setting up jGL
OpenGL is quite simple to setup. You may even
setup the
window using openGL UTilities (GLUT) in platform
independent
way.
//set the background color to white
myGL.glClearColor (1.0f, 1.0f, 1.0f, 0.0f);
//set the shading model to flat (when drawing
triangle, you
will see every face of an object
//as a flat triangle
myGL.glShadeModel (GL.GL_FLAT);
//setting the size of the rectangle in which opengl
will draw
objects
myGL.glViewport (0, 0, w, h);
myGL.glMatrixMode (GL.GL_PROJECTION);
myGL.glLoadIdentity ();
//Now we set the projection type, since we are
drawing 3D
points, we want orthogonal projection.
//Parallel lines look parallel even in 3D.
myGL.glOrtho (-50.0f, 50.0f, -50.0f, 50.0f, -100.0f,
100.0f);//this
sets an orthogonal view (not perspective) ;
Drawing points, lines or
triangles
//first we place the "camera" so that
we look
at the origin and rotate around it by modifying
the
//variable tilt and spin
myGL.glRotatef (tilt, 1.0f, 0.0f, 0.0f); //tilt the
points
up or down
myGL.glRotatef (spin, 0.0f, 1.0f, 0.0f); //rotate
the points
from aroud the height axis
myGL.glTranslatef(0, , -distanceFromCenter);
//all the following points will be red. values
are (R,G,B)
between 0 and 1
myGL.glColor3f (0.8f, 0.0f, 0.0f);
myGL.glPointSize(3); //set the size of the points
myGL.glBegin(GL.GL_POINTS); //draw points
You may also draw lines or triangle by using
myGL.glBegin(GL.GL_LINES); //draw
lines
or
myGL.glBegin(GL.GL_TRIANGLES); //draw
triangles