Don’t be put off by the title!

I’ve uploaded a new project to http://www.math.ntnu.no/~stacey/HowDidIDoThat/iPad/Codea.html: the **cube** project. It displays a cube defined by vertices and edges. Main features:

- The cube is stereographically projected onto the screen from an “eye” above the screen.
- The elements are ordered according to how far from the eye they are and placed on the screen in the right order to create the 3D effect (a little shading, using a technique from TikZ/PGF, ensures that over/under looks right).
- As you tilt the iPad, the cube “stays level” in that it adjusts so that the bottom face always appears to be downwards. So if you tilt the iPad up, you look at the top of the cube.
- You can rotate the cube by touching and dragging on the screen. You should imagine a sphere containing the cube and as you drag your finger across the screen, you rotate this sphere. (Then if you further tilt the iPad, it keeps the current orientation relative to the ground.)

Main code features:

- An extended
`Vec3`

class for holding 3-dimensional vectors. I figured out how to make the`+`

and so forth operators work, so things like`u + v`

*just work*. I had to be a*bit*creative:`u * v`

returns the cross product,`u .. v`

the dot product, and`#v`

the length. It also has a crude`applyMatrix`

function where the matrix is specified as a triple of`Vec3`

objects. Many of the operations work with`vec3`

objects as well. It has functions for stereographic projection of vectors onto the screen from an “eye”. - A class
`Quaternion`

for dealing with quaternions. “*What’s a quaternion?*”, I hear you cry. If you don’t know then think of them as a neat way of encoding rotations. You can apply a quaternion (rotation) to a`Vec3`

object by the notation`v^q`

. Composing rotations corresponds to multiplying quaternions:`q * p`

.

Things to do:

- The vertical stuff is done by ordering the elements according to their distance from the “eye”. I’d planned on using
`zLevel`

for this but couldn’t get it to work as I expected so went for a manual sort. Now that I know a bit more about`zLevel`

I could put this back (maybe). - The vertical stuff can’t cope with this problem: (image included, with the permission of the author, from this blog post which is about something a little similar). This would need more advanced drawing tools, or extremely finickity computation.
- The radius of the “balls” for the vertices varies with the depth, but the calculation is a little crude. This could be improved.
- Adding support for pinch-to-zoom would be super cool!
- The stereographic projection doesn’t do anything sensible with points behind the viewer. This should be possible, though there are a few edge cases to consider.
- Defining the data is fairly abstract, but could be made more so with a 3DObject class.
- The vertices themselves don’t look very 3D.

Other suggestions welcome!