Jump to content

Find if a point is within a triangle: Difference between revisions

Line 801:
</pre>
 
=={{header|DRust}}==
{{trans|C++D}}
<syntaxhighlight lang="d">import std.algorithm; //.comparison for min and max
import std.stdio;
 
}</syntaxhighlight lang="rust">
immutableconst EPS: f64 = 0.001;
immutableconst EPS_SQUARE: f64 = EPS * EPS;
 
doublefn side(double x1: f64, double y1,: doublef64, x2: f64, double y2: f64, double x,: doublef64, y: f64) -> f64 {
return (y2 - y1) * (x - x1) + (-x2 + x1) * (y - y1);
}
 
boolfn naivePointInTrianglenaive_point_in_triangle(double x1,: doublef64, y1,: doublef64, x2,: doublef64, y2: f64, double x3: f64, double y3: f64, double x: f64, double y: f64) -> bool {
doublelet checkSide1check_side1 = side(x1, y1, x2, y2, x, y) >= 0.0;
doublelet checkSide2check_side2 = side(x2, y2, x3, y3, x, y) >= 0.0;
doublelet checkSide3check_side3 = side(x3, y3, x1, y1, x, y) >= 0.0;
return checkSide1check_side1 && checkSide2check_side2 && checkSide3;check_side3
}
 
boolfn pointInTriangleBoundingBoxpoint_in_triangle_bounding_box(double x1,: doublef64, y1,: doublef64, x2,: doublef64, y2: f64, double x3: f64, double y3: f64, double x: f64, double y: f64) -> bool {
doublelet xMinx_min = f64::min(x1, f64::min(x2, x3)) - EPS;
doublelet xMaxx_max = f64::max(x1, f64::max(x2, x3)) + EPS;
doublelet yMiny_min = f64::min(y1, f64::min(y2, y3)) - EPS;
doublelet yMaxy_max = f64::max(y1, f64::max(y2, y3)) + EPS;
return !(x < xMinx_min || xMaxx_max < x || y < yMiny_min || yMaxy_max < y);
}
 
fn distance_square_point_to_segment(x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64) -> f64 {
double distanceSquarePointToSegment(double x1, double y1, double x2, double y2, double x, double y) {
doublelet p1_p2_squareLengthp1_p2_square_length = (x2 - x1) * .powi(x2 - x12) + (y2 - y1) * .powi(y2 - y12);
doublelet dotProductdot_product = ((x - x1) * (x2 - x1) + (y - y1) * (y2 - y1)) / p1_p2_squareLengthp1_p2_square_length;
if (dotProductdot_product < 0).0 {
return (x - x1) * .powi(x - x12) + (y - y1) * .powi(y - y12);
} else if (dotProductdot_product <= 1).0 {
doublelet p_p1_squareLengthp_p1_square_length = (x1 - x) * .powi(x1 - x2) + (y1 - y) * .powi(y1 - y2);
p_p1_square_length - dot_product.powi(2) * p1_p2_square_length
return p_p1_squareLength - dotProduct * dotProduct * p1_p2_squareLength;
} else {
return (x - x2) * .powi(x - x22) + (y - y2) * .powi(y - y22);
}
}
 
boolfn accuratePointInTriangleaccurate_point_in_triangle(double x1,: doublef64, y1,: doublef64, x2,: doublef64, y2: f64, double x3: f64, double y3: f64, double x: f64, double y: f64) -> bool {
if (!pointInTriangleBoundingBoxpoint_in_triangle_bounding_box(x1, y1, x2, y2, x3, y3, x, y)) {
return false;
}
if (naivePointInTrianglenaive_point_in_triangle(x1, y1, x2, y2, x3, y3, x, y)) {
return true;
}
if (distanceSquarePointToSegmentdistance_square_point_to_segment(x1, y1, x2, y2, x, y) <= EPS_SQUARE) {
return true;
}
if (distanceSquarePointToSegmentdistance_square_point_to_segment(x2, y2, x3, y3, x, y) <= EPS_SQUARE) {
return true;
}
if (distanceSquarePointToSegmentdistance_square_point_to_segment(x3, y3, x1, y1, x, y) <= EPS_SQUARE) {
return true;
}
return false;
}
 
fn print_point(x: f64, y: f64) {
void printPoint(double x, double y) {
writeprint!('"(', x{}, {})", "x, y, ')');
}
 
voidfn printTriangleprint_triangle(double x1: f64, double y1,: doublef64, x2: f64, double y2,: doublef64, x3: f64, double y3: f64) {
writeprint!("Triangle is [");
printPointprint_point(x1, y1);
writeprint!(", ");
printPointprint_point(x2, y2);
writeprint!(", ");
printPointprint_point(x3, y3);
writelnprintln!('"]'");
}
 
voidfn test(double x1: f64, double y1: f64, double x2: f64, double y2: f64, double x3: f64, double y3: f64, double x: f64, double y: f64) {
printTriangleprint_triangle(x1, y1, x2, y2, x3, y3);
writeprint!("Point ");
printPointprint_point(x, y);
writeprint!(" is within triangle? ");
writelnprintln!(accuratePointInTriangle"{}", accurate_point_in_triangle(x1, y1, x2, y2, x3, y3, x, y));
}
 
voidfn main() {
test(1.5, 2.4, 5.1, -3.1, -3.8, 1.2, 0.0, 0.0);
println!();
test(1.5, 2.4, 5.1, -3.1, -3.8, 1.2, 0, 1);
test(1.5, 2.4, 5.1, -3.1, -3.8, 1.2, 3, 1);
test(1.5, 2.4, 5.1, -3.1, -3.8, 1.2, 0.0, 1.0);
writeln;
println!();
 
test(0.1, 0.1111111111111111, 12.5, 33.333333333333336, 25, 11.11111111111111, 5.414285714285714, 14.349206349206348);
test(1.5, 2.4, 5.1, -3.1, -3.8, 1.2, 3.0, 1.0);
writeln;
println!();
 
test(0.1, 0.1111111111111111, 12.5, 33.333333333333336, 25.0, 11.11111111111111, 5.414285714285714, 14.349206349206348);
println!();
test(0.1, 0.1111111111111111, 12.5, 33.333333333333336, -12.5, 16.666666666666668, 5.414285714285714, 14.349206349206348);
writelnprintln!();
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
<pre>
<pre>Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)]
Point (0, 0) is within triangle? true
 
Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)]
Point (0, 1) is within triangle? true
 
Triangle is [(1.5, 2.4), (5.1, -3.1), (-3.8, 1.2)]
Point (3, 1) is within triangle? false
 
Triangle is [(0.1, 0.1111111111111111111111), (12.5, 33.3333333333333333336), (25, 11.111111111111111111)]
Point (5.41429414285714285714, 14.3492349206349206348) is within triangle? true
 
Triangle is [(0.1, 0.111111), (12.5, 33.3333), (-12.5, 16.6667)]
Point (5.41429, 14.3492) is within triangle? true</pre>
 
 
Triangle is [(0.1, 0.1111111111111111111111), (12.5, 33.3333333333333333336), (-12.5, 16.6667666666666666668)]
Point (5.41429414285714285714, 14.3492349206349206348) is within triangle? true</pre>
</pre>
 
=={{header|Delphi}}==
121

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.