/* A program written to support the paper

S. Siegel, "The Angles between the Sides of a Quadrilateral and its

Diagonals," Mathematics Teacher 99 (2006), 653-655

in order to identify quadrilaterals whose angles are integers (in degree measure).

Written by: Dr. Laurence Boxer

Department of Computer and Information Sciences

Niagara University, NY 14109

USA

Fall, 2006

*/

#include <iostream.h>

#include <cmath>

#include <iomanip>

#include<fstream.h>


double radians(int);

int minimum(int, int);

int round(double);

bool isInt(double);

void headers(fstream &);

void print(fstream &, int);

void colHead(fstream &, char *);

void underline(fstream &);

void openFile(fstream &, bool &);

const double DEGPERPI = 180.0;

const double PI = acos(-1.0);

const int COLUMNWIDTH = 9;


int main(){

const int SMAX = 177;

int sDeg, vDeg, wDeg, tDeg, wBound;

double x, numerator, denom, sRad, vRad, wRad, tRad;

fstream resultFile;

bool success;

openFile(resultFile, success);

if (success) {

headers(resultFile);

for (sDeg = 1; sDeg <= SMAX; sDeg++)

{ sRad = radians(sDeg);

for (vDeg = 1; vDeg <= SMAX + 1 - sDeg; vDeg++)

{ vRad = radians(vDeg);

wBound = SMAX + 2;

for (wDeg = 1; wDeg <= wBound - sDeg - vDeg; wDeg++)

{ wRad = radians(wDeg);

for (tDeg = 1; tDeg <= minimum(sDeg, wBound - vDeg - wDeg); tDeg++) {

if (tDeg != sDeg) {

tRad = radians(tDeg);

numerator = sin(tRad) * sin(vRad) * sin(vRad + wRad + sRad) * sin(vRad + wRad);

denom = sin(tRad) * sin(vRad) * sin(vRad + wRad + sRad) * cos(vRad + wRad) +

sin(sRad) * sin(wRad) * sin(vRad + wRad + tRad);

if (denom)

{ x = atan(numerator / denom); // radians

x *= DEGPERPI / PI; // degrees

if (isInt(x) && (sDeg != tDeg)) {

print(resultFile, sDeg);

print(resultFile, vDeg);

print(resultFile, wDeg);

print(resultFile, tDeg);

print(resultFile, round(DEGPERPI - sDeg - vDeg - wDeg));

print(resultFile, round(x));

print(resultFile, round(vDeg + wDeg - x));

print(resultFile, round(DEGPERPI - tDeg - vDeg - wDeg));

resultFile << endl;

} // end if isInt

} // end if denom non-zero

} // end if t != s

} // for tDeg

} // end for wDeg

} // end for vDeg

} // end for sDeg

} // end if success

return 0;

}

// - - - - - - - - - - - - - - - -

double radians(int degrees) // radian equivalent of parameter in degrees

{ return degrees * PI / DEGPERPI;

} // end radians

// - - - - - - - - - - - - - - - -

int minimum(int a, int b) // smaller of the parameters

{ return (a <= b)? a: b;

} // end minimum

// - - - - - - - - - - - - - - - -

bool isInt(double x) // true if and only if deviation of parameter from integer is negligible

{ const double EPSILON = 1.0E-11;

double y = x - round(x);

return ((-EPSILON < y) && (y < EPSILON));

} // end isInt

// - - - - - - - - - - - - - - - -

void print(fstream & theFile, int n) // print parameter as formatted

{ theFile << setw(COLUMNWIDTH) << n << ' ';

}

// - - - - - - - - - - - - - - - -

void headers(fstream & theFile) // print column headers

{ colHead(theFile, "s");

colHead(theFile, "v");

colHead(theFile, "w");

colHead(theFile, "t");

colHead(theFile, "180-s-v-w");

colHead(theFile, "x");

colHead(theFile, "v+w-x");

colHead(theFile, "180-t-v-w");

theFile << endl;

for (int i = 1; i <= 8; i++)

underline(theFile);

theFile << endl;

} // end headers

// - - - - - - - - - - - - - - - -

void colHead(fstream & theFile, char * theString) // write theString as formatted

{ theFile << setw(COLUMNWIDTH) << theString << ' ';

} // end colHead

// - - - - - - - - - - - - - - - -

void underline(fstream & theFile) // write a string of underlines

{ for (int i = 0; i < COLUMNWIDTH; i++)

theFile << "-";

theFile << ' ';

}

// - - - - - - - - - - - - - - - -

int round(double x) // round to nearest integer

{ return (int)(x + 0.5);

} // end round

// - - - - - - - - - - - - - - - -

void openFile(fstream & theFile, bool & success)

// attempt opening file, return whether successful.

{ theFile.open("results", ios::out);

success = (theFile != NULL);

if (!success)

cout << "Unable to open file " <<

"results" << endl;

} /* end openfile */