Skip to content

Commit 3d06d75

Browse files
committed
AA on both sides
1 parent 33f37f3 commit 3d06d75

File tree

1 file changed

+79
-124
lines changed

1 file changed

+79
-124
lines changed

debug-tools/examples/line-perp.rs

Lines changed: 79 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,17 @@ fn perpendicular(
149149
(Rgb888::CSS_CORNFLOWER_BLUE, Rgb888::YELLOW)
150150
};
151151

152-
// let (c1, c2) = (Rgb888::GREEN, Rgb888::GREEN);
153-
154-
let mut distance = 0.0f32;
152+
let (c_left, c_right) = (Rgb888::GREEN, Rgb888::GREEN);
155153

156154
let origin = Point::new(x0, y0);
157155

158156
// dbg!(orig_width_l);
159157

160158
let limit_l = orig_width_l * 2.0;
159+
let limit_r = orig_width_r * 2.0;
160+
161+
let mut distance = 0.0f32;
161162

162-
// while tk.pow(2) <= width_l + tk.pow(2) && width_l > 0 {
163163
while distance.floor() <= limit_l && width_l > 0 {
164164
// println!("---");
165165

@@ -208,32 +208,78 @@ fn perpendicular(
208208

209209
// println!("\n===========================\n");
210210

211+
// let mut point = Point::new(x0, y0);
212+
// let mut error = -einit;
213+
// let mut tk = winit;
214+
// let mut p = 0;
215+
216+
// while tk.pow(2) <= width_r + tk.pow(2) && width_r > 0 {
217+
// if p > 0 {
218+
// let thing = (tk.pow(2) - width_l) as f32 / width_l as f32;
219+
// let fract = if tk.pow(2) > width_l {
220+
// 1.0 - thing
221+
// } else {
222+
// 1.0
223+
// };
224+
225+
// Pixel(
226+
// point,
227+
// Rgb565::new(
228+
// (c1.r() as f32 * fract) as u8,
229+
// (c1.g() as f32 * fract) as u8,
230+
// (c1.b() as f32 * fract) as u8,
231+
// ),
232+
// )
233+
// .draw(display)?;
234+
// }
235+
236+
// if error > threshold {
237+
// point -= step.major;
238+
// error += e_minor;
239+
// tk += 2 * dy;
240+
// }
241+
242+
// error += e_major;
243+
// point -= step.minor;
244+
// tk += 2 * dx;
245+
// p += 1;
246+
// }
247+
248+
// // ---
249+
211250
let mut point = Point::new(x0, y0);
212251
let mut error = -einit;
213252
let mut tk = winit;
214253
let mut p = 0;
215254

216-
while tk.pow(2) <= width_r && width_r > 0 {
217-
if p > 0 {
218-
let thing = (tk.pow(2) - width_l) as f32 / width_l as f32;
219-
let fract = if tk.pow(2) > width_l {
220-
1.0 - thing
221-
} else {
222-
1.0
223-
};
224-
225-
let fract = 1.0;
226-
227-
Pixel(
228-
point,
229-
Rgb888::new(
230-
(c_right.r() as f32 * fract) as u8,
231-
(c_right.g() as f32 * fract) as u8,
232-
(c_right.b() as f32 * fract) as u8,
233-
),
234-
)
235-
.draw(display)?;
236-
}
255+
let mut distance = 0.0f32;
256+
257+
while distance.floor() <= limit_r && width_r > 0 {
258+
// println!("---");
259+
260+
let is_outside = {
261+
let le1 = LinearEquation::from_line(&right_extent);
262+
263+
le1.check_side(point, side_check_right)
264+
};
265+
266+
// let fract = 1.0 - dbg!(dist(left_extent, point));
267+
268+
let fract = if !is_outside {
269+
1.0
270+
} else {
271+
1.0 - dist(right_extent, point)
272+
};
273+
274+
Pixel(
275+
point,
276+
Rgb888::new(
277+
(c_right.r() as f32 * fract) as u8,
278+
(c_right.g() as f32 * fract) as u8,
279+
(c_right.b() as f32 * fract) as u8,
280+
),
281+
)
282+
.draw(display)?;
237283

238284
if error > threshold {
239285
point -= step.major;
@@ -244,7 +290,14 @@ fn perpendicular(
244290
error += e_major;
245291
point -= step.minor;
246292
tk += 2 * dx;
247-
p += 1;
293+
294+
// dbg!(distance);
295+
296+
distance = {
297+
let delta = point - origin;
298+
299+
f32::sqrt((delta.x.pow(2) + delta.y.pow(2)) as f32)
300+
};
248301
}
249302

250303
Ok(())
@@ -353,104 +406,6 @@ fn thick_line(
353406
Ok(())
354407
}
355408

356-
// /// Left/right extents perpendicular to a given point. Returns `(L, R)`.
357-
// fn perp_extents(
358-
// start: Point,
359-
// delta: MajorMinor<i32>,
360-
// mut step: MajorMinor<Point>,
361-
// width: i32,
362-
// ) -> (Point, Point) {
363-
// let mut point = start;
364-
365-
// if width < 2 {
366-
// return (start, start);
367-
// }
368-
369-
// let dx = delta.major;
370-
// let dy = delta.minor;
371-
372-
// let mut sign = match (step.major, step.minor) {
373-
// (Point { x: -1, y: 0 }, Point { x: 0, y: 1 }) => -1,
374-
// (Point { x: 0, y: -1 }, Point { x: -1, y: 0 }) => -1,
375-
// (Point { x: 1, y: 0 }, Point { x: 0, y: -1 }) => -1,
376-
// (Point { x: 0, y: 1 }, Point { x: 1, y: 0 }) => -1,
377-
// _ => 1,
378-
// };
379-
380-
// if sign == -1 {
381-
// step.major *= -1;
382-
// step.minor *= -1;
383-
// }
384-
385-
// let dx = dx.abs();
386-
// let dy = dy.abs();
387-
388-
// let threshold = dx - 2 * dy;
389-
// let e_minor = -2 * dx;
390-
// let e_major = 2 * dy;
391-
392-
// let mut error = 0;
393-
// let mut tk = 0i32;
394-
395-
// let side = LineSide::Center;
396-
397-
// let (mut width_l, mut width_r) = side.widths(width);
398-
399-
// if sign == -1 {
400-
// core::mem::swap(&mut width_l, &mut width_r);
401-
// }
402-
403-
// if side == LineSide::Right {
404-
// core::mem::swap(&mut width_l, &mut width_r);
405-
// }
406-
407-
// let orig_width_l = width_l as f32;
408-
// let orig_width_r = width_r as f32;
409-
410-
// let width_l = width_l.pow(2) * (dx * dx + dy * dy);
411-
// let width_r = width_r.pow(2) * (dx * dx + dy * dy);
412-
413-
// while tk.pow(2) <= width_l && width_l > 0 {
414-
// if error >= threshold {
415-
// point += step.major;
416-
// error += e_minor;
417-
// tk += 2 * dy;
418-
// }
419-
420-
// error += e_major;
421-
// point += step.minor;
422-
// tk += 2 * dx;
423-
// }
424-
425-
// let point_l = point;
426-
427-
// let mut point = start;
428-
// let mut error = 0;
429-
// let mut tk = 0i32;
430-
// let mut p = 0;
431-
432-
// while tk.pow(2) <= width_r && width_r > 0 {
433-
// if error > threshold {
434-
// point -= step.major;
435-
// error += e_minor;
436-
// tk += 2 * dy;
437-
// }
438-
439-
// error += e_major;
440-
// point -= step.minor;
441-
// tk += 2 * dx;
442-
// p += 1;
443-
// }
444-
445-
// (point_l, point)
446-
// }
447-
448-
// fn extents(start: Point, end: Point, width: i32, step: MajorMinor<Point>) -> (Line, Line) {
449-
// let delta = end - start;
450-
451-
// let (start_l, start_r) = perp_extents(start, delta, step, width);
452-
// }
453-
454409
struct LineDebug {
455410
start: Point,
456411
end: Point,

0 commit comments

Comments
 (0)