Новый алгоритм и измерение производительности. Сейчас 3991 мс
This commit is contained in:
+57
-18
@@ -4,17 +4,18 @@ use sdl3::pixels::Color;
|
||||
use sdl3::render::{FPoint, WindowCanvas};
|
||||
use std::f32::consts::PI;
|
||||
use std::ops;
|
||||
use std::ops::Rem;
|
||||
use std::time::Instant;
|
||||
|
||||
const SIZE: u32 = 400;
|
||||
const SIZE_F: f32 = 400.0;
|
||||
pub fn main() {
|
||||
let mut point = Point2D::new(0.0, 0.0);
|
||||
let point = Point2D::new(0.0, 0.0);
|
||||
|
||||
let mut cycle = 0_f32;
|
||||
let radius = 100_f32;
|
||||
let mut pasted_points: Vec<FPoint> = vec![point.to_cartesian().to_sdl()];
|
||||
|
||||
|
||||
let sdl_context = sdl3::init().unwrap();
|
||||
let video_subsystem = sdl_context.video().unwrap();
|
||||
|
||||
@@ -30,10 +31,9 @@ pub fn main() {
|
||||
canvas.clear();
|
||||
canvas.present();
|
||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||
let mut i = 0;
|
||||
let time = Instant::now();
|
||||
let mut count = 0;
|
||||
'running: loop {
|
||||
let time = Instant::now();
|
||||
i = (i + 1) % 255;
|
||||
canvas.set_draw_color(Color::RGB(0, 0, 0));
|
||||
canvas.clear();
|
||||
for event in event_pump.poll_iter() {
|
||||
@@ -52,15 +52,21 @@ pub fn main() {
|
||||
let sin = cycle.sin() * radius;
|
||||
let mut next = Point2D::new(cos + cycle * 2.0 * PI, sin);
|
||||
next.optimize();
|
||||
|
||||
|
||||
pasted_points.push(next.to_cartesian().to_sdl());
|
||||
count += 1;
|
||||
canvas.set_draw_color(Color::RGB(255, 255, 255));
|
||||
|
||||
Point2D::draw_point(&mut canvas, &pasted_points);
|
||||
|
||||
canvas.present();
|
||||
println!("Frame time: {}", time.elapsed().as_millis());
|
||||
|
||||
cycle += 0.01;
|
||||
cycle -= 0.01;
|
||||
|
||||
if count == 10000 {
|
||||
println!("Time for 10000 is {} ms", time.elapsed().as_millis());
|
||||
}
|
||||
|
||||
// std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 200));
|
||||
}
|
||||
@@ -87,44 +93,69 @@ impl Point2D {
|
||||
}
|
||||
|
||||
pub fn alt_distance(&self, point: &Point2D) -> f32 {
|
||||
2.0 * SIZE as f32 - self.distance(point)
|
||||
2.0 * SIZE_F - self.distance(point)
|
||||
}
|
||||
|
||||
pub fn to_cartesian(&self) -> Point2D {
|
||||
let angle = (self.x / SIZE as f32) * PI;
|
||||
let angle = (self.x / SIZE_F) * PI;
|
||||
let cos = angle.cos();
|
||||
let sin = angle.sin();
|
||||
let scale = (SIZE as f32 + self.y) / 2.0;
|
||||
let scale = (SIZE_F + self.y) / 2.0;
|
||||
Point2D::new(cos * scale, sin * scale)
|
||||
}
|
||||
|
||||
pub fn to_super_space(&self) -> Point2D {
|
||||
let len = self.x.hypot(self.y);
|
||||
let y = len * 2.0 - SIZE_F;
|
||||
|
||||
let cos = self.x / len;
|
||||
|
||||
let acos = cos.acos();
|
||||
|
||||
let mut x = acos * SIZE_F / PI;
|
||||
|
||||
if self.y < 0.0 {
|
||||
x = -x;
|
||||
}
|
||||
|
||||
Point2D::new(x, y)
|
||||
}
|
||||
|
||||
pub fn to_sdl(&self) -> FPoint {
|
||||
FPoint::new(self.x + SIZE as f32, self.y + SIZE as f32)
|
||||
FPoint::new(self.x + SIZE_F, self.y + SIZE_F)
|
||||
}
|
||||
|
||||
pub fn from_sdl(x: f32, y: f32) -> Point2D {
|
||||
Point2D::new(x - SIZE as f32, y - SIZE as f32)
|
||||
Point2D::new(x - SIZE_F, y - SIZE_F)
|
||||
}
|
||||
|
||||
pub fn draw(&self, canvas: &mut WindowCanvas) {
|
||||
let mut polar = self.to_cartesian();
|
||||
let 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()
|
||||
let mut remainder = l.rem(SIZE_F * 2.0);
|
||||
|
||||
if remainder.abs() > SIZE_F {
|
||||
let out_delta = remainder.abs() - SIZE_F;
|
||||
let re_new = SIZE_F - out_delta;
|
||||
remainder = -re_new * l.signum()
|
||||
}
|
||||
|
||||
remainder
|
||||
}
|
||||
|
||||
pub fn optimize(&mut self) {
|
||||
if self.x.abs() > SIZE as f32 {
|
||||
if self.x.abs() > SIZE_F {
|
||||
self.x = Self::lapped(self.x);
|
||||
}
|
||||
|
||||
if self.y.abs() > SIZE as f32 {
|
||||
if self.y.abs() > SIZE_F {
|
||||
let side = self.y.signum();
|
||||
self.y += -side * 2.0 * (self.y.abs() - SIZE as f32);
|
||||
self.x = self.x + SIZE as f32;
|
||||
self.y += -side * 2.0 * (self.y.abs() - SIZE_F);
|
||||
self.x = self.x + SIZE_F;
|
||||
self.spin = !self.spin;
|
||||
}
|
||||
}
|
||||
@@ -133,6 +164,14 @@ impl Point2D {
|
||||
// canvas.draw_points(&points).unwrap()
|
||||
canvas.draw_points(&points[..]).unwrap();
|
||||
}
|
||||
|
||||
pub fn eq(&self, point: &Point2D) -> bool {
|
||||
(self.x.abs() - point.x.abs()).abs() < 0.1 && (self.y.abs() - point.y.abs()).abs() < 0.1
|
||||
}
|
||||
|
||||
pub fn eq_spin(&self, point: &Point2D) -> bool {
|
||||
self.eq(point) && self.spin == point.spin
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::AddAssign for Point2D {
|
||||
|
||||
Reference in New Issue
Block a user