Home Page Home Page
Curves Plotting Techniques



Feedback

The following page is extracted from the Turbo C++ Graphics Tutor Copyright © Digital International Ltd 1997.
Note : for any additional information concerning this page & the source code included, download the Turbo C++ or Turbo Pascal Graphics Tutor free.

A screen shot from the Turbo Pascal Graphics Tutor :-

Click to enlarge

Contents of this page :-

  1. Introduction.
  2. Curves Tracing in Cartesian Coordinates (2D).
  3. Curves Tracing in Polar Coordinates (2D).
  4. Curves Tracing in Parametric Coordinates (2D).
  5. Lissajous Figures.
  6. Curves Tracing in 3D.

Introduction :

Long ago plotting curves and investigating their behaveour was my interest, and I'm sure that in one situation or another you will need to plot a curve using a programming language, here I have presented some techniques that you may find useful.

Curves tracing using computer :

One of the most important applications in math is the science of curves, since it has many applications in real life. so many programs were developed to plot curves in various coordinates and dimensions.

Curves plotting techniques :

Curves are drawn by computer using the following technique, first we have to calculate Y from the relation :

Y = F(X)
it must be in that form, so we calculate a value for Y using a known value of X, say X1 and the resulting value of Y calculated is Y1. We will use the procedure putpixel(x1,y1,color) so to draw a complete curve we have to put this procedure in a loop where we can compute many values for Y using different values of X, say X1,X2,..,Xn.

Note that you have many choices of how to plot a curve, for example you can use:

Curves tracing in cartesian coordinates (2 Dimension) :

A Sin Curve

Here we are going to sketch the curve in equation (1) step by step. first

Y = SIN X ---------(1)
we have to determine the screen coordinates and origin, look at the images shown below

Origin of Screen Virtual Origin of Screen

the origin of the screen is at the upper left corner, but we want it in the center of the screen, so let us do some work. Define

midx = getmmaxx() / 2
midy = getmaxy(() / 2
and assume we want to sketch the values XPlot and YPlot, so we will write :
y = sin(x);
xplot = midx + x * xfactor; ------------(2)
xplot = midy + y * yfactor; ------------(3)
by equation (2) and (3) we locate the origin at the center of our screen, since when x and y equal zero we get
xplot = midx;
yplot = midy;
and this is the origin. The variables (xfactor & yfactor) are factors, since the value of y calculated from equation (1) will range from (-1 to 1) and this is a small value so we have to multiply it by a factor say 100. After we have located the origin at the center of the screen we need to draw the two coordinates lines by using the functions
line(0,midy,getmaxx(),midy);
line(midx,0,midx,getmaxy());
the first eqution will draw the horizontal line and the second one will draw the vertical line.Now the screen is ready for us to draw the curve. So let us write a function that draw the curve in equation (1).


void Sketch_In_Cartesian()
 /* this is to sketch the curve y = sin x in cartesian coordinates */
{
float x,y,xplot,yplot,x1;
int maxx,maxy;
cleardevice();
maxx = getmaxx();
maxy = getmaxy();
line(0,midy(),maxx,midy());
line(midx(),0,midx(),maxy);
for(x1 = 0.0; x1 <= 720; x1+ = 0.5){
  x = x1 - 360;
  y = sin(x * M_PI / 180);
  xplot = midx + x;
  yplot = midy - y * 100;
  putpixel(xplot,yplot,WHITE);} /* this ends the for loop */
}	                       /* this ends the void */

NOTE : the functions midx & midy are defined in "INIT.INC", to run the function above you must include in your program the header files mentioned at the beginning of this sheet, also you have to include "math.h" header file.

Curves tracing in polar coordinates (2 Dimension) :

The Cardioid Curve

There are many equations in which the independent and dependent variables cann't be separated in the form of equation (1), these equations which are complicated are analyzed in other coordinates,most practical the polar coordinates which are related to the cartesian coordinates by

x = r cos t
y = r sin t
t = arctan(y / x)
r = (x² + y²)1/2
let us take some examples :

  1. r = a ( where a is constant) this is an equation of a circle center at origin.
  2. r = 2a sin t , is a circle with radius a / 2 with center at y coordinates. Let us take some complicated equations :
  3. The Cardioid r = a( 1 ± sin t), in cartesian coordinates it will be
    (x² + y²) / a = (x² + y²)1/2 - y
    from which we see that x and y cann't be separated.
  4. Leaf curve, r = a sin nt or r = a cos nt where n > 1, a is real, has a graph which consists of a number of loops attached to the origin. If n is even there are 2n loops, whereas if n is odd there are n loops.
    Now let us write a function to draw the Cardioid curve
    r = 2(1 - sin(t))

void Sketch_In_Polar()
/* this is to sketch the curve r =2*(1 - sin t) in polar  coordinates */
{
float x,y,xplot,yplot,r,theta;
int maxx,maxy;
cleardevice();
maxx = getmaxx();
maxy = getmaxy();
line(0,midy(),maxx,midy());
line(midx(),0,midx(),maxy);
for(theta = 0.0; theta <= 360.0; x1+ = 0.5){
  r = 2 * (1 - sin(theta * M_PI / 180));
  x = r * cos(theta * M_PI / 180);
  y = r * sin(theta * M_PI / 180);
  xplot = midx + x;
  yplot = midy - y * 100;
  putpixel(xplot,yplot,WHITE);}   /* this eds the for loop */
}                                 /* this ends the void */

Curves tracing in parametric equations (2 Dimension) :

The Astroid Curve

Consider the equations shown below

x = t , y = 2t then y = 2x
the equations above are called the parametric equations of the curve, which in our case is a straight line some complicated equations can be represented in the parametric form, take for example the Astroid parametric equations
x = (cos t)² * cos t
y = (sin t)² * sin t
if we want to illuminate t from the above equations the resulting equation will be very complicated, so it's easy to sketch the Astroid using the above equations.
Very important case occurs when the parametric equations have sinusoidal waveforms, this case has extensive applications in physics and electrical engineering.

Lissajous Figures :

Lissajous figures are patterns arising from the addition of two simple harmonic motions at right angles to each other, first studied by Jules Lissajous (1822-80)They also result when the electron beam in a cathod ray oscilloscope is subjected to two sinusoidal signals controling the vertical and horizental, so the electron beam will follow a specific path that depend on the signals equations :

x = Ax cos(Wx * t + Tx)
y = Ay cos(Wy * t + Ty)

Curves tracing in 3 Dimension :

A 3 Dimension curve


The space curve is very important in physics and antenna theory in telecommunications. Many software packages can draw curves in 3D, The technique used to draw a 3D curve is the same as our technique, but there descriptive and projective geometry are used to project the 3D curve into 2D curve that can be drawn in the plane. And this is all what I know about 3D curves.