Skip to content

Instantly share code, notes, and snippets.

@ivabus
Last active July 17, 2022 07:38
Show Gist options
  • Save ivabus/f22dc325c74fbc913cc32b92bd263751 to your computer and use it in GitHub Desktop.
Save ivabus/f22dc325c74fbc913cc32b92bd263751 to your computer and use it in GitHub Desktop.
Ford circles done bad
[package]
name = "untitled"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bracket-color = "0.8.2"
[dependencies.sdl2]
version = "0.35.2"
default-features = false
features = ["ttf", "gfx"]
extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::gfx::primitives::DrawRenderer;
use bracket_color;
use sdl2::log::Category::Test;
use sdl2::render::TextureQuery;
use sdl2::rect::Rect;
static SCREEN_WIDTH: u32 = 1200;
static SCREEN_HEIGHT: u32 = 650;
macro_rules! rect(
($x:expr, $y:expr, $w:expr, $h:expr) => (
Rect::new($x as i32, $y as i32, $w as u32, $h as u32)
)
);
fn get_centered_rect(rect_width: u32, rect_height: u32, cons_width: u32, cons_height: u32) -> Rect {
let wr = rect_width as f32 / cons_width as f32;
let hr = rect_height as f32 / cons_height as f32;
let (w, h) = if wr > 1f32 || hr > 1f32 {
if wr > hr {
println!("Scaling down! The text will look worse!");
let h = (rect_height as f32 / wr) as i32;
(cons_width as i32, h)
} else {
println!("Scaling down! The text will look worse!");
let w = (rect_width as f32 / hr) as i32;
(w, cons_height as i32)
}
} else {
(rect_width as i32, rect_height as i32)
};
let cx = (1200 as i32 - w) / 2;
let cy = (SCREEN_HEIGHT as i32 - h) / 2;
rect!(cx, cy, w, h)
}
struct Fraction {
p: i32,
q: i32
}
fn gen_tree(layers: i32) -> Vec<Fraction> {
let mut res: Vec<Fraction> = Vec::new();
res.push( Fraction { p: 0, q: 1});
res.push( Fraction { p: 1, q: 1});
for _ in 0..layers - 1 {
let mut new_res: Vec<Fraction> = Vec::new();
for i in 0..res.len() - 1 {
new_res.push(Fraction { p: res[i].p, q: res[i].q});
new_res.push(Fraction { p: res[i].p + res[i+1].p, q: res[i].q + res[i+1].q});
}
new_res.push(Fraction {p: res.last().unwrap().p, q: res.last().unwrap().q});
res = new_res;
}
for i in 0..res.len() {
res.push(Fraction {p: res[i].q - res[i].p, q: res[i].q});
}
res
}
pub fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let mut window = video_subsystem.window("Ford circles", 1200, 650)
.opengl()
.allow_highdpi()
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas()
.accelerated()
.present_vsync()
.build()
.unwrap();
canvas.set_draw_color(Color::RGB(0, 255, 255));
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap();
let mut col = bracket_color::prelude::HSV::new();
col.h = 0f32;
col.s = 1f32;
col.v = 1f32;
let mut tt: bool = false;
let mut t: bool = false;
let mut i: i32 = 2;
let ttf_context = sdl2::ttf::init().unwrap();
let mut font = ttf_context.load_font("/Users/ivabus/Library/Fonts/JetBrainsMono-Regular.ttf", 100).unwrap();
font.set_style(sdl2::ttf::FontStyle::NORMAL);
let texture_creator = canvas.texture_creator();
'running: loop {
/*
if i > 12 {
t = true;
};
if i < 3 {
t = false;
};
if t {
i -= 1;
} else {
i += 1;
}
*/
i = 5;
let tree: Vec<Fraction> = gen_tree(i);
let mut tree_x: Vec<f64> = Vec::new();
let mut tree_yr: Vec<f64> = Vec::new();
for i in 0..tree.len() {
tree_x.push(tree[i].p as f64 / tree[i].q as f64);
}
for i in 0..tree.len() {
tree_yr.push(1f64 / (2 * tree[i].q * tree[i].q) as f64);
}
if col.h >= 0.999 {
tt = true;
}
if col.h <= 0.001 {
tt = false;
}
if tt {
col.h -= 0.001;
} else {
col.h += 0.001;
}
let color_in_rgb = col.to_rgb();
let r: u8 = (color_in_rgb.r * 255f32) as u8;
let g: u8 = (color_in_rgb.g * 255f32) as u8;
let b: u8 = (color_in_rgb.b * 255f32) as u8;
/*
let r: u8 = 0;
let g: u8 = 127;
let b: u8 = 255;
*/
canvas.set_draw_color(Color::RGB(255 - r, 255 - g, 255 - b));
canvas.clear();
for i in 0..tree.len() {
canvas.filled_circle(300 * 2 + (600f64 * 2f64 * tree_x[i]) as i16,
600 * 2 - (tree_yr[i] * 2f64 * 600f64) as i16,
(600f64 * 2f64 * tree_yr[i]) as i16,
Color::RGB(r, g, b)).unwrap();
canvas.filled_circle(300 * 2 + (600f64 * 2f64 * tree_x[i]) as i16,
(tree_yr[i] * 2f64 * 600f64) as i16,
(600f64 * 2f64 * tree_yr[i]) as i16,
Color::RGB(r, g, b)).unwrap();
let mut text = String::from(&tree_x[i].to_string());
text.push_str("/");
text.push_str(&String::from(&tree_yr[i].to_string()));
canvas.set_draw_color(Color::RGB(255,255,255));
let mut test = font.render(&text).blended(Color::RGB(255,255,255)).unwrap();
let mut ll = texture_creator.create_texture_from_surface(&test).unwrap();
let TextureQuery { width, height, .. } = ll.query();
let padding = 64;
let mut target = get_centered_rect(
50u32 * text.len() as u32,
100,
50u32 * text.len() as u32,
100,
);
canvas.present();
target.set_x(300i32 * 2i32 + (600f64 * 2f64 * tree_x[i]) as i32 - (50i32 * text.len() as i32));
target.set_y(600i32 - 100i32);
canvas.present();
canvas.copy(&ll, None, Some(target)).unwrap();
canvas.present();
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
_ => {}
}
}
}
canvas.present();
}
}
@ivabus
Copy link
Author

ivabus commented Jul 17, 2022

2022-07-17.10.22.31.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment