header

Draw an Ellipse Using Parametric Equations

//---------------------------------------------------------
// Ellipse
//
// Draw a curve based on parametric equations.
//---------------------------------------------------------

#include "worldType.h"
#include <graphics.h>
#include <cmath>
#include <iostream>
using namespace std;

const double PI = 3.14159265358979;

//---------------------------------------------------------
// drawEllipse
//
// Draw an ellipse based on the parametric equations
//
//     x = r1 * cos(theta)
//     y = r2 * sin(theta)
//
// where theta runs from 0 to 2*PI.
//
// In parameters: r1, r2
//---------------------------------------------------------
void drawEllipse(worldType w, double r1, double r2)
{
    int numPoints = 360;
    double theta;
    double degree = PI / 180.0;
    
    w.moveTo(r1, 0.0);
    for (int i = 1; i < numPoints; i++)
    {
        theta = double(i) * degree;
        w.lineTo(r1 * cos(theta), r2 * sin(theta));
    }
    w.lineTo(r1, 0.0);
}

//---------------------------------------------------------
//                   M A I N   D R I V E R
//---------------------------------------------------------

int main()
{
    int size;
    double r1, r2;
    
    cout << "This program draws a curve based on parametric\n";
    cout << "equations of the form:\n\n";
    cout << "    x = r1 * cos(theta)\n";
    cout << "    y = r2 * sin(theta)\n\n";
    cout << "where you enter the values of r1 and r2.\n\n";
    cout << "The world coordinates range from -1.0 to 1.0 in\n";
    cout << "both directions.\n\n";
    cout << "Enter size of window (in pixels): ";
    cin >> size;

    worldType w(size, size, 300, 10, -1.0, -1.0, 1.0, 1.0,
                "Ellipse");
    do
    {
        cout << "\nEnter r1 (0 or less to terminate): ";
        cin >> r1;
        if (r1 > 0.0)
        {
            cout << "Enter r2: ";
            cin >> r2;
            cleardevice();
            drawEllipse(w, r1, r2);
        }
    } while (r1 > 0.0);
   
    w.close();
    
    return 0;
}

Drawing a Spiral using Parametric Equations

//---------------------------------------------------------
// Spiral
//
// Draw a curve based on parametric equations.
//---------------------------------------------------------

#include "worldType.h"
#include <graphics.h>
#include <cmath>
#include <iostream>
using namespace std;

const double PI = 3.14159265358979;

//---------------------------------------------------------
// drawSpiral
//
// Draw a spiral with n turns with outer radius r. The
// initial direction of the spiral (from the origin) is
// given by angle ang (in radians).
//
//     x = r * cos(theta + ang)
//     y = r * sin(theta + ang)
//
// where theta runs from 0 to 2*n*PI
// and r = outerR * theta / (2 * n * PI).
//
// In parameters: w, n, outerR, ang
//---------------------------------------------------------
void drawSpiral(worldType w, int n, double outerR, double ang)
{
    double r;
    double theta;
    int numPoints = 360 * n;
    double degree = PI / 180.0;
    double n2PI = 2.0 * double(n) * PI;
    
    w.moveTo(0.0, 0.0);
    for (int i = 0; i < numPoints; i++)
    {
        theta = double(i) * degree;
        r = outerR * theta / n2PI;
        w.lineTo(r * cos(theta + ang), r * sin(theta + ang));
    }
}

//---------------------------------------------------------
// drawCircle
//
// Draw a circle with the specified radius and centered
// on the specified point.
//
//     x = h + r * cos(theta)
//     y = k + r * sin(theta)
//
// where theta goes from 0 to 2*PI
//
// In Parameters: w, r, h, k
//---------------------------------------------------------
void drawCircle(worldType w, double r, double h, double k)
{
    double theta;
    int numPoints = 180;
    double delta = 2.0 * PI / double(numPoints);
    
    w.moveTo(h + r, k);
    for (int i = 1; i < numPoints; i++)
    {
        theta = double(i) * delta;
        w.lineTo(h + r * cos(theta), k + r * sin(theta));
    }
    w.lineTo(h + r, k);
}

//---------------------------------------------------------
//                   M A I N   D R I V E R
//---------------------------------------------------------

int main()
{
    int size;
    int n;
    int turns;
    double r;
    
    cout << "This program draws evenly spaced spirals inside a\n";
    cout << "circle based on the number of turns and outer radius.\n\n";
    cout << "The world coordinates range from -10.0 to 10.0 in\n";
    cout << "both directions.\n\n";
    cout << "Enter size of window (in pixels): ";
    cin >> size;

    worldType w(size, size, 700, 10, -10.0, -10.0, 10.0, 10.0,
                "Spirals");
    do
    {
        cout << "\nEnter number of spirals (0 or less to terminate): ";
        cin >> n;
        if (n > 0.0)
        {
            cout << "Enter the number of turns per spiral: ";
            cin >> turns;
            cout << "Enter outer radius: ";
            cin >> r;
            cleardevice();
            for (int i = 0; i < n; i++)
            {
                drawSpiral(w, turns, r, double(i) * 2.0 * PI / double(n));
            }
            drawCircle(w, r, 0.0, 0.0);
        }
    } while (n > 0);
   
    w.close();
    
    return 0;
}