Im currently working on http://ift.tt/1IE5GH9 for own practice.
I get the number of vertices and thier coordinates and Im suppose to determine the area and if the vertices is given in a clockwise order or counter clockwise order.
I use this formula to count the area of the polygon from http://ift.tt/1nb1zn2
After that I use in order to determine if its clockwise or counter closewise order acroding to http://ift.tt/1GuYRmt
I cant figure out what Im doing wrong in the testcases i get the right ouput.
int main() {
int n, x, y;
vector<int> cordX;
vector<int> cordY;
while(scanf("%d", &n)){
if(n == 0 ) break;
while(n != 0){
scanf("%d %d", &x, &y);
cordX.push_back(x);
cordY.push_back(y);
n--;
}
long double sum = 0;
long double a = 0;
long double b = 0;
for(int i = 0; i != cordY.size()-1;i++){
a += cordX[i] * cordY[i+1];
b += cordX[i+1] * cordY[i];
}
a += cordX[cordX.size()-1] * cordY[0];
b += cordX[0] *cordY[cordY.size()-1];
sum = (a-b)/2.0;
string d = "";
int i = 0;
int ccw = 0;
while(true){
ccw = ((cordX[i+1] - cordX[i]) * (cordY[i+2]- cordY[i])) - ((cordY[i+1]-cordY[i]) * (cordX[i+2] - cordX[i]));
if(ccw != 0){
break;
}
i++;
}
if(ccw > 0){
d = "CCW";
}else{
d = "CW";
}
printf("%s %.1Lf \n", d.c_str(),abs(sum));
cordY.clear();
cordX.clear();
}
}
Aucun commentaire:
Enregistrer un commentaire