commit 89fbae858c86ed9a2dc919c4fdff8ce966770e6a Author: Mikhail Mitrofanov Date: Wed Apr 9 16:34:48 2025 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/PointInteraction.iml b/.idea/PointInteraction.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/PointInteraction.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c75f58d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..618cd84 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,183 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "PointInteraction" +version = "0.1.0" +dependencies = [ + "rand", + "sdl3", +] + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.171" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" + +[[package]] +name = "rand" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" +dependencies = [ + "rand_chacha", + "rand_core", + "zerocopy", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + +[[package]] +name = "sdl3" +version = "0.14.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "156ff51a9b5b08ce6d1e3d38e9a1a079c04f09e88cd003f58e76be7d30b0a1c6" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "sdl3-sys", +] + +[[package]] +name = "sdl3-sys" +version = "0.4.7+SDL3-3.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d16a8a3623a4cb39a3661c81d9d4c5fd77ada27fc056e320b3651bf7bde1b1" + +[[package]] +name = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2586fea28e186957ef732a5f8b3be2da217d65c5969d4b1e17f973ebbe876879" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a996a8f63c5c4448cd959ac1bab0aaa3306ccfd060472f85943ee0750f0169be" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8c2d871 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "PointInteraction" +version = "0.1.0" +edition = "2024" + +[dependencies] +sdl3 = "0.14.19" +rand = "0.9.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..cc14894 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,134 @@ +use sdl3::event::Event; +use sdl3::keyboard::Keycode; +use sdl3::pixels::Color; +use sdl3::render::{FPoint, WindowCanvas}; +use std::f32::consts::PI; +use std::time::Duration; + +const SIZE: u32 = 400; +pub fn main() { + + let mut point = Point2D::new(100.0, 100.0); + + // let cartestian = point.to_cartesian(); + // let re_polar = Point2D::to_polar(cartestian); + // let plane = point.to_cartesian(); + // + // let mut test_point = Point2D::new(700.0, 100.0); + // test_point.optimize(); + // println!("{:?}", test_point); + + let sdl_context = sdl3::init().unwrap(); + let video_subsystem = sdl_context.video().unwrap(); + + let window = video_subsystem + .window("Point interaction", SIZE * 2, SIZE * 2) + .position_centered() + .build() + .unwrap(); + + let mut canvas = window.into_canvas(); + + canvas.set_draw_color(Color::RGB(0, 0, 0)); + canvas.clear(); + canvas.present(); + let mut event_pump = sdl_context.event_pump().unwrap(); + let mut i = 0; + 'running: loop { + i = (i + 1) % 255; + // canvas.set_draw_color(Color::RGB(0, 0, 0)); + // canvas.clear(); + for event in event_pump.poll_iter() { + match event { + Event::Quit { .. } + | Event::KeyDown { + keycode: Some(Keycode::Escape), + .. + } => break 'running, + _ => {} + } + } + // The rest of the game loop goes here... + point.x += 1.0/7.0; + point.y += 1.0; + point.optimize(); + canvas.set_draw_color(Color::RGB(255, 255, 255)); + point.draw(&mut canvas); + + canvas.present(); + std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 100)); + } +} + +#[derive(Debug)] +struct Point2D { + x: f32, + y: f32, +} + +impl Point2D { + pub fn new(x: f32, y: f32) -> Point2D { + Point2D { x, y } + } + + pub fn distance(&self, point: &Point2D) -> f32 { + (self.x - point.x).hypot(self.y - point.y) + } + + pub fn distance_sq(&self, point: &Point2D) -> f32 { + (self.x - point.x).powi(2) + (self.y - point.y).powi(2) + } + + pub fn alt_distance(&self, point: &Point2D) -> f32 { + 2.0 * SIZE as f32 - self.distance(point) + } + + pub fn to_cartesian(&self) -> Point2D { + let angle = (self.x / SIZE as f32) * PI; + let cos = angle.cos(); + let sin = angle.sin(); + let scale = self.y; + Point2D::new(cos * scale, sin * scale) + } + + pub fn to_polar(p: Point2D) -> Point2D { + let len = p.y.hypot(p.x); + let cos = p.x / len; + let sin = p.y / len; + let x = cos.acos(); + let mut y = sin.asin(); + let mb_sin = x.sin(); + if y.signum() != mb_sin.signum() { + y = -y; + } + Point2D::new(x, y) + } + + pub fn to_sdl(&self) -> FPoint { + FPoint::new(self.x + SIZE as f32, self.y + SIZE as f32) + } + + pub fn from_sdl(x: f32, y: f32) -> Point2D { + Point2D::new(x - SIZE as f32, y - SIZE as f32) + } + + pub fn draw(&self, canvas: &mut WindowCanvas) { + let mut polar = self.to_cartesian(); + let sdl_point = polar.to_sdl(); + canvas.draw_point(sdl_point).unwrap(); + } + + fn lapped(l: f32) -> f32 { + ((SIZE as f32) - (l.abs() - SIZE as f32).abs()) * -l.signum() + } + + pub fn optimize(&mut self) { + if self.x.abs() > SIZE as f32 { + self.x = Self::lapped(self.x); + } + + if self.y.abs() > SIZE as f32 { + self.y = Self::lapped(self.y); + } + } +}