The error is listed in the title and then commented in main (last comment). The program runs but once it encounters the bool expression I overloaded, it seems as if the program pauses.
I have a feeling it's a stack overload due to the way my function operates, but I hope I'm wrong.
Here's the code--
#include <cstdlib>
#include <iostream>
#include <math.h>
/**
File test_!.cpp in Project testing_Something.dev
Progress of project: Unexpected System exit after a certain amount of time, but no errors.
Suspecting the stack ran out of room to store variables for vector-magnitude comparisons via
the overloaded operator ==.
So far tests show that this project was successful, but I'm still trying to find the error.
*/
using namespace std;
class Point;
class Triangle;
class P2DVector;
class Point{
private:
double xP;
double yP;
public:
Point(double x, double y){
xP = x;
yP = y;
};
double getX(){return xP;};
double getY(){return yP;};
void showCoord(){
cout << getX() << ", " << getY() << endl;
};
};
class P2DVector{
private:
Point *points[2];
public:
P2DVector(Point *first, Point *second){
points[0] = first;
points[1] = second;
};
~P2DVector(){for(int i = 0; i < 2; i++){ if(points[i] != 0){delete points[i]; points[i] = 0;}}};
static double power(double value, int exponent){
if(exponent < 0)
{
cout << "Negative powers currently unsupported.";
cout << "\nRetrying with positive value." << endl;
return power(value, -exponent);
}
else if(exponent == 0)
return 1;
else
return value * power(value, exponent - 1);
};
/*
static inline double root(double value, int root)
{
double temp = 1.0;
for(double i = 0.000001; i < 10; i += .000001)
{
for(int j = 0; j < root; j++)
temp *= i;
if(static_cast<long>(temp) == value)
return i;
temp = 1.0;
}
return 0.0;
};*/ //not precise enough, and even when it is, it's just too slow
double getXDir(){
return (points[1]->getX() - points[0]->getX());
};
double getYDir(){
return (points[1]->getY() - points[0]->getY());
};
double magnitude(){
return sqrt( (power( points[1]->getX() - points[0]->getX() ,2)
+power(points[1]->getY() - points[0]->getY() ,2)));
};
Point startPoint(){
Point p (points[0]->getX(), points[0]->getY());
return p;
};
Point endPoint(){
Point p (points[1]->getX(), points[1]->getY());
return p;
};
P2DVector *unitP2DVector(){
Point *unitPoint[2];
unitPoint[0] = new Point(points[0]->getX() / magnitude(), points[0]->getY() / magnitude());
unitPoint[1] = new Point(points[1]->getX() / magnitude(), points[1]->getY() / magnitude());
P2DVector *temp = new P2DVector(unitPoint[0], unitPoint[1]);
return temp;
};
void displayLocation(){
cout << "This P2DVector Starts-> " << points[0]->getX() << ", " << points[0]->getY();
cout << "\nEnds-] " << points[1]->getX() << ", " << points[1]->getY();
cout << "\nDirection is <" << getXDir() << ", " << getYDir() <<">";
cout << ".\nContains magnitude: " << magnitude() << "\n" << endl;
};
};
class Triangle{
private:
Point *coordinates[3], *shortestCollision[3];
P2DVector *innerVectors[3], *outterVectors[3];
public:
Triangle(double xLoc, double yLoc, Point *points[3]){
for(int i = 0; i < 3; i++)
coordinates[i] = new Point( xLoc + points[i]->getX(), yLoc + points[i]->getY());
//need the natural vectors of the triangle!
outterVectors[0] = new P2DVector(coordinates[0], coordinates[1]);
outterVectors[1] = new P2DVector(coordinates[1], coordinates[2]);
outterVectors[2] = new P2DVector(coordinates[2], coordinates[0]);
//need vectors for opposite sides of points!
shortestCollision[0] = new Point(outterVectors[1]->startPoint().getX() + ((outterVectors[1]->getXDir() ) / 2),
outterVectors[1]->startPoint().getY() + ((outterVectors[1]->getYDir() ) / 2));
innerVectors[0] = new P2DVector(coordinates[0], shortestCollision[0]);
shortestCollision[1] = new Point(outterVectors[2]->startPoint().getX() + ((outterVectors[2]->getXDir() ) / 2),
outterVectors[2]->startPoint().getY() + ((outterVectors[2]->getYDir() ) / 2));
innerVectors[1] = new P2DVector(coordinates[1], shortestCollision[1]);
shortestCollision[2] = new Point(outterVectors[0]->startPoint().getX() + ((outterVectors[0]->getXDir() ) / 2),
outterVectors[0]->startPoint().getY() + ((outterVectors[0]->getYDir() ) / 2));
innerVectors[2] = new P2DVector(coordinates[2], shortestCollision[2]);
};
~Triangle(){
for(int i = 0; i < 3; i++)
{
innerVectors[i]->P2DVector::~P2DVector();
outterVectors[i]->P2DVector::~P2DVector();
}
};
void displayParameters(){
cout << "This triangle is defined by the points/vectors (in this order):\n" << flush;
for(int i = 0; i < 3; i++)
{
coordinates[i]->showCoord();
cout << "\nWith the inner vector:\n";
innerVectors[i]->displayLocation();
cout << "\n" << flush;
}
};
Point getFirstPoint(){
return coordinates[0][0];
};
Point getSecondPoint(){
return coordinates[1][0];
};
Point getThirdPoint(){
return coordinates[2][0];
};
bool operator== (Triangle other){
Point rogueTrianglePts[3] = {other.getFirstPoint(), other.getSecondPoint(), other.getThirdPoint()};
Point thisTrianglePts[3] = {getFirstPoint(), getSecondPoint(), getThirdPoint()};
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
P2DVector rogueVector (&thisTrianglePts[j], &rogueTrianglePts[i]);
if(
rogueVector.magnitude() <= innerVectors[j]->magnitude()
)
{
cout << "There was an intersection!" << endl;
return true;
}
}
}
cout << "There was no intersection!" << endl;
return false;
};
};
int main(int argc, char *argv[]){
Point *myPoints[3], *otherPoints[3], *anotherOutsider[3];
myPoints[0] = new Point(2, 0);//be careful using unions! You need to set properties for them if
myPoints[1] = new Point(4, 1);//you are using blank variables?...
myPoints[2] = new Point(2, 3);
otherPoints[0] = new Point(0, 0);
otherPoints[1] = new Point(1, 0);
otherPoints[2] = new Point(1, 1);
anotherOutsider[0] = new Point(-2, 2);
anotherOutsider[1] = new Point(0, 0);
anotherOutsider[2] = new Point(-2, 0);
Triangle *tri = new Triangle(1, 1, myPoints), *otherTri = new Triangle(5, 2, otherPoints),
*lastTriangle = new Triangle(4, 0, anotherOutsider);
tri->displayParameters();
otherTri->displayParameters();
lastTriangle->displayParameters();
/*
cout << ((*tri)==(*otherTri)) << endl; //Problem occurs here. Suspecting a stack-overload issue.
cout << ((*tri)==(*lastTriangle)) << endl;
cout << ((*otherTri)==(*lastTriangle)) << endl;
*/
/*
tri->Triangle::~Triangle();
otherTri->Triangle::~Triangle();
lastTriangle->Triangle::~Triangle();
*/
/*
delete tri;
delete otherTri;
delete lastTriangle;
*/
cin.get();
return 0;
}