Code:
#include <iostream>
struct Point
{
int x;
int y;
Point () : x(0) , y(0) { }
};
class Line
{
private:
int _X;
int _Y;
public:
Line () : _X(0) , _Y(0) { }
public:
void MakeLineVector (int& x1, int& y1, int& x2, int& y2);
int DotProduct (Line& _line) ;
int CrossProduct (Line& _line) ;
public:
int GetX (void) { return _X; }
int GetY (void) { return _Y; }
};
void Line::MakeLineVector (int& x1, int& y1, int& x2, int& y2)
{
_X = x2 - x1;
_Y = y2 - y1;
}
int Line::DotProduct (Line& _line) { return ((_X * _line._X) + (_Y * _line._Y)); }
int Line::CrossProduct (Line& _line) { return ((_X * _line._Y) - (_Y * _line._X)); }
int main (void)
{
int TotalPoints , Ctr1 , Ctr2 , Ctr3 , Ctr4;
std::cout << std::endl
<< "Enter Number of Points: ";
std::cin >> TotalPoints;
Point* Coordinate = new Point[TotalPoints];
for (Ctr1 = 0; Ctr1 < TotalPoints; Ctr1++)
{
std::cout << "Point " << Ctr1 + 1 << " x: "; std::cin >> Coordinate[Ctr1].x;
std::cout << "Point " << Ctr1 + 1 << " y: "; std::cin >> Coordinate[Ctr1].y;
}
int TotalPossibleLines = ((TotalPoints)*(TotalPoints - 1)) / 2 , CurrentLine = 0;
Line* Vector = new Line[TotalPossibleLines];
for (Ctr1 = 0; Ctr1 < TotalPoints - 1; Ctr1++)
{
for (Ctr2 = Ctr1 + 1; Ctr2 < TotalPoints; Ctr2++)
{
Vector[CurrentLine].MakeLineVector(Coordinate[Ctr1].x, Coordinate[Ctr1].y, Coordinate[Ctr2].x, Coordinate[Ctr2].y);
CurrentLine++;
}
}
int TotalPossibleRectangles = 0;
for (Ctr1 = 0; Ctr1 < TotalPossibleLines; Ctr1++)
for (Ctr2 = Ctr1 + 1; Ctr2 < TotalPossibleLines; Ctr2++)
if (Vector[Ctr1].CrossProduct(Vector[Ctr2]) == 0) // Parallel lines
for (Ctr3 = 0; Ctr3 < TotalPossibleLines; Ctr3++)
if (Vector[Ctr3].DotProduct(Vector[Ctr1 /*or Ctr2*/]) == 0) // Perpendicular lines
for (Ctr4 = Ctr3 + 1; Ctr4 < TotalPossibleLines; Ctr4++)
if (Vector[Ctr3].CrossProduct(Vector[Ctr4]) == 0) // Parallel lines
TotalPossibleRectangles++;
std::cout << std::endl << "Total Rectangles: " << TotalPossibleRectangles;
for(Ctr1 = 0; Ctr1 < TotalPossibleLines; Ctr1++)
{
std::cout << std::endl << " (" << Vector[Ctr1].GetX() << ")i + (" << Vector[Ctr1].GetY() << ")j";
}
delete[] Vector;
delete[] Coordinate;
return 0;
}
Hey, I got rid of the slope approach as Dot and Cross Products are easier to handle, something which should've been my first thought but I realised how stupid I was being by calculating the slope this morning. (Credit to @flp too as he commented about using the same a few hours after I realised my stupidity).
Note: This program isn't working like I expected it too and I'm not in a state of mind to find what's wrong as it's been a tiring day. I'll sort it out after school tomorrow but until then if someone does decide to run the code and looks at what's going on, please do tell me.
It's nothing different from the previous code. Just a better way to decide if a line is parallel or perpendicular.
I'm guessing each rectangle is being counted more than once proportional to the number on points being entered. I can simply divide it by the proportion constant (if I'm able to figure it out) and get a working solution but that wouldn't be what I'll be doing. First thing after school tomorrow, I'm gonna sit and find where I'm going wrong.
Thanks for the help provided by everybody!
(PS: Sorry about the code elongation. Whenever I copy-paste code, each newline character in my code is translated to two newlines on her idk why)