Tutorial 2: Drawing Curves (Procedural Modelling)

Axes

A set of axes centred around the origin

Linear functions

Linear function y=x

A linear function has the general form y = mx + c where m is the gradient, and c is a constant and the y intercept. A portion of the line can be drawn as a line segment between two points on the line, from point P1(x1,y1) to point P2(x2,y2). Choose two x values x1 and x2 and work out the corresponding y values y1 and y2 using the equation of the line. For the tutorial we are still using the default OpenGL viewing system which provides coordinates which range over the interval [-1,1] in both x and y.

Quadratic functions

Quadratic function y=x^2

A quadratic function of the general form y=ax2+bx+c must be drawn as a series of connected straight line segments as OpenGL does not support directly drawing curved lines (or surfaces). Sometimes this is referred to as piecewise linear approximation (and in 3D curved surfaces are approximated by flat polygons). In mathematics the straight line segments joining points on the curve are sometimes referred to as chords. The drawing can be achieved using a for loop, where each iteration of the loop draws a single line segment. The more line segments used the better the approximation and the more curved the line looks, until we can’t tell the difference more segments make (and remembering the display is actually composed of pixels). Naturally better appearance from more segments comes at the cost of more work or computation.

It may be useful to think of the above functions as parametric equations of the form:

x(t) = t, y(t) = t2

Where in this case t will be the current value you have calculated based on the number of segments.

Sine Waves

A sine wave drawn around a set of axes

Tangent Vectors

A sine wave with tangents

A tangent vector is a vector parallel to a curve (or surface) at a given point. It can be thought of pointing along the tangent line, the gradient m which can be calculated using the derivative dy/dx whereby T=<1,dy/dx>. The tangent vector can approximated by taking the average of two vectors: from the previous point to the current and the current to the next, or similarly using the average of the two gradients as an approximation to dy/dx.

Normal Vectors

A sine wave with normals and tangents

A normal vector is a vector perpendicular to a curve (or surface) at a given point. It can be calculated using the cross product of two tangent vectors (eg, one in the x direction and one in the z direction). For a simple 2d normal vector, after finding the tangent simply reverse the arguments and flip the sign of the new x value (eg tangent x, y becomes -y, x for the normal).

Approximate Numerical Approach