Topic

Surfaces and space curves

Surfaces z=f(x,y)z = f(x, y), parametric surfaces and curves in space, projected to the plane. A 3D object is computed once and kept in space coordinates; turning the camera only repeats the projection, so dragging stays fast.

Fast, then certified

surface(f, params, { x, y, grid }) is a Refinement. The quick stage is a float grid. The exact stage evaluates every vertex with interval arithmetic and reports error: each drawn height is within that distance of the true one. Vertices where ff is undefined are left out, so a surface such as 9−x2−y2\sqrt{9 - x^2 - y^2} stops at the edge of its domain.

const cam = camera({ azimuth: 35, elevation: 25 });
const s = surface("sin(x)*cos(y)", {}, { x: [-3, 3], y: [-3, 3], grid: 40 });
const vp = spaceViewport(600, 450);
drawScene(ctx, vp, [project3d(cam, [s.quick.value], { axes: true })]);   // at once
drawScene(ctx, vp, [project3d(cam, [s.exact()], { axes: true })]);       // certified
s.exact().error;                                                         // about 1e-14

Cameras

camera({ azimuth, elevation, distance, zoom }) looks at the centre of the scene. distance: Infinity, the default, is a parallel projection; a finite distance gives perspective. orbit(cam, dx, dy) is the camera after a drag of that many pixels. The scene's box is drawn as a cube whatever its three ranges are, and spaceViewport(width, height) is a viewport the cube fits at every angle.

Curves, points and arrows

spaceCurve([x, y, z], params, { t }) and parametricSurface([x, y, z], params, { u, v }) work the same way: a float sketch, then certified points. points3d, segment3d and arrow3d add marks. tangentPlane(f, [x0, y0]) returns the plane as a small patch with the point of contact and the normal arrow.

One frame for every exporter

project3d(camera, items, { box, axes, light }) turns any mix of items into one mesh frame: faces and strokes in plane coordinates, sorted from far to near. It is an ordinary layer, so the same picture goes to canvas, SVG, TikZ, PDF, PNG and text. Styles per item: color, opacity, wire, shading: "height".

Try itEdit the code and run it with @ratmath/graphics and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math, and SHOW(svg) draws a picture.
Ctrl/⌘ + Enter runs