Okay so I have a program that tests for collisions using a complicated and 80% accurate method, but I want 100% accuracy.

Like someone posted earlier about shapes colliding into each other, I realized that there will be cases where my method wont work.

However, I've looked into a calculus book and found a seemingly "easy" solution to the problem.


To be more descriptive...

If I have two vectors, and their cross product is not zero (they're not parallel) then they have the potential to intersect, or collide. So I know I have to implement a cross product (which wont be hard for 2D vectors).

That part is fine.

Here's where things get hard though - I need to determine the collision point for the two vectors.

Say vector a is defined to to have a direction of <3, 4> from point (0, 0). (3, 4) is the end point.
Also vector b is defined to have a direction of <-2, 1> from point (1, 0). (-1, 1) is the end point.

Linearly the two vectors can be defined as--

<3t, 4t> from time 0 to time 1 (where time can be seconds or whatever, doesn't matter).
<-2s + 1, s> from a separate time 0 to time 1 (where time can be anything again, doesn't matter. It just defines the point that vector is from "birth" to full existence.)

Here's the problem. I need to solve this, now, in terms of s and t which I don't know how to do in c++.

On paper it's easy, but doing a relational with variables is fairly difficult.

Basically this becomes a system of equations if I want to find out when the vectors meet at separate times t and s.

To find the collision point, solve for--


3t = -2s + 1

4t = s

3t = -2(4t) + 1

3t + 8t = 1

t(3 + 8) = 1
t = 1/(3 + 8) --> t = 1/11
s = 4/(3 + 8) --> s = 4/11

Solution: since both s and t are less than 1, there must be a collision between the 2 vectors since the vectors are fully defined at t and s = 1. If Either s or t were over 1, then there would be no collision... return false.

I know how to do this mathematically, but now how do I program relations like this? I'm thinking of three options but I'm not sure if they're optimal...

--Create a separate program/class that takes a stream object, empties it into a String (which im not sure if I can or can't do... not too good with c++ yet) and examine the equation listed then "solves" for one variable as a function of the other and finds a comparison between the two.
--Create a point-mapper with a precision factor (basically maps points along one vector and the other) and test the starting point of both vectors for the mapped points to see if either of them have the same direction as the point mapped. Then calculate if their magnitudes can reach this point, and if even one cant then obviously there's no collision between the vectors.
--Find an API that handles vector collisions.

I'm hoping I can find a logical way of doing this... I will post the code that I have so that you can better understand what I am trying to do.

Note: The current collision-check operation bool operator== (Triangle) is only 85% precise at the moment, and I want 100% accuracy. Here's the current code:

#include <cstdlib>
#include <iostream>
#include <math.h>

/**
File test_!.cpp in Project testing_Something.dev

Progress of project: Fairly good progress, collision precision is only 85% accurate. Found a solution for the problem, just need to find out how to code it properly.
*/

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;
             };
             Point() {xP = 0; yP = 0;}
             void Set(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() {points[0] = Point(0,0); points[1] = Point(0,0); }
             void Set(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].Set(points[0].getX() / magnitude(), points[0].getY() / magnitude());
                    unitPoint[1].Set(points[1].getX() / magnitude(), points[1].getY() / magnitude());
                    
                    P2DVector temp(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].Set( xLoc + points[i].getX(), yLoc + points[i].getY());    
                                          
                  //need the natural vectors of the triangle!
                  outterVectors[0].Set(coordinates[0], coordinates[1]);
                  outterVectors[1].Set(coordinates[1], coordinates[2]);
                  outterVectors[2].Set(coordinates[2], coordinates[0]);
                  
                  //need vectors for opposite sides of points!
                  shortestCollision[0].Set(outterVectors[1].startPoint().getX() + ((outterVectors[1].getXDir() ) / 2),
                                                   outterVectors[1].startPoint().getY() + ((outterVectors[1].getYDir() ) / 2));
                  innerVectors[0].Set(coordinates[0], shortestCollision[0]);
                  
                  shortestCollision[1].Set(outterVectors[2].startPoint().getX() + ((outterVectors[2].getXDir() ) / 2),
                                                   outterVectors[2].startPoint().getY() + ((outterVectors[2].getYDir() ) / 2));
                  innerVectors[1].Set(coordinates[1], shortestCollision[1]);
                  
                  shortestCollision[2].Set(outterVectors[0].startPoint().getX() + ((outterVectors[0].getXDir() ) / 2),
                                                   outterVectors[0].startPoint().getY() + ((outterVectors[0].getYDir() ) / 2));
                  innerVectors[2].Set(coordinates[2], shortestCollision[2]);                                 
             };                                    
             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];
             };
             Point getSecondPoint(){
                   return coordinates[1];
             };
             Point getThirdPoint(){
                   return coordinates[2];
             };
             P2DVector firstInnerVector(){
                   return innerVectors[0];
             };
             P2DVector secondInnerVector(){
                   return innerVectors[1];
             };
             P2DVector thirdInnerVector(){
                   return innerVectors[2];
             };

             /**
               Currently only 85% precise.
              */
             bool operator== (Triangle other){
                  Point rogueTrianglePts[3] = {other.getFirstPoint(), other.getSecondPoint(), other.getThirdPoint()};
                  Point thisTrianglePts[3] = {getFirstPoint(), getSecondPoint(), getThirdPoint()};
                  int amount = 0;   
                  for(int i = 0; i < 3; i++){
                          for(int j = 0; j < 3; j++){
                             P2DVector rogueVector (thisTrianglePts[j], rogueTrianglePts[i]);
                             P2DVector ourVector (thisTrianglePts[i], rogueTrianglePts[j]);
                             P2DVector rogueInnerVectors[3] = {other.firstInnerVector(), other.secondInnerVector(),
                                                               other.thirdInnerVector()};
                             
                            if(
                                  rogueVector.magnitude() <= innerVectors[j].magnitude()
                              ){
                                 amount++;
                              }
                            else if(
                                      ourVector.magnitude() <= rogueInnerVectors[j].magnitude()
                                   ){
                                   amount++;     
                               }
                          }
                  }
                  
                  if(amount > 4){
                      cout << "There was an intersection!" << endl;
                      return true;
                  }
                  else{
                      cout << "There was NO intersection!" << endl;
                      return false;
                  }
             };                                 
};

int main(int argc, char *argv[]){
    
    Point myPoints[3];
    Point otherPoints[3];
    Point anotherOutsider[3];
    Point morePoints[3];
    
    myPoints[0].Set(2, 0);
    myPoints[1].Set(4, 1);
    myPoints[2].Set(2, 3);
    
    otherPoints[0].Set(0, 0);
    otherPoints[1].Set(1, 0);
    otherPoints[2].Set(1, 1);
    
    anotherOutsider[0].Set(-2, 2);
    anotherOutsider[1].Set(0, 0);
    anotherOutsider[2].Set(-2, 0);
    
    morePoints[0].Set(0, 0);
    morePoints[1].Set(2, 1);
    morePoints[2].Set(0, 1);
    
    Triangle tri(1, 1, myPoints);
    Triangle otherTri(5, 2, otherPoints);
    Triangle lastTriangle(4, 0, anotherOutsider);
    Triangle finalTriangle(2, 3, morePoints);
    
    tri.displayParameters();
    otherTri.displayParameters();
    lastTriangle.displayParameters();
    cout << (tri == otherTri) << endl;
    cout << (tri==lastTriangle) << endl;
    cout << (otherTri==lastTriangle) << endl;
    cout << (tri == finalTriangle) << endl;
    cout << (finalTriangle == otherTri) << endl;
    
    cin.get();
    return 0;
}

I am definitely not asking anyone to do this for me. I'd just like to be given the proper direction for implementing this solution into the program.

alex,

disclaimer_1: I did not even look at the code.
disclaimer_2: I think your main question is, "how do i solve the system of equations programmatically"

Have you considered using matrices?

You are not the first to try and solve (with a computer) a system of equations. In fact, there is a whole branch of math that deals with this called "Linear Algebra...college level".

The techniques to solving a linear matrix can be quite involved, but it IS the way to solve linear systems of equations.

I can help you with some advanced matrix techniques if you want it, but I think for your purposes your equations are always going to be first order - Ax+B type equations...and always a 2x2 system. (2 unknowns, 2 equations).

I do not know how familiar you are with matrix math, but b/c you asked this question and did not mention matrices, i assume you are relatively new to this. If i assumed incorrectly, my apologies.

Now, for the brief how to. Let, A be a 2x2 matrix, and let x be a 2x1 variable matrix, and let b be a 2x1 solution matrix. Our matrix equation is,
Ax=b
more specifically,
|a11 a12| * |x1| = |b1|
|a21 a22| _ |x2| _ |b2|

(i had to put underscores to keep the space)

so for your example it would look like

|3 2| * |t| = |1-0|
|4 -1| _ |s| _ |0-0|

The idea, is to put the matrix A (the coefficient matrix) into Row reduced echelon form (this is very algorithmic and why matrices are so prized by mathematicians). Once A is in reduced row echelon form, the matrix on the right hand side of the equation becomes your solution. I skipped over a lot of the details. I can help you with that if you want, but in case you already know it i didn't want to type it all up here. Also, many many many websites describe how to put a matrix into row echelon form. Note, make sure what ever operations you do to A you also do to b. This way, b becomes your solution.

In General, if you have two vectors u=<u0,u1> and v=<v0,v1>. If u starts at <x1,y1> and v starts at <x2,y2>, then the matrix set up will be as follows

|u0 -v0| * |t| = |x2-x1|
|u1 -v1| _ |s| _ |y2-y1|

It maybe confusing because the - signs on the left matrix are negatives (it is a 2x2), and the - on the right matrix are subtractions (it is a 2x1). Also, the | are supposed to be matrix brackets.

reduce row echelon this..and you have your solution. With this method you could easily add more vectors and see if they all meet at certain point..

the cross product you speak of actually is the determinant of matrix A. And there are thereoms that state if A has non-zero determinant than you have solutions, otherwise...none (parallel distinct) or infinite (parallel the same).

sites..

mathworld
wolfram?
sparknotes
wikipedia
google matrix math, reduced row echelon form (there are tricks that would make your math even more simple..by combing the matrix A and matrix b.

please don't be frightened...you already know the math. the matrices just help you keep track of things (they are the accountants). Matrices are very very powerful, they will be well worth your time to learn.

hth

It's a damn shame I didn't take Linear Algebra during this quarter =P

Even so I believe I already have an algorithm for computing matrices.

Yes I'm aware of matrices, yes I know how to do matrice multiplication and no I didn't know you could transcribe vectors into matrices.

Now it's just a matter of programming this. It's not like you can use variable names as coefficients. Variables in programming store values, they are not considered scalars.

By the way, thank you for the intuitive response.

You could have an array filled with coefficients.

for example your 2x2 matrix A

a[0][0] a[0][1]
a[1][0] a[1][1]

Just for example,
if you have an nth degree polynomial with coeficients a0,a1,a2,...an

an * x^n + a(n-1) * x ^ (n-1) +... + a1 * x^ 1 + a0 * x^0

have an array a

int a[n+1];//populate with your (n+1) coefficients.

You could have an array filled with coefficients.

for example your 2x2 matrix A

a[0][0] a[0][1]
a[1][0] a[1][1]

Just for example,
if you have an nth degree polynomial with coeficients a0,a1,a2,...an

an * x^n + a(n-1) * x ^ (n-1) +... + a1 * x^ 1 + a0 * x^0

have an array a

int a[n+1];//populate with your (n+1) coefficients.

Actually, if there's a way to do matrice division (basically like multiplication, but backwards) then I think that would be a more ideal solution...

for example instead of--

|3 2| * |t| = |1-0|
|4 -1| _ |s| _ |0-0|

--I'd have something like--

|3 2| / |1 - 0| = |t|
|4 -1| _ |0 0| _ |s|

--Either way I have to somehow tell the computer to pause and then check for potential solutions if I am to use your coefficient method, otherwise if I use a coefficient array and do matrice multiplication I expect the computer to calculate the answer before solving for s in terms of t and vice versa.

I may have to create a separate program for this that reads the direction and position of the vector and solves for the collision... a major pain but it really seems necessary at this point.

in the 2x2 case you can get away with taking shortcuts. I would warn you though, that you want to be careful not to divide by zero...

if you get anything higher than 2x2, it is time to do matrix operations.

hth

in the 2x2 case you can get away with taking shortcuts. I would warn you though, that you want to be careful not to divide by zero...

if you get anything higher than 2x2, it is time to do matrix operations.

hth

Yes I just noticed that doing division won't help... I either get a division-by-zero problem or the matrices cannot be multiplied =/

Damn...

Edit: For now I'm going to put this off... need to study for an upcoming final. Hopefully I'll be able to make some kind of algorithm for solving "string equations" and simply making this into one and solving it each time. Seems more ideal programming-wise.

I think I figured out the solution symbolically...

time2 = (((x2-x1)+((Vx)((y2-y1)/(-Vy))))/((Ux)+((Uy)/(-Vy))))

and therefore...

time1 = ((y2-y1)-((Uy)(time2)))/(-Vy)

Where Vx and Vy are the component directions of the 2nd vector and Ux and Uy are the component directions of the first vector, and the x and y are just specifying points.

I'm going to implement this code soon and see if it works. Hopefully it will, but I must study for my final.

Even if it doesn't work, I must admit that your advice turned out incredibly helpful.

I was capable of breaking down the problem algebraically and doing reverse-solving. These values wont be hard to implement in a function at all!

YES!!! YES!!! It works!

#include <cstdlib>
#include <iostream>
#include <math.h>

/**
Successful!!! Whoo!
*/

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;
             };
             Point() {xP = 0; yP = 0;}
             void Set(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() {points[0] = Point(0,0); points[1] = Point(0,0); }
             void Set(Point& first, Point& second){
                      points[0] = first;
                      points[1] = second;        
             };
             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);
             };
             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].Set(points[0].getX() / magnitude(), points[0].getY() / magnitude());
                    unitPoint[1].Set(points[1].getX() / magnitude(), points[1].getY() / magnitude());
                    
                    P2DVector temp(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;
             };
             bool operator==(P2DVector &other)
             {
                  double otherXDirection = other.getXDir();
                  double otherYDirection = other.getYDir();
                  double xDirection = getXDir();
                  double yDirection = getYDir();
                  
                  double time2 = (((other.startPoint().getX()-startPoint().getX())+((otherXDirection) *
                             ((other.startPoint().getY()
                             -startPoint().getY())
                             /(-otherYDirection))))
                             /((xDirection)+((yDirection)/(-otherYDirection))));
                  
                  double time1 = ((other.startPoint().getY()-startPoint().getY())
                                -((yDirection) *
                                (time2)))
                              /(-otherYDirection);
                              
                              //for debugging purposes only--
                         //     cout << "Time 1 was: " << time1 << endl;
                          //    cout << "Time 2 was: " << time2 << endl;
                              
                 return time1 > 0 && time1 <=1 && time2 > 0 && time2 <=1;
                              
                  
             };
};

class Triangle{
      private:
              Point coordinates[3], shortestCollision[3];
              P2DVector innerVectors[3], outterVectors[6];
      
      public:
             Triangle(double xLoc, double yLoc, Point points[3]){         
                  for(int i = 0; i < 3; i++)
                        coordinates[i].Set( xLoc + points[i].getX(), yLoc + points[i].getY());    
                                          
                  //need the natural vectors of the triangle!
                  outterVectors[0].Set(coordinates[0], coordinates[1]);
                  outterVectors[1].Set(coordinates[1], coordinates[2]);
                  outterVectors[2].Set(coordinates[2], coordinates[0]);
                  
                  //reversed inner vectors
                  outterVectors[3].Set(coordinates[1], coordinates[0]);
                  outterVectors[4].Set(coordinates[2], coordinates[1]);
                  outterVectors[5].Set(coordinates[0], coordinates[2]);                  
                  
                  //need vectors for opposite sides of points!
                  shortestCollision[0].Set(outterVectors[1].startPoint().getX() + ((outterVectors[1].getXDir() ) / 2),
                                                   outterVectors[1].startPoint().getY() + ((outterVectors[1].getYDir() ) / 2));
                  innerVectors[0].Set(coordinates[0], shortestCollision[0]);
                  
                  shortestCollision[1].Set(outterVectors[2].startPoint().getX() + ((outterVectors[2].getXDir() ) / 2),
                                                   outterVectors[2].startPoint().getY() + ((outterVectors[2].getYDir() ) / 2));
                  innerVectors[1].Set(coordinates[1], shortestCollision[1]);
                  
                  shortestCollision[2].Set(outterVectors[0].startPoint().getX() + ((outterVectors[0].getXDir() ) / 2),
                                                   outterVectors[0].startPoint().getY() + ((outterVectors[0].getYDir() ) / 2));
                  innerVectors[2].Set(coordinates[2], shortestCollision[2]);                                 
             };                                    
             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];
             };
             Point getSecondPoint(){
                   return coordinates[1];
             };
             Point getThirdPoint(){
                   return coordinates[2];
             };
             P2DVector firstInnerVector(){
                   return innerVectors[0];
             };
             P2DVector secondInnerVector(){
                   return innerVectors[1];
             };
             P2DVector thirdInnerVector(){
                   return innerVectors[2];
             };
             P2DVector firstOutterVector(){
                   return outterVectors[0];
             };
             P2DVector secondOutterVector(){
                   return outterVectors[1];
             };
             P2DVector thirdOutterVector(){
                   return outterVectors[2];
             };
             P2DVector fourthOutterVector(){
                   return outterVectors[3];
             };
             P2DVector fifthOutterVector(){
                   return outterVectors[4];
             };
             P2DVector sixthOutterVector(){
                   return outterVectors[5];
             };
             bool operator==(Triangle &other)
             {
                  P2DVector myVectors[] = {firstOutterVector(), secondOutterVector(), 
                                           thirdOutterVector(), fourthOutterVector(), 
                                           fifthOutterVector(), sixthOutterVector()};
                                           
                  P2DVector rogueVectors[] = {other.firstOutterVector(), other.secondOutterVector(), 
                                           other.thirdOutterVector(), other.fourthOutterVector(), 
                                           other.fifthOutterVector(), other.sixthOutterVector()};                                           
                  
                  for(int i = 0; i < 6; i++)
                  {
                          for(int j = 0; j < 6; j++)
                          {
                                  if(myVectors[i] == rogueVectors[j])
                                  {
                                      cout << "Intersection met!" << endl;
                                      return true;
                                  }
                          }
                  }
                  
                  cout << "No intersection met!" << endl;
                  return false;
             };                         
};

int main(int argc, char *argv[]){
    
    Point myPoints[3];
    Point otherPoints[3];
    Point anotherOutsider[3];
    
    myPoints[0].Set(2, 0);
    myPoints[1].Set(4, 1);
    myPoints[2].Set(2, 3);
    
    otherPoints[0].Set(0, 0);
    otherPoints[1].Set(1, 0);
    otherPoints[2].Set(1, 1);
    
    anotherOutsider[0].Set(-2, 2);
    anotherOutsider[1].Set(0, 0);
    anotherOutsider[2].Set(-2, 0);
    
    Triangle tri(1, 1, myPoints);
    Triangle otherTri(5, 2, otherPoints);
    Triangle lastTriangle(4, 0, anotherOutsider);
    
    tri.displayParameters();
    otherTri.displayParameters();
    lastTriangle.displayParameters();
    
    cout << (tri == otherTri) << endl;
    cout << (tri==lastTriangle) << endl;
    cout << (otherTri==lastTriangle) << endl;   
    
    cin.get();
    return 0;
}

WHOOO!!

/*line 116*/    return time1 >= 0 && time1 <=1 && time2 >= 0 && time2 <=1;

Had to change line >= 0 to account for vectors at start-points of collision.

Wow please allow me to apologize. I wrote the solution inverted (slightly dyslexic @_@ )

#include <cstdlib>
#include <iostream>
#include <math.h>

/**
Successful!!! Whoo!
*/

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;
             };
             Point() {xP = 0; yP = 0;}
             void Set(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() {points[0] = Point(0,0); points[1] = Point(0,0); }
             void Set(Point& first, Point& second){
                      points[0] = first;
                      points[1] = second;        
             };
             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);
             };
             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].Set(points[0].getX() / magnitude(), points[0].getY() / magnitude());
                    unitPoint[1].Set(points[1].getX() / magnitude(), points[1].getY() / magnitude());
                    
                    P2DVector temp(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;
             };
             bool operator==(P2DVector &other)
             {
                  double otherXDirection = other.getXDir();
                  double otherYDirection = other.getYDir();
                  double xDirection = getXDir();
                  double yDirection = getYDir();
                  /*
                  double time2 = (((other.startPoint().getX()-startPoint().getX())+((otherXDirection) *
                             ((other.startPoint().getY()
                             -startPoint().getY())
                             /(-otherYDirection))))
                             /((xDirection)+((yDirection)/(-otherYDirection))));*/
                             
                  double time2 = ((other.startPoint().getY()-startPoint().getY())-
                  (((other.getYDir())*((other.startPoint().getX())-(startPoint().getX())))
                  /(other.getXDir())))/((getYDir())-((other.getYDir())*((getXDir())/(other.getXDir()))));                             
                  
                  /*
                  double time1 = ((other.startPoint().getY()-startPoint().getY())
                                -((yDirection) *
                                (time2)))
                              /(-otherYDirection);*/
                              
                  double time1 = (
                                    (startPoint().getX() - other.startPoint().getX())
                                    + ((getXDir()) * (time2))
                                  )/(other.getXDir());                              
                              
                              //for debugging purposes only--
                              cout << "Time 1 was: " << time1 << endl;
                              cout << "Time 2 was: " << time2 << endl;
                              
                 return time1 > 0 && time1 <=1 && time2 > 0 && time2 <=1;
                              
                  
             };
};

class Triangle{
      private:
              Point coordinates[3], shortestCollision[3];
              P2DVector innerVectors[3], outterVectors[6];
      
      public:
             Triangle(double xLoc, double yLoc, Point points[3]){         
                  for(int i = 0; i < 3; i++)
                        coordinates[i].Set( xLoc + points[i].getX(), yLoc + points[i].getY());    
                                          
                  //need the natural vectors of the triangle!
                  outterVectors[0].Set(coordinates[0], coordinates[1]);
                  outterVectors[1].Set(coordinates[1], coordinates[2]);
                  outterVectors[2].Set(coordinates[2], coordinates[0]);
                  
                  //reversed outter vectors
                  outterVectors[3].Set(coordinates[1], coordinates[0]);
                  outterVectors[4].Set(coordinates[2], coordinates[1]);
                  outterVectors[5].Set(coordinates[0], coordinates[2]);                  
                  
                  //need vectors for opposite sides of points!
                  shortestCollision[0].Set(outterVectors[1].startPoint().getX() + ((outterVectors[1].getXDir() ) / 2),
                                                   outterVectors[1].startPoint().getY() + ((outterVectors[1].getYDir() ) / 2));
                  innerVectors[0].Set(coordinates[0], shortestCollision[0]);
                  
                  shortestCollision[1].Set(outterVectors[2].startPoint().getX() + ((outterVectors[2].getXDir() ) / 2),
                                                   outterVectors[2].startPoint().getY() + ((outterVectors[2].getYDir() ) / 2));
                  innerVectors[1].Set(coordinates[1], shortestCollision[1]);
                  
                  shortestCollision[2].Set(outterVectors[0].startPoint().getX() + ((outterVectors[0].getXDir() ) / 2),
                                                   outterVectors[0].startPoint().getY() + ((outterVectors[0].getYDir() ) / 2));
                  innerVectors[2].Set(coordinates[2], shortestCollision[2]);                                 
             };                                    
             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];
             };
             Point getSecondPoint(){
                   return coordinates[1];
             };
             Point getThirdPoint(){
                   return coordinates[2];
             };
             P2DVector firstInnerVector(){
                   return innerVectors[0];
             };
             P2DVector secondInnerVector(){
                   return innerVectors[1];
             };
             P2DVector thirdInnerVector(){
                   return innerVectors[2];
             };
             P2DVector firstOutterVector(){
                   return outterVectors[0];
             };
             P2DVector secondOutterVector(){
                   return outterVectors[1];
             };
             P2DVector thirdOutterVector(){
                   return outterVectors[2];
             };
             P2DVector fourthOutterVector(){
                   return outterVectors[3];
             };
             P2DVector fifthOutterVector(){
                   return outterVectors[4];
             };
             P2DVector sixthOutterVector(){
                   return outterVectors[5];
             };
             bool operator==(Triangle &other)
             {
                  P2DVector myVectors[] = {firstOutterVector(), secondOutterVector(), 
                                           thirdOutterVector(), fourthOutterVector(), 
                                           fifthOutterVector(), sixthOutterVector()};
                                           
                  P2DVector rogueVectors[] = {other.firstOutterVector(), other.secondOutterVector(), 
                                           other.thirdOutterVector(), other.fourthOutterVector(), 
                                           other.fifthOutterVector(), other.sixthOutterVector()};                                           
                  
                  for(int i = 0; i < 6; i++)
                  {
                          for(int j = 0; j < 6; j++)
                          {
                                  if(myVectors[i] == rogueVectors[j])
                                  {
                                      cout << "Intersection met!" << endl;
                                      return true;
                                  }
                          }
                  }
                  
                  cout << "No intersection met!" << endl;
                  return false;
             };                         
};

int main(int argc, char *argv[]){
    
    Point myPoints[3];
    Point otherPoints[3];
    
    myPoints[0].Set(2, 0);
    myPoints[1].Set(4, 1);
    myPoints[2].Set(2, 3);
    
    otherPoints[0].Set(0, 0);
    otherPoints[1].Set(1, 0);
    otherPoints[2].Set(1, 1);
    
    Triangle tri(1, 1, myPoints);
    Triangle otherTri(6, 2, otherPoints);
 
    
    tri.displayParameters();
    otherTri.displayParameters();
    
    cout << (tri == otherTri) << endl;  
    
    cin.get();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.