Determine if two triangles overlap: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 24:
{{trans|D}}
<
(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)</
{{out}}
Line 165:
=={{header|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>
{{out}}
<pre>true
Line 219:
{{Trans|Kotlin}}
... with different output format (based on Modula 2).
<
record Point ( real x, y );
record Triangle ( reference(Point) p1, p2, p3 );
Line 341:
CheckOverlap( t1, t2, 0.0, false, false );
end
end.</
{{out}}
<pre>
Line 355:
=={{header|AutoHotkey}}==
<
counter := 0
for i, Pt in T1
Line 399:
isBetween(x, p1, p2){
return !((x>p1 && x>p2) || (x<p1 && x<p2))
}</
Examples:<
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</
Outputs:<pre>1
1
Line 420:
=={{header|C}}==
{{trans|C++}}
<
#include <stdbool.h>
#include <stdio.h>
Line 563:
return EXIT_SUCCESS;
}</
{{out}}
<pre>1,true
Line 575:
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
Line 727:
}
}
}</
{{out}}
<pre>Triangle: (0, 0), (5, 0), (0, 5) and
Line 764:
=={{header|C++}}==
<
#include <iostream>
#include <stdexcept>
Line 883:
cout << TriTri2D(t1, t2, 0.0, false, false) << "," << false << endl;}
}</
{{out}}
Line 897:
=={{header|D}}==
{{trans|Kotlin}}
<
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);
}</
{{out}}
Line 1,078:
=={{header|F Sharp|F#}}==
{{trans|Kotlin}}
<
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</
{{out}}
<pre>((0, 0), (5, 0), (0, 5)) and
Line 1,213:
=={{header|FreeBASIC}}==
<
Iif(x>y,y,x)
#endmacro
Line 1,306:
print triangle_overlap( a(), b() )
next t
</syntaxhighlight>
{{out}}
<pre>
Line 1,320:
=={{header|Go}}==
{{trans|Kotlin}}
<
import "fmt"
Line 1,467:
overlapping = triTri2D(t1, t2, 0.0, false, false)
fmt.Println(iff(overlapping, "overlap", "do not overlap"))
}</
{{out}}
Line 1,476:
=={{header|Groovy}}==
{{trans|Java}}
<
class TriangleOverlap {
Line 1,649:
}
}
}</
{{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]]
<
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))]</
<pre>λ> test
Line 1,729:
{{trans|Kotlin}}
{{works with|Java|8}}
<
public class TriangleOverlap {
Line 1,904:
}
}
}</
{{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'''
<
# Triangles are realized as triples of Points [p1, p2, p3]
Line 2,007:
else true # The triangles overlap
end
end ;</
'''The 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</
{{out}}
<pre>
Line 2,100:
{{trans|Python}}
'''Module''':
<
using LinearAlgebra
Line 2,151:
end
end # module Triangles</
'''Main''':
<
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())</
{{out}}
Line 2,202:
=={{header|Kotlin}}==
{{trans|C++}}
<
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")
}</
{{out}}
Line 2,347:
=={{header|Lambdatalk}}==
<
Here we present a rasterized version based on a single function "isInside".
Line 2,473:
.............................11
..............................1
</syntaxhighlight>
=={{header|Lua}}==
{{trans|C++}}
<
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))</
{{out}}
<pre>Triangle: (0, 0), (5, 0), (0, 5) and
Line 2,652:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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]</
{{out}}
<pre>True
Line 2,689:
=={{header|Modula-2}}==
<
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE;
FROM LongStr IMPORT RealToFixed;
Line 2,874:
ReadChar
END Overlap.</
=={{header|Nim}}==
{{trans|Go}}
<
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"</
{{out}}
Line 3,029:
=={{header|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 </
{{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).
<
program TrianglesOverlap;
{
Line 4,053:
TestTrianglePair( 0,0,1,0,0,1, 1,0,2,0,1,1);
end.
</syntaxhighlight>
{{out}}
<pre>
Line 4,067:
=={{header|Perl}}==
===Port of Lua===
<
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";</
{{out}}
<pre>Triangle: (0, 0), (5, 0), (0, 5) and
Line 4,263:
===More Idiomatic===
<
use warnings;
use feature 'say';
Line 4,347:
);
say overlap(\$_->[0], \$_->[1], $_->[2], $_->[3], $_->[4]) for @tests;</
{{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.
<!--<
<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>
<!--</
=={{header|Python}}==
{{libheader|NumPy}}
Using numpy:
<
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)</
{{out}}
Line 4,589:
{{libheader|Shapely}}
Using shapely:
<
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), "?")</
{{out}}
Line 4,637:
=={{header|QB64}}==
<syntaxhighlight 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>
=={{header|Racket}}==
{{trans|Zkl}}
<
;; 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>
No output → 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"
# 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 ;</
{{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)
<
Signal On Halt
Signal On Novalue
Line 5,455:
Nop
End
Exit 12</
{{out}}
<pre>ABC: (0/0) (5/0) (0/5)
Line 5,531:
=={{header|Ruby}}==
{{trans|C}}
<
def det2D(p1, p2, p3)
Line 5,648:
end
main()</
{{out}}
<pre>Triangle: [Vector[0, 0], Vector[5, 0], Vector[0, 5]]
Line 5,684:
=={{header|Scala}}==
{{trans|Kotlin}}
<
type Point = (Double, Double)
Line 5,787:
println(if (triTri2D(t1, t2, onBoundary = false)) "overlap" else "do not overlap")
}
}</
{{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#}}
<
Class Triangle
Line 5,973:
End Sub
End Module</
{{out}}
<pre>Triangle: (0, 0), (5, 0), (0, 5) and
Line 6,011:
=={{header|Vlang}}==
{{trans|Go}}
<
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"))
}</
{{out}}
Line 6,158:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<
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")</
{{out}}
Line 6,300:
=={{header|zkl}}==
{{trans|C++}}
<
fcn det2D(triangle){
Line 6,339:
}
True // The triangles collide
}</
<
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</
{{out}}
<pre>
|