Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / pacs / vector_2s.cpp
blob57e635f5dcb4ec758a122e0608982d49098422f5
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdpacs.h"
19 #include "nel/pacs/vector_2s.h"
21 namespace NLPACS {
24 // Test if 2 segments intersects
25 bool CVector2s::intersect(const CVector2s& a0, const CVector2s& a1, const CVector2s& b0, const CVector2s& b1, double* pa, double* pb)
28 double a0x = CVector2s::unpack(a0.x);
29 double a0y = CVector2s::unpack(a0.y);
30 double a1x = CVector2s::unpack(a1.x);
31 double a1y = CVector2s::unpack(a1.y);
32 double b0x = CVector2s::unpack(b0.x);
33 double b0y = CVector2s::unpack(b0.y);
34 double b1x = CVector2s::unpack(b1.x);
35 double b1y = CVector2s::unpack(b1.y);
37 double ax = +(a1x-a0x);
38 double ay = +(a1y-a0y);
39 double bx = +(b1x-b0x);
40 double by = +(b1y-b0y);
41 double cx = +(b0x-a0x);
42 double cy = +(b0y-a0y);
44 double d = -ax*by + ay*bx;
46 if (d == 0.0)
47 return false;
49 double a = (bx*cy - by*cx) / d;
50 double b = (ax*cy - ay*cx) / d;
52 if (pa != NULL)
53 *pa = a;
54 if (pb != NULL)
55 *pb = b;
57 return d != 0.0 && a >= 0.0 && a <= 1.0 && b >= 0.0 && b <= 1.0;
61 } // NLPACS