<p>I am trying to find an algorithm as mentioned in the subject. It is as follows:</p>

<pre><code> int orientation(glm::i16vec2 &amp;p1,
                 glm::i16vec2 &amp;p2,
                 glm::i16vec2 &amp;p3)
 {
   return (((p3.x - p1.x) * (p2.y - p1.y)) - ((p2.x - p1.x) * (p3.y - p1.y)));
 }


 bool onSegment(const glm::i16vec2 &amp;pi,
                const glm::i16vec2 &amp;pj,
                const glm::i16vec2 &amp;pk)
 {
   if((std::min(pi.x,pj.x) &lt;= pk.x &amp;&amp; pk.x &lt;= std::max(pi.x,pj.x)) &amp;&amp;
      (std::min(pi.y,pj.y) &lt;= pk.y &amp;&amp; pk.y &lt;= std::max(pi.y,pj.y)))
    return true;
   else
    return false;
 }

 int main()
 {
   glm::i16vec2 hs(5,2);  // p1
   glm::i16vec2 he(5,3); // p2


   glm::i16vec2 cs(5,1);   // p3
   glm::i16vec2 ce(5,3); // p4

   int d1 = orientation(cs,ce,hs);
   int d2 = orientation(cs,ce,he);
   int d3 = orientation(hs,he,cs);
   int d4 = orientation(hs,he,ce);

   if((d1 == 0 &amp;&amp; d2 == 0 &amp;&amp; d3 == 0 &amp;&amp; d4 == 0) &amp;&amp;
        (onSegment(cs,ce,hs) &amp;&amp; onSegment(cs,ce,he)))
   {
    std::cout &lt;&lt; "HPs is entirely inside CPs" &lt;&lt; std::endl;
   }

   return 0;
 }
</code></pre>

<p>So far it working fine. Is there any leak within procedure when it may not work as intended in some situations ? Would like  to know about those scenarios ...</p>

<p>Thanks </p>

CC BY-SA 3.0
2016-05-08T10:17:12.200Z
explain your API. I would expect to see something like vertices A1,B1, A2,B2. Beside, what about the case they are not even aligned ? what kind of error threshold do you plan ?
Fabrice NEYRET
CC BY-SA 3.0
2016-05-09T15:40:48.973Z
will it solve the issue if d1, d2 , d3 and d3 are stored as 64-bit integer type instead ?
sajis997
<algorithm>
Check if one line segments is entirely on the other line segment
sajis997
# Check if one line segments is entirely on the other line segment

(Asked by: username_0 on 2016-05-06)

I am trying to find an algorithm as mentioned in the subject. It is as follows:

```
 int orientation(glm::i16vec2 &p1,
                 glm::i16vec2 &p2,
                 glm::i16vec2 &p3)
 {
   return (((p3.x - p1.x) * (p2.y - p1.y)) - ((p2.x - p1.x) * (p3.y - p1.y)));
 }

 bool onSegment(const glm::i16vec2 &pi,
                const glm::i16vec2 &pj,
                const glm::i16vec2 &pk)
 {
   if((std::min(pi.x,pj.x) <= pk.x && pk.x <= std::max(pi.x,pj.x)) &&
      (std::min(pi.y,pj.y) <= pk.y && pk.y <= std::max(pi.y,pj.y)))
    return true;
   else
    return false;
 }

 int main()
 {
   glm::i16vec2 hs(5,2);  // p1
   glm::i16vec2 he(5,3); // p2

   glm::i16vec2 cs(5,1);   // p3
   glm::i16vec2 ce(5,3); // p4

   int d1 = orientation(cs,ce,hs);
   int d2 = orientation(cs,ce,he);
   int d3 = orientation(hs,he,cs);
   int d4 = orientation(hs,he,ce);

   if((d1 == 0 && d2 == 0 && d3 == 0 && d4 == 0) &&
        (onSegment(cs,ce,hs) && onSegment(cs,ce,he)))
   {
    std::cout << "HPs is entirely inside CPs" << std::endl;
   }

   return 0;
 }

```

So far it working fine. Is there any leak within procedure when it may not work as intended in some situations ? Would like to know about those scenarios ...

Thanks

**Tags:** algorithm

> **Comments:**
> **username_1**: explain your API. I would expect to see something like vertices A1,B1, A2,B2. Beside, what about the case they are not even aligned ? what kind of error threshold do you plan ?  
> **username_0**: will it solve the issue if d1, d2 , d3 and d3 are stored as 64-bit integer type instead ?  

---

## 0 answers

