3dCAD: functions

After completing the first 6 chapters of Eloquent Javascript and beginning to wrap my mind around functional programming, I decided to take a first stab at defining functions, embedded below in a gist (I’ll add it to a file in my github project later today). These functions correlate directly to the operations listed in the mockups I put up in an earlier post. One advantage of doing it this way is that having one’s CAD objects as code, you can have an object tree and use tools developed for code, such as git for revision control.

In the process of doing so, I had the idea to use equations to define curved lines/surfaces. This idea assumes that the equation code converts the equation to short line segments (if 2D) or triangles (if 3D). The line length/triangle size would be determined on the fly by the zoom level and perhaps rendering resolution. I’m not sure how hard it would be to write such code, but I suspect that it’s far from trivial.

Eventually, I’ll also need to think about making areas/volumes vs. collection of lines/surfaces. In 3D geometry, that’s easy: for a sphere surface of radius 1, you would have (x-1)^2 + (y-1)^2 + (z-1)^2 = r^2, and for a solid sphere of radius 1, you would have (x-1)^2 + (y-1)^2 + (z-1)^2 < r^2. But in an actual CAD program, I’m not sure how easy this would be.

Not being a 3D coding guy, I need advice: how far is this from actual working code? I can imagine the code that needs to go in all the functions, but not how to bring the whole thing to working code. I will be looking at 3D libraries linked to earlier to see if I can figure it out myself.


Tags: 3dCAD