Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 27, 2019 00:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/d29298ef83c1e075d7a6860bfdfc2f05 to your computer and use it in GitHub Desktop.
Save rust-play/d29298ef83c1e075d7a6860bfdfc2f05 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// Filled circle algorithm with edge softening
fn main() {
let w = 12;
let r: f32 = w as f32 / 2.0;
println!("=================================================");
println!("radius: {}", r);
println!("=================================================");
println!("col,row -> dx,dy -> dist from center -> diff -> alpha");
println!("=================================================");
for row in 0..w {
for col in 0..w {
let dx = (col as f32 + 0.5 - r).abs();
let dy = (row as f32 + 0.5 - r).abs();
let dist = ((dx*dx + dy*dy) as f32).sqrt();
let diff = dist - r;
let alpha: u8;
if diff > 1.0 {
alpha = 0;
}
else if diff > 0.0 && diff < 1.0 {
alpha = ((1.0 - diff) * 255.0) as u8;
}
else {
alpha = 255;
}
println!("{},{} -> {},{} -> {} -> {} -> {}", col, row, dx, dy, dist, diff, alpha);
}
println!("=================================================");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment