Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ : check if two triangles intersect or not
    primarykey
    data
    text
    <p>Problem : </p> <p>Given the vertices of two triangles, check whether both of them have any common interior point. No points on the edges or vertices are considered interior to a triangle.<br> I think this is a problem to find whether two triangles intersect or not. </p> <p>My idea is : </p> <pre><code>1. Make three line segments for each of the two triangles 2. For each pair of line segment (A,B) (where A is in triangle #1 and B is in Triangle #2) check whether they intersect or not. 3. If any pair of segments intersect, then the two triangles have a interior point. Otherwise not . </code></pre> <p>My code :</p> <pre><code>#include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;cstdio&gt; #include &lt;cmath&gt; #define READ freopen("in.txt","r",stdin) #define ui64 unsigned long long int #define i64 long long int using namespace std; class Point{ public: i64 x, y; Point() : x(0), y(0) {} Point(int a, i64 b) : x(a), y(b) {} }; class Segment{ public: Point a, b; Segment() : a(0,0), b(0,0) {} Segment(Point e, Point r) : a(e), b(r) {} Segment(const Segment &amp;s){ a = s.a; b = s.b; } i64 Direction(const Point &amp;p){ return (p.x - a.x) * (b.y - a.y) - (b.x - a.x) * (p.y - a.y); } inline bool inside(int a,int b,int c){ return a&lt;=c &amp;&amp; c&lt;=b; } bool onSegment(const Point &amp;p){ return inside(min(a.x,b.x), max(a.x,b.x), p.x) &amp;&amp; inside(min(a.y,b.y), max(a.y,b.y), p.y); } bool Intersect(Segment &amp;s){ int d1 = this-&gt;Direction(s.a); int d2 = this-&gt;Direction(s.b); int d3 = s.Direction(a); int d4 = s.Direction(b); if(((d1&gt;0 &amp;&amp; d2&lt;0) || (d1&lt;0 &amp;&amp; d2&gt;0)) &amp;&amp; ((d3&gt;0 &amp;&amp; d4&lt;0) || (d3&lt;0 &amp;&amp; d4&gt;0))) return true; //check if the two segments just touch each other but don't cross //i ignored this because as the problem said ... //No points on the edges or vertices are considered interior to a triangle. /*if(!d1 &amp;&amp; this-&gt;onSegment(s.a)) return true; if(!d2 &amp;&amp; this-&gt;onSegment(s.b)) return true; if(!d3 &amp;&amp; s.onSegment(a)) return true; if(!d4 &amp;&amp; s.onSegment(b)) return true;*/ return false; } }; Point p1[3], p2[3]; Segment s1[3], s2[3]; bool check() { for(int i=0;i&lt;3;i++) for(int j=0;j&lt;3;j++) if(s1[i].Intersect(s2[j])) return true; return false; } int main() { //READ; int t, ts=0; scanf("%d",&amp;t);//number of test cases while(t--){ //points for first triangle for(int i=0;i&lt;3;i++){ scanf("%lld %lld",&amp;p1[i].x, &amp;p1[i].y); } //points for second triangle for(int i=0;i&lt;3;i++){ scanf("%lld %lld",&amp;p2[i].x, &amp;p2[i].y); } for(int i=0;i&lt;3;i++){ s1[i] = Segment(p1[i], p1[(i+1)%3]); s2[i] = Segment(p2[i], p2[(i+1)%3]); } printf("pair %d: %s\n",++ts, check() ? "yes" : "no"); } return 0; } </code></pre> <p>However for this input .. </p> <pre><code>1 0 0 5 0 2 4 4 0 5 0 -4 16 </code></pre> <p>My program gives output</p> <pre><code>no </code></pre> <p>but the correct answer is </p> <pre><code>yes </code></pre> <p>and there are many cases for which my program doesn't work.<br> I checked my Segment class for other program and it worked fine.<br> But in this , i can't find the mistake.<br> Please help. </p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload