CSCI 151 Videotaped Course - Program One on Recursion Removal
This is the code to modify for program one.
You will need to remove the recursion using gotos and a stack.
Delete everything above and including the line of asterisks below -
*******************************************************************
/** PROGRAM ClipTest.cpp *******************************************************
Program Name: ClipTest.cpp
Purpose : Test recursive clipping function.
Contains corrected code for recursive polygon clipper and related
functions.
**/
#include <iostream.h>
#include <iomanip.h>
#define FALSE 0
#define TRUE !FALSE
#define MAXPOINTS 500
#define LEFTEDGE 0 // Clockwise rotation, assuming that given
#define TOPEDGE 1 // the points (0,0), (0,10), (20,10) and
#define RIGHTEDGE 2 // (20,0), which define a window, (0,0) is
#define BOTTOMEDGE 3 // the bottom-left corner.
#define BOUNDARYCOUNT 4
// Typedefs.
//
typedef struct
{
float x;
float y;
} vertex;
typedef char boolean;
// Function prototypes.
//
void PolygonClipper(void);
void ClipThis(vertex p, int edge);
void ClipCloser(void);
void OutputVertex(vertex p);
// The following are to be coded by you.
//
vertex Intersection(vertex p, vertex s, int edge);
boolean Inside(vertex p, int edge);
boolean Cross(vertex p, vertex s, int edge);
// Variables needed globally by the clip functions.
//
vertex InArray[MAXPOINTS], // Polygon before clipping.
OutArray[MAXPOINTS]; // Polygon after clipping.
int InLength, // Verteces in polygon before clipping.
OutLength; // Verteces in polygon after clipping.
vertex s[BOUNDARYCOUNT],
FirstPoint[BOUNDARYCOUNT];
boolean NewEdge[BOUNDARYCOUNT]; // Tracks edges that have been
// clipped against.
float WinLeft, WinRight,
WinTop, WinBottom; // The window to clip to.
// Testbed for clipper.
// When you write your functions, you can use this testbed to check them.
// This data comes from the sample in your assignment.
//
void main(void)
{
int i;
WinTop = 10;
WinRight = 16;
WinBottom = 0;
WinLeft = 0; // Input window coords.
InLength = 6; // Input polyggon.
InArray[0].x = 3;
InArray[0].y = 6;
InArray[1].x = 11;
InArray[1].y = 17;
InArray[2].x = 22;
InArray[2].y = 9;
InArray[3].x = 13;
InArray[3].y = 6;
InArray[4].x = 8;
InArray[4].y = 2;
InArray[5].x = 3;
InArray[5].y = -4;
PolygonClipper();
for (i = 0; i < 79; ++i)
cout << "*";
cout << endl;
for (i = 0; i < OutLength; ++i)
cout << "(" << OutArray[i].x << ", " << OutArray[i].y << ")" << endl;
for (i = 0; i < 80; ++i)
cout << "*";
cout << endl;
} // void main(void)
// Wrapper for recursive polygon clipper, to be called publicly.
//
void PolygonClipper(void)
{
int k;
OutLength = 0;
for (k = 0; k < BOUNDARYCOUNT; ++k)
NewEdge[k] = TRUE;
for (k = 0; k < InLength; ++k)
ClipThis(InArray[k], LEFTEDGE);
ClipCloser();
} // void PolygonClipper(void)
// Recursive clipper.
//
void ClipThis(vertex p, int edge)
{
vertex intersectpt;
// Save the first point clipped against a window edge.
if (NewEdge[edge])
{
FirstPoint[edge] = p;
NewEdge[edge] = FALSE;
}
else
{
// If side ps crosses window edge, find intersection and clip intersection
// against next window edge.
if (Cross(p, s[edge], edge))
{
intersectpt = Intersection(p, s[edge], edge);
// Recursion #1.
if (edge < (BOUNDARYCOUNT - 1))
ClipThis(intersectpt, edge + 1);
else
OutputVertex(intersectpt);
} // if (Cross(p, s[edge], edge))
} // else
s[edge] = p;
// If p is inside the window edge, clip it against the next window edge.
if (Inside(p, edge))
{
// Recursion #2.
if (edge < (BOUNDARYCOUNT - 1))
ClipThis(p, edge + 1);
else
OutputVertex(p);
} // if (Inside(p, edge))
} // void ClipThis(vertex p, int edge)
// Closing routine.
//
void ClipCloser(void)
{
vertex intersectpt;
int edge;
for (edge = 0; edge < BOUNDARYCOUNT; ++edge)
{
if (Cross(s[edge], FirstPoint[edge], edge))
{
intersectpt = Intersection(s[edge], FirstPoint[edge], edge);
if (edge < (BOUNDARYCOUNT - 1))
ClipThis(intersectpt, edge + 1);
else
OutputVertex(intersectpt);
} // if (Cross(s[edge], FirstPoint[edge], edge))
} // for (edge = 0; edge < BOUNDARYCOUNT; ++edge)
} // void ClipCloser(void)
void OutputVertex(vertex p)
{
OutArray[OutLength++] = p;
}
// Returns the vertex that is the intersection of p->s with the window's edge.
//
vertex Intersection(vertex p, vertex s, int edge)
{
vertex i;
return i;
} // vertex Intersection(vertex p, vertex ss, int edge)
// Returns TRUE if vertex p is inside window's edge.
//
boolean Inside(vertex p, int edge)
{
}
// Returns TRUE if side p->s intersects a window's edge.
//
boolean Cross(vertex p, vertex s, int edge)
{
}