# HG changeset patch
# User insilmaril
# Date 1201879715 0
# Node ID 9abdd5e6c3dc315ee1545c988e90bf4fef6c7d1d
# Parent  6a5e2c27f8a4da1b1723da9ba4016812844cbb31
Added brasilian translation by Amadeu JĂșnior

diff -r 6a5e2c27f8a4 -r 9abdd5e6c3dc geometry.cpp
--- a/geometry.cpp	Fri Feb 01 15:28:35 2008 +0000
+++ b/geometry.cpp	Fri Feb 01 15:28:35 2008 +0000
@@ -1,7 +1,10 @@
 #include "geometry.h"
 
 #include <math.h>
+#include <iostream>
+#include "misc.h"
 
+using namespace std;
 
 QRectF addBBox(QRectF r1, QRectF r2)
 {	
@@ -44,8 +47,9 @@
 
 QPointF normalize (const QPointF &p)
 {
-	qreal n=sqrt ( p.x()*p.x() + p.y()*p.y() );
-	return QPointF (p.x()/n,p.y()/n);
+	if (p==QPointF(0,0)) return p;
+	qreal l=sqrt ( p.x()*p.x() + p.y()*p.y() );
+	return QPointF (p.x()/l,p.y()/l);
 }
 
 // Dot product of two vectors
@@ -54,19 +58,6 @@
 	return a.x()*b.x() + a.y()*b.y();
 }
 
-// Structure that stores the results of the PolygonCollision function
-class PolygonCollisionResult {
-public:
-    // Are the polygons going to intersect forward in time?
-    bool WillIntersect;
-
-    // Are the polygons currently intersecting?
-    bool Intersect;
-
-    // The translation to apply to the first polygon to push the polygons apart.
-    QPointF MinimumTranslationVector;
-};
-
 
 /* Calculate the projection of a polygon on an axis
    and returns it as a [min, max] interval
@@ -110,8 +101,8 @@
 PolygonCollisionResult PolygonCollision(QPolygonF polygonA, 
                               QPolygonF polygonB, QPointF velocity) {
     PolygonCollisionResult result;
-    result.Intersect = true;
-    result.WillIntersect = true;
+    result.intersect = true;
+    result.willIntersect = true;
 
     int edgeCountA = polygonA.size();
     int edgeCountB = polygonB.size();
@@ -119,14 +110,42 @@
     QPointF translationAxis;
     QPointF edge;
 
+	cout << "\nA: ";
+	for (int k=0; k<edgeCountA;k++)
+		cout <<polygonA.at(k);
+	cout << "\nB: ";
+	for (int k=0; k<edgeCountB;k++)
+		cout <<polygonB.at(k);
+		
+		
     // Loop through all the edges of both polygons
-
-    for (int i=0; i < edgeCountA + edgeCountB; i++) 
+	int i=0;
+	int j=0;
+	while (i<edgeCountA && j<edgeCountB)
 	{
         if (i< edgeCountA) 
-            edge = polygonA.at(i);
-        else 
-            edge = polygonB.at(i - edgeCountA);
+		{
+			if (i<edgeCountA - 1)
+				edge = QPointF (
+					polygonA.at(i+1).x()-polygonA.at(i).x(), 
+					polygonA.at(i+1).y()-polygonA.at(i).y());
+			else		
+				edge = QPointF (
+					polygonA.at(0).x()-polygonA.at(i).x(), 
+					polygonA.at(0).y()-polygonA.at(i).y());
+			i++;		
+        } else 
+		{
+			if (i < edgeCountB -1)
+				edge = QPointF (
+					polygonB.at(j+1).x() - polygonA.at(i).x(), 
+					polygonB.at(j+1).y() - polygonA.at(i).y());
+			else	
+				edge = QPointF (
+					polygonB.at(0).x() - polygonA.at(i).x(), 
+					polygonB.at(0).y() - polygonA.at(i).y());
+			j++;
+		}
 
         // ===== 1. Find if the polygons are currently intersecting =====
 
@@ -134,7 +153,7 @@
         // Find the axis perpendicular to the current edge
 
         QPointF axis (-edge.y(), edge.x());
-        normalize(axis);
+        axis=normalize(axis);
 
         // Find the projection of the polygon on the current axis
 
@@ -144,8 +163,10 @@
 
         // Check if the polygon projections are currentlty intersecting
 
-        if (intervalDistance(minA, maxA, minB, maxB) > 0)\
-            result.Intersect = false;
+        if (intervalDistance(minA, maxA, minB, maxB) > 0)
+            result.intersect = false;
+		else	
+            result.intersect = true;
 
         // ===== 2. Now find if the polygons *will* intersect =====
 
@@ -156,42 +177,58 @@
 
         // Get the projection of polygon A during the movement
 
-        if (velocityProjection < 0) {
+        if (velocityProjection < 0) 
             minA += velocityProjection;
-        } else {
+        else 
             maxA += velocityProjection;
-        }
+        
 
         // Do the same test as above for the new projection
 
         qreal d = intervalDistance(minA, maxA, minB, maxB);
-        if (d > 0) result.WillIntersect = false;
+        if (d > 0) result.willIntersect = false;
+		/*
+		*/
+		cout <<"   ";
+		cout <<"minA="<<minA<<"  ";
+		cout <<"maxA="<<maxA<<"  ";
+		cout <<"minB="<<minB<<"  ";
+		cout <<"maxB="<<maxB<<"  ";
+		cout <<"  d="<<d<<"   ";
+		cout <<"minD="<<minIntervalDistance<<"  ";
+		cout <<"axis="<<axis<<"  ";
+		cout <<"int="<<result.intersect<<"  ";
+		cout <<"wint="<<result.willIntersect<<"  ";
+		//cout <<"velProj="<<velocityProjection<<"  ";
+		cout <<endl;
 
-        // If the polygons are not intersecting and won't intersect, exit the loop
 
-        if (!result.Intersect && !result.WillIntersect) break;
+	
+        if (result.intersect || result.willIntersect) 
+		{
+			// Check if the current interval distance is the minimum one. If so
+			// store the interval distance and the current distance.  This will
+			// be used to calculate the minimum translation vector
 
-        // Check if the current interval distance is the minimum one. If so store
-        // the interval distance and the current distance.
-        // This will be used to calculate the minimum translation vector
+			if (d<0) d=-d;
+			if (d < minIntervalDistance) {
+				minIntervalDistance = d;
+				translationAxis = axis;
+				cout << "tAxix="<<translationAxis<<endl;
 
-        if (d<0) d=-d;
-        if (d < minIntervalDistance) {
-            minIntervalDistance = d;
-            translationAxis = axis;
-
-            //QPointF t = polygonA.Center - polygonB.Center;
-            QPointF t = polygonA.at(0) - polygonB.at(0);
-            if (dotProduct(t,translationAxis) < 0)
-                translationAxis = -translationAxis;
-        }
+				//QPointF t = polygonA.Center - polygonB.Center;
+				QPointF t = polygonA.at(0) - polygonB.at(0);
+				if (dotProduct(t,translationAxis) < 0)
+					translationAxis = -translationAxis;
+			}
+		}
     }
 
     // The minimum translation vector
     // can be used to push the polygons appart.
 
-    if (result.WillIntersect)
-        result.MinimumTranslationVector = 
+    if (result.willIntersect)
+        result.minTranslation = 
                translationAxis * minIntervalDistance;
     
     return result;
@@ -208,7 +245,7 @@
 if (r.WillIntersect) 
   // Move the polygon by its velocity, then move
   // the polygons appart using the Minimum Translation Vector
-  polygonATranslation = velocity + r.MinimumTranslationVector;
+  polygonATranslation = velocity + r.minTranslation;
 else 
   // Just move the polygon by its velocity
   polygonATranslation = velocity;
diff -r 6a5e2c27f8a4 -r 9abdd5e6c3dc geometry.h
--- a/geometry.h	Fri Feb 01 15:28:35 2008 +0000
+++ b/geometry.h	Fri Feb 01 15:28:35 2008 +0000
@@ -10,4 +10,25 @@
 
 QPointF normalize (const QPointF &p);
 
+
+qreal dotProduct (const QPointF &a, const QPointF &b);
+
+class PolygonCollisionResult {
+public:
+    // Are the polygons going to intersect forward in time?
+    bool willIntersect;
+
+    // Are the polygons currently intersecting?
+    bool intersect;
+
+    // The translation to apply to the first polygon to push the polygons apart.
+    QPointF minTranslation;
+};
+
+
+void ProjectPolygon(QPointF axis, QPolygonF polygon, qreal &min, qreal &max) ;
+qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB);
+PolygonCollisionResult PolygonCollision(QPolygonF polygonA, 
+                              QPolygonF polygonB, QPointF velocity);
+
 #endif