Find if a point is within a triangle: Difference between revisions
Content added Content deleted
(J) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 32: | Line 32: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="11l">V EPS = 0.001 |
||
V EPS_SQUARE = EPS * EPS |
V EPS_SQUARE = EPS * EPS |
||
Line 104: | Line 104: | ||
p3 = (-100.0 / 8, 100.0 / 6) |
p3 = (-100.0 / 8, 100.0 / 6) |
||
tri = Triangle(p1, p2, p3) |
tri = Triangle(p1, p2, p3) |
||
test(tri, pt)</ |
test(tri, pt)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 129: | Line 129: | ||
It uses a generic two-dimensional geometry, that could be affine, euclidean, or a lot stranger than that. You only need to specify the type of one dimension, and the library ''should'' handle the rest. Edge cases probably exist where they shouldn't, as the area formula might add some imprecision. |
It uses a generic two-dimensional geometry, that could be affine, euclidean, or a lot stranger than that. You only need to specify the type of one dimension, and the library ''should'' handle the rest. Edge cases probably exist where they shouldn't, as the area formula might add some imprecision. |
||
< |
<syntaxhighlight lang="ada">-- triangle.ads |
||
generic |
generic |
||
type Dimension is private; |
type Dimension is private; |
||
Line 163: | Line 163: | ||
with Inline; |
with Inline; |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="ada">-- triangle.adb |
||
package body Triangle is |
package body Triangle is |
||
Line 185: | Line 185: | ||
end IsPointInTriangle; |
end IsPointInTriangle; |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="ada">-- test_triangle.adb |
||
with Ada.Text_IO; |
with Ada.Text_IO; |
||
use Ada.Text_IO; |
use Ada.Text_IO; |
||
Line 202: | Line 202: | ||
Put_Line("IsPointInTriangle("&Image(origin)&", "&Image(tri2)&") yields "&IsPointInTriangle(origin,tri2)'Image); |
Put_Line("IsPointInTriangle("&Image(origin)&", "&Image(tri2)&") yields "&IsPointInTriangle(origin,tri2)'Image); |
||
end test_triangle; |
end test_triangle; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 214: | Line 214: | ||
With additional material |
With additional material |
||
{{Trans|Wren}} |
{{Trans|Wren}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN # determine whether a point is within a triangle or not # |
||
# tolerance for the accurate test # |
# tolerance for the accurate test # |
||
REAL eps = 0.001; |
REAL eps = 0.001; |
||
Line 287: | Line 287: | ||
, ( 0.1, 0.11111111111111 ), ( 12.5, 33.333333333333 ), ( -12.5, 16.666666666667 ) |
, ( 0.1, 0.11111111111111 ), ( 12.5, 33.333333333333 ), ( -12.5, 16.666666666667 ) |
||
) |
) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 300: | Line 300: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">T := [[1.5, 2.4], [5.1, -3.1], [-3.8, 1.2]] |
||
for i, p in [[0, 0], [0, 1], [3, 1], [5.4142857, 14.349206]] |
for i, p in [[0, 0], [0, 1], [3, 1], [5.4142857, 14.349206]] |
||
result .= "[" p.1 ", " p.2 "] is within triangle?`t" (TriHasP(T, p) ? "ture" : "false") "`n" |
result .= "[" p.1 ", " p.2 "] is within triangle?`t" (TriHasP(T, p) ? "ture" : "false") "`n" |
||
Line 315: | Line 315: | ||
TriArea(x1, y1, x2, y2, x3, y3){ |
TriArea(x1, y1, x2, y2, x3, y3){ |
||
return Abs((x1 * (y2-y3) + x2 * (y3-y1) + x3 * (y1-y2)) / 2) |
return Abs((x1 * (y2-y3) + x2 * (y3-y1) + x3 * (y1-y2)) / 2) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[0, 0] is within triangle? ture |
<pre>[0, 0] is within triangle? ture |
||
Line 324: | Line 324: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="c">#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 421: | Line 421: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Triangle is [(1.500000, 2.400000), (5.100000, -3.100000), (-3.800000, 1.200000)] |
<pre>Triangle is [(1.500000, 2.400000), (5.100000, -3.100000), (-3.800000, 1.200000)] |
||
Line 438: | Line 438: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
const double EPS = 0.001; |
const double EPS = 0.001; |
||
Line 533: | Line 533: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)] |
<pre>Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)] |
||
Line 550: | Line 550: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight lang="lisp"> |
|||
<lang Lisp> |
|||
; There are different algorithms to solve this problem, such as adding areas, adding angles, etc... but these |
; There are different algorithms to solve this problem, such as adding areas, adding angles, etc... but these |
||
; solutions are sensitive to rounding errors intrinsic to float operations. We want to avoid these issues, therefore we |
; solutions are sensitive to rounding errors intrinsic to float operations. We want to avoid these issues, therefore we |
||
Line 581: | Line 581: | ||
(- (car P) (car A)) )))) |
(- (car P) (car A)) )))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 602: | Line 602: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="d">import std.algorithm; //.comparison for min and max |
||
import std.stdio; |
import std.stdio; |
||
Line 692: | Line 692: | ||
test(0.1, 0.1111111111111111, 12.5, 33.333333333333336, -12.5, 16.666666666666668, 5.414285714285714, 14.349206349206348); |
test(0.1, 0.1111111111111111, 12.5, 33.333333333333336, -12.5, 16.666666666666668, 5.414285714285714, 14.349206349206348); |
||
writeln; |
writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)] |
<pre>Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)] |
||
Line 709: | Line 709: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Uses the parametric equations method from [https://totologic.blogspot.com/2014/01/accurate-point-in-triangle-test.html]. |
Uses the parametric equations method from [https://totologic.blogspot.com/2014/01/accurate-point-in-triangle-test.html]. |
||
< |
<syntaxhighlight lang="factor">USING: accessors fry io kernel locals math math.order sequences ; |
||
TUPLE: point x y ; |
TUPLE: point x y ; |
||
Line 734: | Line 734: | ||
3 3 <point> 16 10 <point> 10 16 <point> <triangle> ! Make a triangle |
3 3 <point> 16 10 <point> 10 16 <point> <triangle> ! Make a triangle |
||
'[ [ _ point-in-triangle? "#" "." ? write ] each nl ] each nl ! Show points inside the triangle with '#' |
'[ [ _ point-in-triangle? "#" "." ? write ] each nl ] each nl ! Show points inside the triangle with '#' |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 760: | Line 760: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
<syntaxhighlight lang="fortran"> |
|||
<lang Fortran> |
|||
PROGRAM POINT_WITHIN_TRIANGLE |
PROGRAM POINT_WITHIN_TRIANGLE |
||
Line 819: | Line 819: | ||
END PROGRAM POINT_WITHIN_TRIANGLE |
END PROGRAM POINT_WITHIN_TRIANGLE |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
Point ( 0.0000000000000000 , 0.0000000000000000 ) is within triangle [( 1.5000000000000000 , 2.4000000953674316 ), ( 5.0999999046325684 , -3.0999999046325684 ), ( -3.7999999523162842 , 1.2000000476837158 )]. |
Point ( 0.0000000000000000 , 0.0000000000000000 ) is within triangle [( 1.5000000000000000 , 2.4000000953674316 ), ( 5.0999999046325684 , -3.0999999046325684 ), ( -3.7999999523162842 , 1.2000000476837158 )]. |
||
Line 825: | Line 825: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
type p2d |
type p2d |
||
x as double 'define a two-dimensional point |
x as double 'define a two-dimensional point |
||
Line 855: | Line 855: | ||
print |
print |
||
next y |
next y |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre>........................................ |
{{out}}<pre>........................................ |
||
........................................ |
........................................ |
||
Line 880: | Line 880: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 971: | Line 971: | ||
within = accuratePointInTriangle(x1, y1, x2, y2, x3, y3, x, y) |
within = accuratePointInTriangle(x1, y1, x2, y2, x3, y3, x, y) |
||
fmt.Println("Point", pt, "is within triangle ?", within) |
fmt.Println("Point", pt, "is within triangle ?", within) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 988: | Line 988: | ||
=={{header|GW-BASIC}}== |
=={{header|GW-BASIC}}== |
||
< |
<syntaxhighlight lang="gwbasic">10 PIT1X! = 3 : PIT1Y! = 1.3 : REM arbitrary triangle for demonstration |
||
20 PIT2X! = 17.222 : PIT2Y! = 10 |
20 PIT2X! = 17.222 : PIT2Y! = 10 |
||
30 PIT3X! = 5.5 : PIT3Y! = 18.212 |
30 PIT3X! = 5.5 : PIT3Y! = 18.212 |
||
Line 1,012: | Line 1,012: | ||
1110 IF PITXXS!+PITXXT!>=1 THEN RETURN |
1110 IF PITXXS!+PITXXT!>=1 THEN RETURN |
||
1120 PITRES% = 1 |
1120 PITRES% = 1 |
||
1130 RETURN</ |
1130 RETURN</syntaxhighlight> |
||
{{out}}<pre>......................................... |
{{out}}<pre>......................................... |
||
......................................... |
......................................... |
||
Line 1,039: | Line 1,039: | ||
The point to be tested is transformed by affine transformation which turns given triangle to the simplex: Triangle (0,0) (0,s) (s,0), where s is half of the triangles' area. After that criteria of overlapping become trivial. Affinity allows to avoid division, so all functions work for points on the integer, or rational, or even modular meshes as well. |
The point to be tested is transformed by affine transformation which turns given triangle to the simplex: Triangle (0,0) (0,s) (s,0), where s is half of the triangles' area. After that criteria of overlapping become trivial. Affinity allows to avoid division, so all functions work for points on the integer, or rational, or even modular meshes as well. |
||
< |
<syntaxhighlight lang="haskell">type Pt a = (a, a) |
||
data Overlapping = Inside | Outside | Boundary |
data Overlapping = Inside | Outside | Boundary |
||
Line 1,070: | Line 1,070: | ||
(y <= s && y >= 0) && |
(y <= s && y >= 0) && |
||
(x == 0 || y == 0 || y == s - x) -> Boundary |
(x == 0 || y == 0 || y == s - x) -> Boundary |
||
| otherwise -> Outside</ |
| otherwise -> Outside</syntaxhighlight> |
||
Testing |
Testing |
||
< |
<syntaxhighlight lang="haskell">tests = let |
||
t1 = Triangle (2,0) (-1,2) (-2,-2) |
t1 = Triangle (2,0) (-1,2) (-2,-2) |
||
bs = [(2,0), (-1,2), (-2,-2), (0,-1), (1/2,1), (-3/2,0)] |
bs = [(2,0), (-1,2), (-2,-2), (0,-1), (1/2,1), (-3/2,0)] |
||
Line 1,093: | Line 1,093: | ||
| i <- [-10..10] :: [Int] ] |
| i <- [-10..10] :: [Int] ] |
||
| j <- [-5..5] :: [Int] ] |
| j <- [-5..5] :: [Int] ] |
||
where t = Triangle (-8,-3) (8,1) (-1,4)</ |
where t = Triangle (-8,-3) (8,1) (-1,4)</syntaxhighlight> |
||
<pre>λ> tests |
<pre>λ> tests |
||
Line 1,116: | Line 1,116: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Implementation, using complex numbers to represent x,y coordinates: |
Implementation, using complex numbers to represent x,y coordinates: |
||
< |
<syntaxhighlight lang="j">area=: [:| 0.5-/ .*@,.+. NB. signed area of triangle |
||
I3=: =i.3 NB. identity matrix |
I3=: =i.3 NB. identity matrix |
||
inside=: {{ (area y)=+/area"1|:(I3*x)+(1-I3)*y }}</ |
inside=: {{ (area y)=+/area"1|:(I3*x)+(1-I3)*y }}</syntaxhighlight> |
||
This is based on the algorithm documented for the ada implementation: compute the area of triangles using the determinant method (we want the absolute area here), and check whether the triangles formed with the test point and the sides of the test triangle matches the area of the test triangle. |
This is based on the algorithm documented for the ada implementation: compute the area of triangles using the determinant method (we want the absolute area here), and check whether the triangles formed with the test point and the sides of the test triangle matches the area of the test triangle. |
||
Examples:< |
Examples:<syntaxhighlight lang="j"> 0j0 inside 1.5j2.4 5.1j_3.1 _3.8j1.2 |
||
1 |
1 |
||
0j1 inside 1.5j2.4 5.1j_3.1 _3.8j1.2 |
0j1 inside 1.5j2.4 5.1j_3.1 _3.8j1.2 |
||
Line 1,131: | Line 1,131: | ||
1 |
1 |
||
5.414285714285714j14.349206349206348 inside 0.1j1r9 12.5j100r3 _12.5j100r6 |
5.414285714285714j14.349206349206348 inside 0.1j1r9 12.5j100r3 _12.5j100r6 |
||
1</ |
1</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="java">import java.util.Objects; |
||
public class FindTriangle { |
public class FindTriangle { |
||
Line 1,270: | Line 1,270: | ||
test(tri, pt); |
test(tri, pt); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Triangle[(1.500000, 2.400000), (5.100000, -3.100000), (-3.800000, 1.200000)] |
<pre>Triangle[(1.500000, 2.400000), (5.100000, -3.100000), (-3.800000, 1.200000)] |
||
Line 1,296: | Line 1,296: | ||
'''Preliminaries''' |
'''Preliminaries''' |
||
< |
<syntaxhighlight lang="jq">def sum_of_squares(stream): reduce stream as $x (0; . + $x * $x); |
||
def distanceSquared(P1; P2): sum_of_squares(P1[0]-P2[0], P1[1]-P2[1]); |
def distanceSquared(P1; P2): sum_of_squares(P1[0]-P2[0], P1[1]-P2[1]); |
||
Line 1,310: | Line 1,310: | ||
def EPS: 0.001; |
def EPS: 0.001; |
||
def EPS_SQUARE: EPS * EPS; </ |
def EPS_SQUARE: EPS * EPS; </syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">def side(P1; P2; Q): |
||
[P1, P2, Q] |
[P1, P2, Q] |
||
| xy |
| xy |
||
Line 1,349: | Line 1,349: | ||
elif distanceSquarePointToSegment(P3; P1; Q) <= EPS_SQUARE then true |
elif distanceSquarePointToSegment(P3; P1; Q) <= EPS_SQUARE then true |
||
else false |
else false |
||
end;</ |
end;</syntaxhighlight> |
||
'''Examples''' |
'''Examples''' |
||
< |
<syntaxhighlight lang="jq">def task1: |
||
def pts: [ [0, 0], [0, 1], [3, 1]]; |
def pts: [ [0, 0], [0, 1], [3, 1]]; |
||
"Triangle is \(.)", |
"Triangle is \(.)", |
||
Line 1,370: | Line 1,370: | ||
([ [3/2, 12/5], [51/10, -31/10], [-19/5, 1.2] ] | task1), "", |
([ [3/2, 12/5], [51/10, -31/10], [-19/5, 1.2] ] | task1), "", |
||
([ [1/10, 1/9], [100/8, 100/3], [100/4, 100/9] ] | task2), "", |
([ [1/10, 1/9], [100/8, 100/3], [100/4, 100/9] ] | task2), "", |
||
([ [1/10, 1/9], [100/8, 100/3], [-100/8, 100/6] ] | task2)</ |
([ [1/10, 1/9], [100/8, 100/3], [-100/8, 100/6] ] | task2)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,388: | Line 1,388: | ||
{{trans|Python}} |
{{trans|Python}} |
||
Using the Wren examples. |
Using the Wren examples. |
||
< |
<syntaxhighlight lang="julia">Point(x, y) = [x, y] |
||
Triangle(a, b, c) = [a, b, c] |
Triangle(a, b, c) = [a, b, c] |
||
LEzero(x) = x < 0 || isapprox(x, 0, atol=0.00000001) |
LEzero(x) = x < 0 || isapprox(x, 0, atol=0.00000001) |
||
Line 1,434: | Line 1,434: | ||
end |
end |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Using triangle [[1.5, 2.4], [0.51, -0.31], [-0.38, 1.2]]: |
Using triangle [[1.5, 2.4], [0.51, -0.31], [-0.38, 1.2]]: |
||
Line 1,457: | Line 1,457: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">import kotlin.math.max |
||
import kotlin.math.min |
import kotlin.math.min |
||
Line 1,552: | Line 1,552: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Triangle[(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)] |
<pre>Triangle[(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)] |
||
Line 1,569: | Line 1,569: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="lua">EPS = 0.001 |
||
EPS_SQUARE = EPS * EPS |
EPS_SQUARE = EPS * EPS |
||
Line 1,653: | Line 1,653: | ||
test(0.1, 0.1111111111111111, 12.5, 33.333333333333336, -12.5, 16.666666666666668, 5.414285714285714, 14.349206349206348) |
test(0.1, 0.1111111111111111, 12.5, 33.333333333333336, -12.5, 16.666666666666668, 5.414285714285714, 14.349206349206348) |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)] |
<pre>Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)] |
||
Line 1,669: | Line 1,669: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">RegionMember[Polygon[{{1, 2}, {3, 1}, {2, 4}}], {2, 2}]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>True</pre> |
<pre>True</pre> |
||
Line 1,675: | Line 1,675: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
const |
const |
||
Line 1,760: | Line 1,760: | ||
p3 = (-100 / 8, 100 / 6) |
p3 = (-100 / 8, 100 / 6) |
||
tri = initTriangle(p1, p2, p3) |
tri = initTriangle(p1, p2, p3) |
||
test(tri, pt)</ |
test(tri, pt)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,778: | Line 1,778: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Translate the Java program at [https://totologic.blogspot.com/2014/01/accurate-point-in-triangle-test.html this blog post] and data set is taken from the [[Find_if_a_point_is_within_a_triangle#Raku|Raku]] entry. |
Translate the Java program at [https://totologic.blogspot.com/2014/01/accurate-point-in-triangle-test.html this blog post] and data set is taken from the [[Find_if_a_point_is_within_a_triangle#Raku|Raku]] entry. |
||
< |
<syntaxhighlight lang="perl"># 20201123 added Perl programming solution |
||
use strict; |
use strict; |
||
Line 1,840: | Line 1,840: | ||
while ( my @vertex = $iter->()) { print '(',join(',',@vertex),') ' } |
while ( my @vertex = $iter->()) { print '(',join(',',@vertex),') ' } |
||
print ': ',naivePointInTriangle (@DATA, @$point) ? 'True' : 'False', "\n" ; |
print ': ',naivePointInTriangle (@DATA, @$point) ? 'True' : 'False', "\n" ; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Point (0,0) is within triangle (1.5,2.4) (5.1,-3.1) (-3.8,0.5) : True |
<pre>Point (0,0) is within triangle (1.5,2.4) (5.1,-3.1) (-3.8,0.5) : True |
||
Line 1,850: | Line 1,850: | ||
=== using convex_hull === |
=== using convex_hull === |
||
Using convex_hull() from [[Convex_hull#Phix]] |
Using convex_hull() from [[Convex_hull#Phix]] |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">p0</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">p0</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> |
||
Line 1,862: | Line 1,862: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Point %v is with triangle %v?:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">inside</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Point %v is with triangle %v?:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">inside</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">)})</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Point %v is with triangle %v?:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">inside</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Point %v is with triangle %v?:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">inside</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">)})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,872: | Line 1,872: | ||
===trans python=== |
===trans python=== |
||
(same output) |
(same output) |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">p0</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">p0</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> |
||
Line 1,907: | Line 1,907: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"point %v is with triangle %v?:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">iswithin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"point %v is with triangle %v?:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">iswithin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">)})</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"point %v is with triangle %v?:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">iswithin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"point %v is with triangle %v?:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">iswithin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">triangle</span><span style="color: #0000FF;">)})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python"> |
||
""" find if point is in a triangle """ |
""" find if point is in a triangle """ |
||
Line 1,942: | Line 1,942: | ||
isornot = "is" if iswithin(pnt, a, b, c) else "is not" |
isornot = "is" if iswithin(pnt, a, b, c) else "is not" |
||
print("Point", pnt, isornot, "within the triangle", TRI) |
print("Point", pnt, isornot, "within the triangle", TRI) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Point Point2D(0, 0) is within the triangle Triangle(Point2D(3/2, 12/5), Point2D(51/10, -31/10), Point2D(-19/5, 1/2)) |
Point Point2D(0, 0) is within the triangle Triangle(Point2D(3/2, 12/5), Point2D(51/10, -31/10), Point2D(-19/5, 1/2)) |
||
Line 1,956: | Line 1,956: | ||
I would probably use the dot-product version, if only because it requires less (no) division. |
I would probably use the dot-product version, if only because it requires less (no) division. |
||
< |
<syntaxhighlight lang="racket">#lang racket/base |
||
(define-syntax-rule (all-between-0..1? x ...) |
(define-syntax-rule (all-between-0..1? x ...) |
||
Line 2,003: | Line 2,003: | ||
(run-tests point-in-triangle?/barycentric) |
(run-tests point-in-triangle?/barycentric) |
||
(run-tests point-in-triangle?/parametric) |
(run-tests point-in-triangle?/parametric) |
||
(run-tests point-in-triangle?/dot-product))</ |
(run-tests point-in-triangle?/dot-product))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,013: | Line 2,013: | ||
Reusing code from the [[Convex_hull#Raku|Convex hull]] task and some logic from the [[Determine_if_two_triangles_overlap#Raku|Determine if two triangles overlap]] task. |
Reusing code from the [[Convex_hull#Raku|Convex hull]] task and some logic from the [[Determine_if_two_triangles_overlap#Raku|Determine if two triangles overlap]] task. |
||
<lang |
<syntaxhighlight lang="raku" line>class Point { |
||
has Real $.x is rw; |
has Real $.x is rw; |
||
has Real $.y is rw; |
has Real $.y is rw; |
||
Line 2,040: | Line 2,040: | ||
say "Point {$point.gist} is within triangle {join ', ', @triangle».gist}: ", |
say "Point {$point.gist} is within triangle {join ', ', @triangle».gist}: ", |
||
$point.&is-within: @triangle |
$point.&is-within: @triangle |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Point (0, 0) is within triangle (1.5, 2.4), (5.1, -3.1), (-3.8, 0.5): True |
<pre>Point (0, 0) is within triangle (1.5, 2.4), (5.1, -3.1), (-3.8, 0.5): True |
||
Line 2,050: | Line 2,050: | ||
<br>Extra certification code was added to verify that the '''X,Y''' coördinates for the points are not missing and are numeric. |
<br>Extra certification code was added to verify that the '''X,Y''' coördinates for the points are not missing and are numeric. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program determines if a specified point is within a specified triangle. */ |
||
parse arg p a b c . /*obtain optional arguments from the CL*/ |
parse arg p a b c . /*obtain optional arguments from the CL*/ |
||
if p=='' | p=="," then p= '(0,0)' /*Not specified? Then use the default.*/ |
if p=='' | p=="," then p= '(0,0)' /*Not specified? Then use the default.*/ |
||
Line 2,066: | Line 2,066: | ||
y: procedure; parse arg ',' y ")"; return cert(y,"Y") /* " " Y " */ |
y: procedure; parse arg ',' y ")"; return cert(y,"Y") /* " " Y " */ |
||
$: parse arg aa,bb,cc; return (x(aa)-x(cc)) *(y(bb)-y(cc)) - (x(bb)-x(cc)) *(y(aa)-y(cc)) |
$: parse arg aa,bb,cc; return (x(aa)-x(cc)) *(y(bb)-y(cc)) - (x(bb)-x(cc)) *(y(aa)-y(cc)) |
||
?: #1=$(p,a,b); #2=$(p,b,c); #3=$(p,c,a); return (#1>=0>=0>=0) | (#1<=0<=0<=0)</ |
?: #1=$(p,a,b); #2=$(p,b,c); #3=$(p,c,a); return (#1>=0>=0>=0) | (#1<=0<=0<=0)</syntaxhighlight> |
||
{{out|output|text= when using the default triangle and the point at: <tt> (0,0) </tt>}} |
{{out|output|text= when using the default triangle and the point at: <tt> (0,0) </tt>}} |
||
<pre> |
<pre> |
||
Line 2,082: | Line 2,082: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ruby">EPS = 0.001 |
||
EPS_SQUARE = EPS * EPS |
EPS_SQUARE = EPS * EPS |
||
Line 2,169: | Line 2,169: | ||
end |
end |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Triangle is [[1.5, 2.4], [5.1, -3.1], [-3.8, 1.2]] |
<pre>Triangle is [[1.5, 2.4], [5.1, -3.1], [-3.8, 1.2]] |
||
Line 2,184: | Line 2,184: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">import math |
||
const eps = 0.001 |
const eps = 0.001 |
||
Line 2,270: | Line 2,270: | ||
within = accurate_point_in_triangle(x1, y1, x2, y2, x3, y3, x, y) |
within = accurate_point_in_triangle(x1, y1, x2, y2, x3, y3, x, y) |
||
println("Point $pt is within triangle ? $within") |
println("Point $pt is within triangle ? $within") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,288: | Line 2,288: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
This is a translation of the ActionScript code for the 'accurate' method in the first referenced article above. |
This is a translation of the ActionScript code for the 'accurate' method in the first referenced article above. |
||
< |
<syntaxhighlight lang="ecmascript">var EPS = 0.001 |
||
var EPS_SQUARE = EPS * EPS |
var EPS_SQUARE = EPS * EPS |
||
Line 2,368: | Line 2,368: | ||
y3 = tri[2][1] |
y3 = tri[2][1] |
||
within = accuratePointInTriangle.call(x1, y1, x2, y2, x3, y3, x, y) |
within = accuratePointInTriangle.call(x1, y1, x2, y2, x3, y3, x, y) |
||
System.print("Point %(pt) is within triangle ? %(within)")</ |
System.print("Point %(pt) is within triangle ? %(within)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,385: | Line 2,385: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func real Dot(W,X,Y,Z); \Return the dot product of two 2D vectors |
||
real W,X,Y,Z; \ (W-X) dot (Y-Z) |
real W,X,Y,Z; \ (W-X) dot (Y-Z) |
||
real WX(2), YZ(2); |
real WX(2), YZ(2); |
||
Line 2,408: | Line 2,408: | ||
Text(0, if PointInTri([-5.0,-2.2]) then "inside" else "outside"); CrLf(0); |
Text(0, if PointInTri([-5.0,-2.2]) then "inside" else "outside"); CrLf(0); |
||
Text(0, if PointInTri([10.5, 6.3]) then "inside" else "outside"); CrLf(0); |
Text(0, if PointInTri([10.5, 6.3]) then "inside" else "outside"); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |