Ray-casting algorithm: Difference between revisions

m (→‎{{header|REXX}}: added whitespace, changed an indentation for alignment.)
Line 3,180:
{{trans|Python}}
<lang rust>use std::f64;
use std::clone::Clone;
 
const _EPS: f64 = 0.00001;
Line 3,186 ⟶ 3,185:
const _MAX: f64 = f64::MAX;
 
#[derive(Debug, Clone)]
struct Point {
x: f64,
y: f64,
}
 
#[derive(Debug, Clone)]
struct Edge {
pt1: Point,
pt2: Point,
}
 
impl Edge {
fn new(pt1: (f64, f64), pt2: (f64, f64)) -> Edge {
returnEdge true;{
Edge { pt1: Point { x: pt1.0, y: pt1.1 }, pt2: Point {x: pt2.0, y: pt2.1} }
pt2: Point { x: pt2.0, y: pt2.1 },
}
}
}
 
struct Polygon {
#[derive(Debug)]
edges: Vec<Edge>, // Polygon has to be created with counter-clockwise coordinates
struct Polygon{
edges: Vec<Edge> // Polygon has to be created with counter-clockwise coordinates
}
 
fn pt_in_polygon(pt: &Point, poly: &Polygon) -> bool {
let count = poly.edges.iter()
.mapiter(|edge| ray_intersect_seg(pt, edge))
.foldfilter(0u8, |sum, valedge| sum + ray_intersect_seg(valpt, as u8edge)) % 2 == 1
.count();
 
count % 2 == 1
}
 
Line 3,219 ⟶ 3,223:
let (mut a, mut b): (&Point, &Point) = (&edge.pt1, &edge.pt2);
if a.y > b.y {
let t =std::mem::swap(&mut a;, a =&mut b; b = t);
}
if pt.y == a.y || pt.y == b.y {
pt = Point {x: pt.x, y: pt.y += _EPS};
}
 
if (pt.y > b.y || pt.y < a.y) || pt.x > a.x.max(b.x) {
return false;
} else if pt.x < a.x.min(b.x) {
}
if pt.x < a.x.min(b.x) {true
return true;
} else {
let m_red = if (a.x - b.x).abs() > _MIN { (b.y - a.y) / (b.x - a.x) } else { _MAX };
let m_blue = if (a.x - pt.x).abs() > _MIN { (ptb.y - a.y) / (ptb.x - a.x) } else { _MAX };
return} m_blueelse >= m_red;{
_MAX
};
let m_blue = if (a.x - pt.x).abs() > _MIN {
(pt.y - a.y) / (pt.x - a.x)
} else {
_MAX
};
m_blue >= m_red
}
}
 
fn main() {
let p = |x, y| Point {x: x, y: y};
let testpoints = vec![p(5.0, 5.0), p(5.0, 8.0), p(-10.0, 5.0), p(0.0, 5.0), p(10.0, 5.0), p(8.0, 5.0), p(10.0, 10.0)];
let poly_square = Polygon {
edges: vec![
Line 3,244 ⟶ 3,256:
Edge::new((10.0, 0.0), (10.0, 10.0)),
Edge::new((10.0, 10.0), (0.0, 10.0)),
Edge::new((0.0, 10.0), (0.0, 0.0)),
],
};
let poly_square_hole = Polygon {
edges: vec![
Line 3,256 ⟶ 3,268:
Edge::new((7.5, 2.5), (7.5, 7.5)),
Edge::new((7.5, 7.5), (2.5, 7.5)),
Edge::new((2.5, 7.5), (2.5, 2.5)),
],
};
let poly_strange = Polygon {
Line 3,267 ⟶ 3,279:
Edge::new((7.5, 7.5), (10.0, 10.0)),
Edge::new((10.0, 10.0), (10.0, 0.0)),
Edge::new((10.0, 0.0), (2.5, 2.5)),
],
};
let poly_hexagon = Polygon {
Line 3,277 ⟶ 3,289:
Edge::new((7.0, 10.0), (3.0, 10.0)),
Edge::new((3.0, 10.0), (0.0, 5.0)),
Edge::new((0.0, 5.0), (3.0, 0.0)),
],
};
print!("\nSquare :");
for pt in &testpoints.iter() {
print!(" {:?}", pt_in_polygon(pt, &poly_square));
}
print!("\nSquare with hole:");
for pt in &testpoints.iter() {
print!(" {:?}", pt_in_polygon(pt, &poly_square_hole));
}
print!("\nStrange polygon :");
for pt in &testpoints.iter() {
print!(" {:?}", pt_in_polygon(pt, &poly_strange));
}
print!("\nHexagon :");
for pt in &testpoints.iter() {
print!(" {:?}", pt_in_polygon(pt, &poly_hexagon));
}
printprintln!("\n");
}</lang>
{{out}}