Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
Help.cpp
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------------------
3This source file is part of Hydrax.
4Visit ---
5
6Copyright (C) 2008 Xavier Verguín González <xavierverguin@hotmail.com>
7 <xavyiy@gmail.com>
8
9This program is free software; you can redistribute it and/or modify it under
10the terms of the GNU Lesser General Public License as published by the Free Software
11Foundation; either version 2 of the License, or (at your option) any later
12version.
13
14This program is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public License along with
19this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21http://www.gnu.org/copyleft/lesser.txt.
22--------------------------------------------------------------------------------
23*/
24
25#include <Help.h>
26
27namespace Hydrax
28{
29 Ogre::Vector2 Math::intersectionOfTwoLines(const Ogre::Vector2 &a, const Ogre::Vector2 &b,
30 const Ogre::Vector2 &c, const Ogre::Vector2 &d)
31 {
32 float r, s, denominator = (b.x - a.x) * (d.y - c.y) - (b.y - a.y) * (d.x - c.x);
33
34 // If the denominator in above is zero, AB & CD are colinear
35 if (denominator == 0)
36 {
37 return Ogre::Vector2::ZERO;
38 }
39
40 float numeratorR = (a.y - c.y) * (d.x - c.x) - (a.x - c.x) * (d.y - c.y);
41 // If the numerator above is also zero, AB & CD are collinear.
42 // If they are collinear, then the segments may be projected to the x-
43 // or y-axis, and overlap of the projected intervals checked.
44
45 r = numeratorR / denominator;
46
47 float numeratorS = (a.y - c.y) * (b.x - a.x) - (a.x - c.x) * (b.y - a.y);
48
49 s = numeratorS / denominator;
50
51 // If 0<=r<=1 & 0<=s<=1, intersection exists
52 // r<0 or r>1 or s<0 or s>1 line segments do not intersect
53 if (r < 0 || r > 1 || s < 0 || s > 1)
54 {
55 return Ogre::Vector2::ZERO;
56 }
57
59 // Note:
60 // If the intersection point of the 2 lines are needed (lines in this
61 // context mean infinite lines) regardless whether the two line
62 // segments intersect, then
63 //
64 // If r>1, P is located on extension of AB
65 // If r<0, P is located on extension of BA
66 // If s>1, P is located on extension of CD
67 // If s<0, P is located on extension of DC
68 //*/
69
70 // Find intersection point
71 return Ogre::Vector2((a.x + (r * (b.x - a.x))),
72 (a.y + (r * (b.y - a.y))));
73 }
74}
static Ogre::Vector2 intersectionOfTwoLines(const Ogre::Vector2 &a, const Ogre::Vector2 &b, const Ogre::Vector2 &c, const Ogre::Vector2 &d)
Find the intersection point of two lines.
Definition Help.cpp:29