Created
April 27, 2019 00:26
-
-
Save rust-play/d29298ef83c1e075d7a6860bfdfc2f05 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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