Following from the solution from this post here. It seems that using the points (1,0), (-1,0), (0,1), and (0,-1) the solution fails when it should return that these points indeed form a square.
Perhaps there is something wrong with my implementation. Here is my code:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
double x1 = p1[0], x2 = p2[0], x3 = p3[0], x4 = p4[0];
double y1 = p1[1], y2 = p2[1] , y3 = p3[1], y4 = p4[1];
double cx = (x1+x2+x3+x4)/4;
double cy = (y1+y2+y3+y4)/4;
double a1 = (cx - x1), a2 = (cy - y1);
double b1 = (cx - x2), b2 = (cy - y2);
double c1 = (cx - x3), c2 = (cy - y3);
double d1 = (cx - x4), d2 = (cy - y4);
double dd1 = a1*a1 + a2*a2;
double dd2 = b1*b1 + b2*b2;
double dd3 = c1*c1 + c2*c2;
double dd4 = d1*d1 + d2*d2;
double epsilon = 0.00001;
return abs(dd1 - dd2) < epsilon && abs(dd1 - dd3) < epsilon && abs(dd1 - dd4) < epsilon;
}
};
int main() {
vector<int> p1, p2, p3, p4;
p1.push_back(1);
p1.push_back(0);
p2.push_back(-1);
p2.push_back(0);
p3.push_back(0);
p3.push_back(1);
p4.push_back(0);
p4.push_back(-1);
Solution m;
bool x;
x = m.validSquare(p1,p2,p3,p4);
if(x == 1) {
cout << "Points form a square" << endl;
}
else {
cout << "Points do not form a square" << endl;
}
return 0;
}
The solution in the link is definitely correct but for some reason for these four points I do not get an accurate return. If anyone has any suggestions please let me know.
Update:
Following suggestions from users. I have changed a few lines. Using the points (0,0), (5,0), (5,4), and (0,4) should not return that these points can make a square. Unfortunately, my updated code still returns that these points are a square so I am not sure what the problem is.
Here is my code:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Solution {
public:
bool isFloatEqual(double a, double b) {
double epsilon = 0.001;
return abs(a - b) < epsilon;
}
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
double x1 = p1[0], x2 = p2[0], x3 = p3[0], x4 = p4[0];
double y1 = p1[1], y2 = p2[1] , y3 = p3[1], y4 = p4[1];
double cx = (x1+x2+x3+x4)/4;
double cy = (y1+y2+y3+y4)/4;
double a1 = (cx - x1), a2 = (cy - y1);
double b1 = (cx - x2), b2 = (cy - y2);
double c1 = (cx - x3), c2 = (cy - y3);
double d1 = (cx - x4), d2 = (cy - y4);
double dd1 = a1*a1 + a2*a2;
double dd2 = b1*b1 + b2*b2;
double dd3 = c1*c1 + c2*c2;
double dd4 = d1*d1 + d2*d2;
return isFloatEqual(dd1,dd2) && isFloatEqual(dd1, dd3) &&isFloatEqual(dd1, dd4);
}
};
int main() {
vector<int> p1, p2, p3, p4;
p1.push_back(0);
p1.push_back(0);
p2.push_back(5);
p2.push_back(0);
p3.push_back(5);
p3.push_back(4);
p4.push_back(0);
p4.push_back(4);
Solution m;
bool x;
x = m.validSquare(p1,p2,p3,p4);
if(x == 1) {
cout << "Points form a square" << endl;
}
else {
cout << "Points do not form a square" << endl;
}
return 0;
}