I am currently working on polymorphism in class, part of my homework is the classic point inside a shape test. I always have access to the lower left point of the triangle and it is always assumed to be isosceles. Last year I solved this problem using linear inequalites but I found out that method can fail under certain situations. So I am trying the cross product method. It seems to work at least with the dozen or so tests I have preformed. I just wanted a second pair of eyes on it in case there is a situation I may not be considering. Thanks in advance.

bool Triangle::contains (double pX, double pY){
   double lowLeftX;
   double lowLeftY;
   double midX;
   double midY;
   double lowRightX;
   double lowRightY;
   bool inside = false;
   location (lowLeftX, lowLeftY); // access method to get points from 
                                             // shape ancestor
   midX = .5 * base;             // finding top (midX and midY)
   midY = height;                 // and lower Right triangle points
   lowRightX = lowLeftX + base;
   lowRightY = lowLeftY;
   double crossProductA = (pX *(lowLeftY - midY)) + (pY * (midX - lowLeftX)) +
                           (lowLeftX * midY  - midX * lowLeftY);
   double crossProductB = (pX *(midY - lowRightY)) + (pY * (lowRightX - midX)) +
                           (midX * lowRightY  - lowRightX * midY);
   double crossProductC = (pX *(lowLeftY - lowRightY)) + (pY * (lowRightX - lowLeftX)) +
                           (lowLeftX * lowRightY  - lowRightX * lowLeftY);
   crossProductA = .5 * abs(crossProductA);
   crossProductB = .5 * abs(crossProductB);
   crossProductC = .5 * abs(crossProductC);

   if (crossProductA + crossProductB + crossProductC == (.5 * base * height)){
      inside = true;
   return inside;

whoops, forgot to add the lowerleft values to my midX and midY that would make my code only work when the lower left corner of the triangle was at (0, 0). Anyone notice any other mistakes or redundancies?

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.