mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
hit_test for HtmlAreaElement polygons
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
This commit is contained in:
parent
cd36c35cf2
commit
4fd818d46d
1 changed files with 23 additions and 2 deletions
|
@ -46,6 +46,8 @@ pub enum Area {
|
||||||
bottom_right: (f32, f32),
|
bottom_right: (f32, f32),
|
||||||
},
|
},
|
||||||
Polygon {
|
Polygon {
|
||||||
|
/// Stored as a flat array of coordinates
|
||||||
|
/// e.g. [x1, y1, x2, y2, x3, y3] for a triangle
|
||||||
points: Vec<f32>,
|
points: Vec<f32>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -203,8 +205,27 @@ impl Area {
|
||||||
p.y >= top_left.1
|
p.y >= top_left.1
|
||||||
},
|
},
|
||||||
|
|
||||||
//TODO polygon hit_test
|
Area::Polygon { ref points } => {
|
||||||
_ => false,
|
// Ray-casting algorithm to determine if point is inside polygon
|
||||||
|
let mut inside = false;
|
||||||
|
|
||||||
|
debug_assert!(points.len() % 2 == 0);
|
||||||
|
let vertices = points.len() / 2;
|
||||||
|
|
||||||
|
for i in 0..vertices {
|
||||||
|
let next_i = if i + 1 == vertices { 0 } else { i + 1 };
|
||||||
|
|
||||||
|
let xi = points[2 * i];
|
||||||
|
let yi = points[2 * i + 1];
|
||||||
|
let xj = points[2 * next_i];
|
||||||
|
let yj = points[2 * next_i + 1];
|
||||||
|
|
||||||
|
if (yi > p.y) != (yj > p.y) && p.x < (xj - xi) * (p.y - yi) / (yj - yi) + xi {
|
||||||
|
inside = !inside;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inside
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue