1 /*  $Id: RCouple.cpp,v 1.3 2012/07/14 03:44:21 sarrazip Exp $
2     RCouple.h - Class representing a couple of integers.
3 
4     flatzebra - Generic 2D Game Engine library
5     Copyright (C) 1999-2012 Pierre Sarrazin <http://sarrazip.com/>
6 
7     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; either version 2
10     of the License, or (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20     02110-1301, USA.
21 */
22 
23 #include <flatzebra/RCouple.h>
24 
25 using namespace flatzebra;
26 
27 
28 const double RCouple::tolerance = 0.0001;
29 
30 
31 bool
rectangleCollision(const RCouple & pos1,const RCouple & size1,const RCouple & pos2,const RCouple & size2)32 RCouple::rectangleCollision(const RCouple &pos1,
33                             const RCouple &size1,
34                             const RCouple &pos2,
35                             const RCouple &size2)
36 {
37     if (pos1.x + size1.x <= pos2.x)  // s1 at the left of s2
38         return false;
39     if (pos1.y + size1.y <= pos2.y)  // s1 above s2
40         return false;
41     if (pos2.x + size2.x <= pos1.x)  // s1 at the right of s2
42         return false;
43     if (pos2.y + size2.y <= pos1.y)  // s1 below s2
44         return false;
45     return true;
46 }
47 
48 
49 RCouple
getClosestPointOnSegment(const RCouple & segStart,const RCouple & segEnd) const50 RCouple::getClosestPointOnSegment(const RCouple &segStart,
51                                   const RCouple &segEnd) const
52 {
53     assert(segStart != segEnd);  // this condition avoids division by zero
54     RCouple seg = segEnd - segStart;
55     RCouple c = *this - segStart;
56     double t = seg.dotProduct(c) / seg.dotProduct(seg);  // project pt on segment
57     if (t < 0.0)
58         t = 0.0;
59     else if (t > 1.0)
60         t = 1.0;
61     return segStart + t * seg;
62 }
63 
64 
65 bool
isOnSegment(const RCouple & segStart,const RCouple & segEnd) const66 RCouple::isOnSegment(const RCouple &segStart,
67                      const RCouple &segEnd) const
68 {
69     assert(segStart != segEnd);  // this condition avoids division by zero
70     RCouple seg = segEnd - segStart;
71     RCouple c = *this - segStart;
72     double t = seg.dotProduct(c) / seg.dotProduct(seg);  // project pt on segment
73     if (t < 0.0 || t > 1.0)
74         return false;
75     RCouple proj = segStart + t * seg;
76     return proj == *this;
77 }
78