I have been getting different results if I run my program on Visual Code or an online compiler. I think this is an version issue, but I am not able to pin point the exact piece of code that is causing it. The program takes in two polygon vertices and prints the vertices of the intersection of the two polygons.
#include<iostream>
#include<vector>
struct point{
double x, y;
// point operator = (point const& obj)
// {
// point res;
// res.x = x + obj.x;
// res.y = y + obj.y;
// return res;
// }
};
int main()
{
int n,m;
std::vector<point> polyA, polyB;
std::cin >> n;
for(int i=0;i<n;i++)
{
point temp;
std::cin >> temp.x >> temp.y;
polyA.push_back(temp);
}
polyA.push_back(polyA[0]);
std::cin >> m;
for(int i=0;i<m;i++)
{
point temp;
std::cin >> temp.x >> temp.y;
polyB.push_back(temp);
}
polyB.push_back(polyB[0]);
auto determinant = [](point a, point b, point c) -> int
{
double deter = (double)a.x*((double)b.y-(double)c.y) - (double)b.x*((double)a.y-(double)c.y) + (double)c.x*((double)a.y-(double)b.y);
return (0 < deter) - (0 > deter);
};
auto lineEq = [](point A, point B) -> std::vector<double>
{
double a1 = B.y - A.y;
double b1 = A.x - B.x;
double c1 = a1*A.x + b1*A.y;
return {a1, b1, c1};
};
std::vector<point> polyInter = polyB;
for(int i=0;i<n;i++)
{
for(int j=0;polyInter.begin()+ j != polyInter.end();j++)
{
if(determinant(polyA[i], polyA[i+1], polyInter[j]) >= 0)
{
if(determinant(polyA[i], polyA[i+1], polyInter[j+1]) >= 0)
continue;
else
{
std::vector<double> eq1 = lineEq(polyA[i], polyA[i+1]);
std::vector<double> eq2 = lineEq(polyInter[j], polyInter[j+1]);
point temp;
double det = eq1[0]*eq2[1] - eq2[0]*eq1[1];
temp.x = (int)((eq2[1]*eq1[2] - eq1[1]*eq2[2])/det);
temp.y = (int)((eq1[0]*eq2[2] - eq2[0]*eq1[2])/det);
polyInter.insert(polyInter.begin()+j+1, temp);
j++;
}
}
else
{
if(determinant(polyA[i], polyA[i+1], polyInter[j+1]) > 0)
{
std::vector<double> eq1 = lineEq(polyA[i], polyA[i+1]);
std::vector<double> eq2 = lineEq(polyInter[j], polyInter[j+1]);
point temp;
double det = eq1[0]*eq2[1] - eq2[0]*eq1[1];
temp.x = (int)((eq2[1]*eq1[2] - eq1[1]*eq2[2])/det);
temp.y = (int)((eq1[0]*eq2[2] - eq2[0]*eq1[2])/det);
polyInter.insert(polyInter.begin()+j+1, temp);
}
polyInter.erase(polyInter.begin() + j);
j--;
}
}
// for(unsigned int i=0;i<polyInter.size(); i++)
// {
// std::cout << polyInter[i].x << " " << polyInter[i].y << " ";
// }
// std::cout << std::endl;
}
if(polyInter.empty())
{
std::cout << 0 << std::endl;
return 0;
}
polyInter.pop_back();
polyInter.insert(polyInter.end(), polyInter[0]);
polyInter.erase(polyInter.begin());
std::cout << polyInter.size() << std::endl;
for(unsigned int i=0;i<polyInter.size(); i++)
{
std::cout << polyInter[i].x << " " << polyInter[i].y << " ";
}
return 0;
}
Visual Code Input and Result:
4
0 0 10 0 10 10 0 10
4
5 -1 11 5 5 11 -1 5
8
10 4 10 6 6 10 4 10 0 6 0 4 4 0 6 0
Tutorialspoint online compiler (or any other online one):
4
0 0 10 0 10 10 0 10
4
5 -1 11 5 5 11 -1 5
7
10 4 10 6 6 10 4 10 0 6 0 4 6 0
Can any one suggest where the problem is?
Aucun commentaire:
Enregistrer un commentaire