Browse Source

maximum algorithm slightly simpler

boyska 7 years ago
parent
commit
24ce1a2339
1 changed files with 11 additions and 11 deletions
  1. 11 11
      lines.cpp

+ 11 - 11
lines.cpp

@@ -19,27 +19,25 @@ int main(int argc, char *argv[])
 	std::vector<cv::Point> hull;
 	cv::convexHull(cv::Mat(contours[0]),hull,false);
 
-	int i;
-	int idx[4]={0};
+	// takes the two points that are more distant from the next, ie:
+	// i st distance(hull[i], hull[i+1]) is maximum and 2nd maximum
+	int idx[2]={0};
 	int distance[2]={0};
-	for( i=0; hull.size()>i; i++ )
+	for( unsigned i=0; hull.size()>i; i++ )
 	{
-		cv::Point one=hull[i];
-		cv::Point two=hull[(i+1)%hull.size()];
-		int d=pow(one.x-two.x,2)+pow(one.y-two.y,2);
-		if( d>distance[0] )
+		cv::Point thispoint=hull[i];
+		cv::Point nextpoint=hull[(i+1)%hull.size()];
+		int d=pow(thispoint.x-nextpoint.x,2)+pow(thispoint.y-nextpoint.y,2);
+		if( d>distance[0] ) //the biggest till now
 		{
 			idx[1]=idx[0];
-			idx[3]=(idx[1]+1)%hull.size();
 			idx[0]=i;
-			idx[2]=(idx[0]+1)%hull.size();
 			distance[1]=distance[0];
 			distance[0]=d;
 		}
-		else if( d>distance[1] )
+		else if( d>distance[1] ) // it is the second biggest till now
 		{
 			idx[1]=i;
-			idx[3]=(i+1)%hull.size();
 			distance[1]=d;
 		}
 	}
@@ -61,3 +59,5 @@ int main(int argc, char *argv[])
 
 	return 0;
 }
+
+// vim: set noet ts=4 sw=4: