geometry.cpp
changeset 792 7d67be709091
parent 789 d85834ad8c54
child 798 d251c7b2de54
     1.1 --- a/geometry.cpp	Mon Sep 07 15:36:57 2009 +0000
     1.2 +++ b/geometry.cpp	Tue Sep 08 12:15:39 2009 +0000
     1.3 @@ -3,6 +3,8 @@
     1.4  #include <math.h>
     1.5  #include "misc.h"
     1.6  
     1.7 +#include <QString>
     1.8 +
     1.9  #include <iostream>
    1.10  using namespace std;
    1.11  
    1.12 @@ -46,6 +48,48 @@
    1.13      return false;	
    1.14  }
    1.15  
    1.16 +Vector::Vector ():QPointF ()
    1.17 +{
    1.18 +}
    1.19 +
    1.20 +Vector::Vector (const QPointF &p):QPointF (p)
    1.21 +{
    1.22 +}
    1.23 +
    1.24 +Vector::Vector (qreal x, qreal y):QPointF (x,y)
    1.25 +{
    1.26 +}
    1.27 +
    1.28 +//! Normalize vector
    1.29 +void Vector::normalize ()
    1.30 +{
    1.31 +	if (x()==0 && y()==0) return;
    1.32 +	qreal l=sqrt ( x()*x() + y()*y() );
    1.33 +	setX (x()/l);
    1.34 +	setY (y()/l);
    1.35 +}
    1.36 +
    1.37 +//! Dot product of two vectors
    1.38 +qreal Vector::dotProduct (const QPointF &b)
    1.39 +{
    1.40 +	return x()*b.x() + y()*b.y();
    1.41 +}
    1.42 +
    1.43 +
    1.44 +void Vector::scale (const qreal &f)
    1.45 +{
    1.46 +	setX (x()*f);
    1.47 +	setY (y()*f);
    1.48 +}
    1.49 +
    1.50 +void Vector::invert ()
    1.51 +{
    1.52 +	setX (-x());
    1.53 +	setY (-y());
    1.54 +}
    1.55 +
    1.56 +/*! Calculate the projection of a polygon on an axis
    1.57 +    and returns it as a [min, max] interval  */
    1.58  ConvexPolygon::ConvexPolygon ()
    1.59  {
    1.60  }
    1.61 @@ -89,45 +133,43 @@
    1.62  	return _area;
    1.63  }
    1.64  
    1.65 -//! Normalize vector
    1.66 -QPointF normalize (const QPointF &p)
    1.67 +std::string ConvexPolygon::toStdString()
    1.68  {
    1.69 -	if (p==QPointF(0,0)) return p;
    1.70 -	qreal l=sqrt ( p.x()*p.x() + p.y()*p.y() );
    1.71 -	return QPointF (p.x()/l,p.y()/l);
    1.72 +	QString s ("(");
    1.73 +	for (int i=0;i<size();++i)
    1.74 +	{
    1.75 +		s+=QString("(%1,%2)").arg(at(i).x()).arg(at(i).y());
    1.76 +		if (i<size()-1) s+=",";
    1.77 +	}
    1.78 +	s+=")";	
    1.79 +	return s.toStdString();
    1.80  }
    1.81  
    1.82 -//! Dot product of two vectors
    1.83 -qreal dotProduct (const QPointF &a, const QPointF &b)
    1.84 +Vector ConvexPolygon::at(const int &i) const
    1.85  {
    1.86 -	return a.x()*b.x() + a.y()*b.y();
    1.87 +	return Vector (QPolygonF::at(i).x(),QPolygonF::at(i).y());
    1.88  }
    1.89  
    1.90 +void ConvexPolygon::translate ( const Vector & offset )
    1.91 +{ translate (offset.x(),offset.y());}
    1.92  
    1.93 -QPointF scale (const QPointF &v,const qreal &f)
    1.94 +void ConvexPolygon::translate ( qreal dx, qreal dy )
    1.95  {
    1.96 -	return QPointF (v.x()*f,v.y()*f);
    1.97 +	QPolygonF::translate (dx,dy);
    1.98 +	_centroid=_centroid+QPointF (dx,dy);
    1.99  }
   1.100  
   1.101 -QPointF invert (const QPointF &v)
   1.102 -{
   1.103 -	return QPointF (-v.x(),-v.y());
   1.104 -}
   1.105 -
   1.106 -/*! Calculate the projection of a polygon on an axis
   1.107 -    and returns it as a [min, max] interval  */
   1.108 -
   1.109 -void projectPolygon(QPointF axis, QPolygonF polygon, qreal &min, qreal &max) 
   1.110 +void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max) 
   1.111  {
   1.112      // To project a point on an axis use the dot product
   1.113  
   1.114  	//cout << "Projecting on "<< axis<<endl;
   1.115 -    qreal d = dotProduct(axis,polygon.at(0));
   1.116 +    qreal d = axis.dotProduct(polygon.at(0));
   1.117      min = d;
   1.118      max = d;
   1.119      for (int i = 0; i < polygon.size(); i++) 
   1.120  	{
   1.121 -        d= dotProduct (polygon.at(i),axis);
   1.122 +        d= polygon.at(i).dotProduct (axis);
   1.123          if (d < min) 
   1.124              min = d;
   1.125          else 
   1.126 @@ -153,8 +195,8 @@
   1.127   of the polygons (i.e. velocityA - velocityB)
   1.128  */
   1.129  
   1.130 -PolygonCollisionResult polygonCollision(QPolygonF polygonA, 
   1.131 -                              QPolygonF polygonB, QPointF velocity) 
   1.132 +PolygonCollisionResult polygonCollision(ConvexPolygon polygonA, 
   1.133 +                              ConvexPolygon polygonB, Vector velocity) 
   1.134  {
   1.135      PolygonCollisionResult result;
   1.136      result.intersect = true;
   1.137 @@ -207,8 +249,8 @@
   1.138  
   1.139          // Find the axis perpendicular to the current edge
   1.140  
   1.141 -        QPointF axis (-edge.y(), edge.x());
   1.142 -        axis=normalize(axis);
   1.143 +        Vector axis (-edge.y(), edge.x());
   1.144 +        axis.normalize();
   1.145  
   1.146          // Find the projection of the polygon on the current axis
   1.147  
   1.148 @@ -226,7 +268,7 @@
   1.149  
   1.150          // Project the velocity on the current axis
   1.151  
   1.152 -        qreal velocityProjection = dotProduct(axis,velocity);
   1.153 +        qreal velocityProjection = axis.dotProduct(velocity);
   1.154  
   1.155          // Get the projection of polygon A during the movement
   1.156