Determine if two triangles overlap: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 24:
{{trans|D}}
 
<langsyntaxhighlight lang="11l">T Triangle
(Float, Float) p1, p2, p3
 
Line 125:
print(t1" and\n"t2)
print(‘which have only a single corner in contact, if boundary points do not collide’)
overlap(&t1, &t2, 0.0, 0B, 0B)</langsyntaxhighlight>
 
{{out}}
Line 165:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">
WITH Ada.Text_IO; USE Ada.Text_IO;
 
Line 205:
Put ((0.0, 0.0, 1.0, 0.0, 0.0, 1.0), (1.0, 0.0, 2.0, 0.0, 1.0, 1.0));
END Main;
</syntaxhighlight>
</lang>
{{out}}
<pre>true
Line 219:
{{Trans|Kotlin}}
... with different output format (based on Modula 2).
<langsyntaxhighlight lang="algolw">begin % determine if two triangles overlap %
record Point ( real x, y );
record Triangle ( reference(Point) p1, p2, p3 );
Line 341:
CheckOverlap( t1, t2, 0.0, false, false );
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 355:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">TrianglesIntersect(T1, T2){ ; T1 := [[x1,y1],[x2,y2],[x3,y3]] , T2 :=[[x4,y4],[x5,y5],[x6,y6]]
counter := 0
for i, Pt in T1
Line 399:
isBetween(x, p1, p2){
return !((x>p1 && x>p2) || (x<p1 && x<p2))
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">result := ""
result .= TrianglesIntersect([[0,0],[5,0],[0,5]], [[0,0],[5,0],[0,6]]) "`n"
result .= TrianglesIntersect([[0,0],[0,5],[5,0]], [[0,0],[0,5],[5,0]]) "`n"
Line 409:
result .= TrianglesIntersect([[0,0],[1,0],[0,1]], [[1,0],[2,0],[1,1]]) "`n"
MsgBox % result
return</langsyntaxhighlight>
Outputs:<pre>1
1
Line 420:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
Line 563:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
{{out}}
<pre>1,true
Line 575:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 727:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Triangle: (0, 0), (5, 0), (0, 5) and
Line 764:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
#include <stdexcept>
Line 883:
cout << TriTri2D(t1, t2, 0.0, false, false) << "," << false << endl;}
 
}</langsyntaxhighlight>
 
{{out}}
Line 897:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.stdio;
import std.typecons;
 
Line 1,037:
writeln("which have only a single corner in contact, if boundary points do not collide");
overlap(t1, t2, 0.0, false, false);
}</langsyntaxhighlight>
 
{{out}}
Line 1,078:
=={{header|F Sharp|F#}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="fsharp">open System
 
type Point = double * double
Line 1,176:
Console.WriteLine("{0} and\n{1}\nwhich have only a single corner in contact, if boundary points do not collide\n{2}", t11, t12, if TriTri2D 0.0 false false t11 t12 then "overlap" else "do not overlap")
 
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>((0, 0), (5, 0), (0, 5)) and
Line 1,213:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#macro min(x,y)
Iif(x>y,y,x)
#endmacro
Line 1,306:
print triangle_overlap( a(), b() )
next t
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,320:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,467:
overlapping = triTri2D(t1, t2, 0.0, false, false)
fmt.Println(iff(overlapping, "overlap", "do not overlap"))
}</langsyntaxhighlight>
 
{{out}}
Line 1,476:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">import java.util.function.BiFunction
 
class TriangleOverlap {
Line 1,649:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Triangle: (0.0, 0.0), (5.0, 0.0), (0.0, 5.0) and
Line 1,688:
Uses the solution of the task [[Find_if_a_point_is_within_a_triangle#Haskell]]
 
<langsyntaxhighlight lang="haskell">isOverlapping :: Triangle Double -> Triangle Double -> Bool
isOverlapping t1 t2 = vertexInside || midLineInside
where
Line 1,721:
, (Triangle (0,0) (1,1) (0,2), Triangle (2,1) (3,0) (3,2))
, (Triangle (0,0) (1,1) (0,2), Triangle (2,1) (3,-2) (3,4))
, (Triangle (0,0) (1,0) (0,1), Triangle (1,0) (2,0) (1,1))]</langsyntaxhighlight>
 
<pre>λ> test
Line 1,729:
{{trans|Kotlin}}
{{works with|Java|8}}
<langsyntaxhighlight Javalang="java">import java.util.function.BiFunction;
 
public class TriangleOverlap {
Line 1,904:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Triangle: (0.0, 0.0), (5.0, 0.0), (0.0, 5.0) and
Line 1,944:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq"># Points are realized as arrays of two numbers [x, y]
# Triangles are realized as triples of Points [p1, p2, p3]
Line 2,007:
else true # The triangles overlap
end
end ;</langsyntaxhighlight>
'''The Task'''
<langsyntaxhighlight lang="jq">def task:
def t: "Triangle: ";
def printTris(t1; t2; nl):
Line 2,059:
if triTri2D($t1; $t2; 0; false; false) then "overlap" else "do not overlap" end) );
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 2,100:
{{trans|Python}}
'''Module''':
<langsyntaxhighlight lang="julia">module Triangles
 
using LinearAlgebra
Line 2,151:
end
 
end # module Triangles</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight lang="julia">using .Triangles
 
t1 = [0 0; 5 0; 0 5]
Line 2,188:
t1 = [0 0; 1 0; 0 1]
t2 = [1 0; 2 0; 1 1]
@show Triangles.overlap(t1, t2, MildCheck())</langsyntaxhighlight>
 
{{out}}
Line 2,202:
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.1.0
 
typealias Point = Pair<Double, Double>
Line 2,307:
println("which have only a single corner in contact, if boundary points do not collide")
println(if (triTri2D(t1, t2, 0.0, false, false)) "overlap" else "do not overlap")
}</langsyntaxhighlight>
 
{{out}}
Line 2,347:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
Here we present a rasterized version based on a single function "isInside".
 
Line 2,473:
.............................11
..............................1
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
{{trans|C++}}
<langsyntaxhighlight lang="lua">function det2D(p1,p2,p3)
return p1.x * (p2.y - p3.y)
+ p2.x * (p3.y - p1.y)
Line 2,616:
print(formatTri(t1).." and")
print(formatTri(t2))
print(overlap(t1,t2,0.0,false,false))</langsyntaxhighlight>
{{out}}
<pre>Triangle: (0, 0), (5, 0), (0, 5) and
Line 2,652:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">p1 = Polygon@{{0, 0}, {5, 0}, {0, 5}};
p2 = Polygon@{{0, 0}, {5, 0}, {0, 6}};
! RegionDisjoint[p1, p2]
Line 2,678:
p1 = Polygon@{{0, 0}, {1, 0}, {0, 1}};
p2 = Polygon@{{1, 0}, {2, 0}, {1, 1}};
! RegionDisjoint[p1, p2]</langsyntaxhighlight>
{{out}}
<pre>True
Line 2,689:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Overlap;
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE;
FROM LongStr IMPORT RealToFixed;
Line 2,874:
 
ReadChar
END Overlap.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
type Point = tuple[x, y: float]
Line 2,991:
echo "which have only a single corner in contact, if boundary points do not collide"
overlapping = triTri2D(t1, t2, 0, false, false)
echo if overlapping: "overlap\n" else: "do not overlap\n"</langsyntaxhighlight>
 
{{out}}
Line 3,029:
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/*--------------------------------------------------------------------
* Determine if two triangles overlap
* Fully (?) tested with integer coordinates of the 6 corners
Line 3,575:
res=res word(list,i)
End
Return res </langsyntaxhighlight>
{{out}}
<pre>0 0 4 0 0 4 1 1 2 1 1 2
Line 3,915:
=={{header|Pascal}}==
A console application in Free Pascal, created with the Lazarus IDE. It recognizes three possible outcomes: disjoint, positive overlap, and borderline (overlap at a point or line segment).
<langsyntaxhighlight lang="pascal">
program TrianglesOverlap;
{
Line 4,053:
TestTrianglePair( 0,0,1,0,0,1, 1,0,2,0,1,1);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,067:
=={{header|Perl}}==
===Port of Lua===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 4,228:
@t1 = ({x=>0, y=>0}, {x=>1, y=>0}, {x=>0, y=>1});
@t2 = ({x=>1, y=>0}, {x=>2, y=>0}, {x=>1, y=>1});
print formatTri(\@t1), " and\n", formatTri(\@t2), "\n", overlap(\@t1, \@t2, 0.0, 0, 0), "\n";</langsyntaxhighlight>
{{out}}
<pre>Triangle: (0, 0), (5, 0), (0, 5) and
Line 4,263:
 
===More Idiomatic===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 4,347:
);
 
say overlap(\$_->[0], \$_->[1], $_->[2], $_->[3], $_->[4]) for @tests;</langsyntaxhighlight>
{{out}}
<pre> overlap: (0,0), (5,0), (0,5) and (0,0), (5,0), (0,6)
Line 4,361:
{{trans|zkl}}
Plus draw all eight pairs of triangles for visual confirmation.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Determine_if_two_triangles_overlap.exw
Line 4,486:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
{{libheader|NumPy}}
Using numpy:
<langsyntaxhighlight lang="python">from __future__ import print_function
import numpy as np
 
Line 4,576:
t1 = [[0,0],[1,0],[0,1]]
t2 = [[1,0],[2,0],[1,1]]
print (TriTri2D(t1, t2, onBoundary = False), False)</langsyntaxhighlight>
 
{{out}}
Line 4,589:
{{libheader|Shapely}}
Using shapely:
<langsyntaxhighlight lang="python">from __future__ import print_function
from shapely.geometry import Polygon
 
Line 4,625:
t1 = [[0,0],[1,0],[0,1]]
t2 = [[1,0],[2,0],[1,1]]
print (PolyOverlaps(t1, t2), "?")</langsyntaxhighlight>
 
{{out}}
Line 4,637:
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
<lang QB64>
DATA 0,0,5,0,0,5,0,0,5,0,0,6
DATA 0,0,0,5,5,0,0,0,0,5,5,0
Line 4,715:
p.y = (10 + p.y) * 30
END SUB
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
{{trans|Zkl}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
;; A triangle is a list of three pairs of points: '((x . y) (x . y) (x . y))
Line 4,783:
(check-true (tri-tri-2d? c1 c2 #:on-boundary? #t))
(check-false (tri-tri-2d? c1 c2 #:on-boundary? #f))))
</syntaxhighlight>
</lang>
 
No output &rarr; all tests passed
Line 4,790:
(formerly Perl 6)
First, check if any vertex is inside each other triangle (that should cover most overlapping cases including enclosures). Then see if an edge of triangle A intersects any of two edges of B (for shapes like Star of David [https://en.wikipedia.org/wiki/Star_of_David])
<syntaxhighlight lang="raku" perl6line># Reference:
# https://stackoverflow.com/questions/2049582/how-to-determine-if-a-point-is-in-a-2d-triangle
# https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
Line 4,867:
];
 
if-overlap $_ for DATA ;</langsyntaxhighlight>
{{out}}
<pre>[(0 0) (5 0) (0 5)] and [(0 0) (5 0) (0 6)] do overlap.
Line 4,879:
=={{header|REXX}}==
Note: The triangles must be real triangles (no edge of length 0)
<langsyntaxhighlight lang="rexx">/* REXX */
Signal On Halt
Signal On Novalue
Line 5,455:
Nop
End
Exit 12</langsyntaxhighlight>
{{out}}
<pre>ABC: (0/0) (5/0) (0/5)
Line 5,531:
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang="ruby">require "matrix"
 
def det2D(p1, p2, p3)
Line 5,648:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>Triangle: [Vector[0, 0], Vector[5, 0], Vector[0, 5]]
Line 5,684:
=={{header|Scala}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="scala">object Overlap {
type Point = (Double, Double)
 
Line 5,787:
println(if (triTri2D(t1, t2, onBoundary = false)) "overlap" else "do not overlap")
}
}</langsyntaxhighlight>
{{out}}
<pre>Triangle: (0.0,0.0), (5.0,0.0), (0.0,5.0) and
Line 5,825:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Class Triangle
Line 5,973:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Triangle: (0, 0), (5, 0), (0, 5) and
Line 6,011:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">struct Point {
x f64
y f64
Line 6,150:
overlapping = tri_tri_2d(mut t1, mut t2, 0.0, false, false)
println(iff(overlapping, "overlap", "do not overlap"))
}</langsyntaxhighlight>
 
{{out}}
Line 6,158:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<langsyntaxhighlight lang="ecmascript">import "/dynamic" for Tuple, Struct
 
var Point = Tuple.create("Point", ["x", "y"])
Line 6,259:
printTris.call(t1, t2, "\n")
System.print("which have only a single corner in contact, if boundary points do not collide")
System.print(triTri2D.call(t1, t2, 0, false, false) ? "overlap" : "do not overlap")</langsyntaxhighlight>
 
{{out}}
Line 6,300:
=={{header|zkl}}==
{{trans|C++}}
<langsyntaxhighlight lang="zkl">// A triangle is three pairs of points: ( (x,y), (x,y), (x,y) )
 
fcn det2D(triangle){
Line 6,339:
}
True // The triangles collide
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn toTri(ax,ay,bx,by,cx,cy){ //-->( (ax,ay),(bx,by),(cx,cy) )
vm.arglist.apply("toFloat").pump(List,Void.Read)
}
Line 6,364:
c1,c2 := toTri(0,0, 1,0, 0,1), toTri(1,0, 2,0, 1,1);
println("Corner case (barely touching): ",triTri2D(c1,c2,0.0,False,True)); // True
println("Corner case (barely touching): ",triTri2D(c1,c2,0.0,False,False)); // False</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits