Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 10, 2026 15:56
Show Gist options
  • Select an option

  • Save rust-play/55fcef9fc4c2c5307fcc7ed7489238bf to your computer and use it in GitHub Desktop.

Select an option

Save rust-play/55fcef9fc4c2c5307fcc7ed7489238bf to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use image::{ImageBuffer, Rgb};
use std::f64::consts::PI;
fn main() {
let grid_size = 11;
let cell_size = 200;
let padding = 20;
let width = grid_size * cell_size;
let height = grid_size * cell_size;
let mut img = ImageBuffer::new(width as u32, height as u32);
// Background color (off-white)
for pixel in img.pixels_mut() {
*pixel = Rgb([245, 240, 230]);
}
for n in 1..=grid_size {
for m in 1..=grid_size {
let center_x = ((m - 1) * cell_size + cell_size / 2) as f64;
let center_y = ((n - 1) * cell_size + cell_size / 2) as f64;
let radius = (cell_size / 2 - padding) as f64;
// Simple color variation based on n and m
let r = (n * 20) as u8 % 255;
let g = (m * 20) as u8 % 255;
let b = ((n + m) * 10) as u8 % 255;
let color = Rgb([r, g, b]);
// To complete the curve, we need to iterate theta
// n/m determines the symmetry. 2*PI*m is usually sufficient.
let max_theta = 2.0 * PI * m as f64;
let step = 0.01;
let mut theta = 0.0;
while theta <= max_theta {
let r_val = (n as f64 / m as f64 * theta).cos();
// Polar to Cartesian conversion
let x = center_x + radius * r_val * theta.cos();
let y = center_y + radius * r_val * theta.sin();
if x >= 0.0 && x < width as f64 && y >= 0.0 && y < height as f64 {
img.put_pixel(x as u32, y as u32, color);
}
theta += step;
}
}
}
img.save("rose_curves.png").expect("Failed to save image");
println!("Image saved as rose_curves.png");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment