1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/// Represents a line segment
#[derive(Debug, Clone, Copy)]
pub struct Line {
pub start: (f64, f64),
pub end: (f64, f64),
}
impl Line {
pub fn new(start: (f64, f64), end: (f64, f64)) -> Self {
Self { start, end }
}
}
/// Represents a rectangle
#[derive(Debug, Clone, Copy)]
pub struct Rect {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
impl Rect {
pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
Self {
x,
y,
width,
height,
}
}
}
/// Checks if a point is inside a rectangle
pub fn point_in_rect(point: (f64, f64), rect: &Rect) -> bool {
point.0 >= rect.x
&& point.0 <= rect.x + rect.width
&& point.1 >= rect.y
&& point.1 <= rect.y + rect.height
}
/// Checks if two rectangles intersect (AABB collision)
pub fn rect_intersects_rect(r1: &Rect, r2: &Rect) -> bool {
!(r1.x + r1.width < r2.x
|| r2.x + r2.width < r1.x
|| r1.y + r1.height < r2.y
|| r2.y + r2.height < r1.y)
}
/// Checks if two line segments intersect using parametric equations
pub fn line_intersects_line(l1: &Line, l2: &Line) -> bool {
let x1 = l1.start.0;
let y1 = l1.start.1;
let x2 = l1.end.0;
let y2 = l1.end.1;
let x3 = l2.start.0;
let y3 = l2.start.1;
let x4 = l2.end.0;
let y4 = l2.end.1;
let denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if denom.abs() < 1e-10 {
return false; // Parallel or coincident
}
let t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom;
let u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom;
(0.0..=1.0).contains(&t) && (0.0..=1.0).contains(&u)
}
/// Checks if a line segment intersects a rectangle
pub fn line_intersects_rect(line: &Line, rect: &Rect) -> bool {
if point_in_rect(line.start, rect) || point_in_rect(line.end, rect) {
return true;
}
let edges = [
Line::new((rect.x, rect.y), (rect.x + rect.width, rect.y)),
Line::new(
(rect.x + rect.width, rect.y),
(rect.x + rect.width, rect.y + rect.height),
),
Line::new(
(rect.x + rect.width, rect.y + rect.height),
(rect.x, rect.y + rect.height),
),
Line::new((rect.x, rect.y + rect.height), (rect.x, rect.y)),
];
edges.iter().any(|edge| line_intersects_line(line, edge))
}
/// Spatial hash grid for fast collision detection
/// that stores cells as indices of lines.
pub struct SpatialGrid {
cell_size: f64,
cells: Vec<Vec<usize>>,
columns: usize,
rows: usize,
}
impl SpatialGrid {
/// Creates a new spatial grid for the given dimensions
pub fn new(width: f64, height: f64, cell_size: f64) -> Self {
// Safe cast: width and height are positive map dimensions, divided by cell_size and clamped to at least 1.0
// The resulting grid size will be reasonable (< millions of cells) for typical map sizes
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let columns = (width / cell_size).ceil().max(1.0) as usize;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let rows = (height / cell_size).ceil().max(1.0) as usize;
let cells = vec![Vec::new(); columns * rows];
Self {
cell_size,
cells,
columns,
rows,
}
}
/// Inserts a line into the spatial grid
pub fn insert_line(&mut self, line_idx: usize, line: &Line) {
let min_x = line.start.0.min(line.end.0);
let max_x = line.start.0.max(line.end.0);
let min_y = line.start.1.min(line.end.1);
let max_y = line.start.1.max(line.end.1);
// Safe cast: coordinates are clamped to [0.0, max] before casting, ensuring valid grid indices
// The .max(0.0) ensures non-negative, and .min(columns/rows - 1) ensures within bounds
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let start_column =
((min_x / self.cell_size).floor().max(0.0) as usize).min(self.columns - 1);
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let end_column = ((max_x / self.cell_size).floor().max(0.0) as usize).min(self.columns - 1);
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let start_row = ((min_y / self.cell_size).floor().max(0.0) as usize).min(self.rows - 1);
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let end_row = ((max_y / self.cell_size).floor().max(0.0) as usize).min(self.rows - 1);
for row in start_row..=end_row {
for col in start_column..=end_column {
let cell_idx = row * self.columns + col;
self.cells[cell_idx].push(line_idx);
}
}
}
/// Queries the spatial grid for lines that might intersect with the given rectangle
/// Returns line indices without duplicates
pub fn query_rect(&self, rect: &Rect) -> impl Iterator<Item = usize> + '_ {
let min_x = rect.x;
let max_x = rect.x + rect.width;
let min_y = rect.y;
let max_y = rect.y + rect.height;
let max_column_idx = if self.columns > 0 {
self.columns - 1
} else {
0
};
let max_row_idx = if self.rows > 0 { self.rows - 1 } else { 0 };
// Safe cast: grid indices are small (< thousands) for typical map sizes, well within f64 precision
// The coordinates are clamped to valid grid bounds before casting to usize
#[allow(clippy::cast_precision_loss)]
let max_column_f64 = max_column_idx as f64;
#[allow(clippy::cast_precision_loss)]
let max_row_f64 = max_row_idx as f64;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let start_column = ((min_x / self.cell_size)
.floor()
.max(0.0)
.min(max_column_f64)) as usize;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let end_column = ((max_x / self.cell_size)
.floor()
.max(0.0)
.min(max_column_f64)) as usize;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let start_row = ((min_y / self.cell_size).floor().max(0.0).min(max_row_f64)) as usize;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let end_row = ((max_y / self.cell_size).floor().max(0.0).min(max_row_f64)) as usize;
let mut line_indices = Vec::new();
for row in start_row..=end_row {
for column in start_column..=end_column {
let cell_idx = row * self.columns + column;
line_indices.extend_from_slice(&self.cells[cell_idx]);
}
}
line_indices.sort_unstable();
line_indices.dedup();
line_indices.into_iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_point_in_rect() {
let rect = Rect::new(10.0, 10.0, 20.0, 20.0);
assert!(point_in_rect((15.0, 15.0), &rect));
assert!(point_in_rect((10.0, 10.0), &rect));
assert!(point_in_rect((30.0, 30.0), &rect));
assert!(!point_in_rect((5.0, 15.0), &rect));
assert!(!point_in_rect((35.0, 15.0), &rect));
}
#[test]
fn test_rect_intersects_rect() {
let r1 = Rect::new(0.0, 0.0, 10.0, 10.0);
let r2 = Rect::new(5.0, 5.0, 10.0, 10.0);
let r3 = Rect::new(20.0, 20.0, 10.0, 10.0);
assert!(rect_intersects_rect(&r1, &r2));
assert!(rect_intersects_rect(&r2, &r1));
assert!(!rect_intersects_rect(&r1, &r3));
}
#[test]
fn test_line_intersects_line() {
let l1 = Line::new((0.0, 0.0), (10.0, 10.0));
let l2 = Line::new((0.0, 10.0), (10.0, 0.0));
let l3 = Line::new((20.0, 20.0), (30.0, 30.0));
assert!(line_intersects_line(&l1, &l2));
assert!(!line_intersects_line(&l1, &l3));
}
#[test]
fn test_line_intersects_rect() {
let rect = Rect::new(10.0, 10.0, 20.0, 20.0);
let l1 = Line::new((0.0, 15.0), (40.0, 15.0)); // Horizontal through
let l2 = Line::new((15.0, 0.0), (15.0, 40.0)); // Vertical through
let l3 = Line::new((0.0, 0.0), (5.0, 5.0)); // Outside
assert!(line_intersects_rect(&l1, &rect));
assert!(line_intersects_rect(&l2, &rect));
assert!(!line_intersects_rect(&l3, &rect));
}
}
|