draw square

This commit is contained in:
bronku 2026-02-15 23:31:16 +01:00
parent 842f0d1f22
commit 6c73b01a52

View file

@ -4,26 +4,26 @@ const WINDOW_WIDTH: i32 = 640;
const WINDOW_HEIGHT: i32 = 480; const WINDOW_HEIGHT: i32 = 480;
struct Vec2 { struct Vec2 {
x: f32, x: f64,
y: f32, y: f64,
} }
impl Vec2 { impl Vec2 {
fn to_screen_space(self) -> (i32, i32) { fn to_screen_space(self) -> (i32, i32) {
return ( return (
((WINDOW_WIDTH as f32) * ((self.x + 1.0) / 2.0)) as i32, ((WINDOW_WIDTH as f64) * ((self.x + 1.0) / 2.0)) as i32,
((WINDOW_HEIGHT as f32) * ((self.y + 1.0) / 2.0)) as i32, ((WINDOW_HEIGHT as f64) * ((self.y + 1.0) / 2.0)) as i32,
); );
} }
} }
struct Vec3 { struct Vec3 {
x: f32, x: f64,
y: f32, y: f64,
z: f32, z: f64,
} }
impl Vec3 { impl Vec3 {
fn to_screen_space(self) -> (i32, i32) { fn to_screen_space(self) -> (i32, i32) {
const FOCAL_LENGTH: f32 = 1.0; const FOCAL_LENGTH: f64 = 1.0;
let camera_pos = Vec2 { let camera_pos = Vec2 {
x: FOCAL_LENGTH * self.x / self.z, x: FOCAL_LENGTH * self.x / self.z,
y: FOCAL_LENGTH * self.y / self.z, y: FOCAL_LENGTH * self.y / self.z,
@ -38,44 +38,52 @@ fn draw_point(d: &mut RaylibDrawHandle, pos: Vec3) {
d.draw_rectangle(window_x, window_y, POINT_SIZE, POINT_SIZE, Color::RED); d.draw_rectangle(window_x, window_y, POINT_SIZE, POINT_SIZE, Color::RED);
} }
fn draw_square(d: &mut RaylibDrawHandle, dist: f64) {
draw_point(
d,
Vec3 {
x: -0.5,
y: -0.5,
z: dist,
},
);
draw_point(
d,
Vec3 {
x: 0.5,
y: -0.5,
z: dist,
},
);
draw_point(
d,
Vec3 {
x: 0.5,
y: 0.5,
z: dist,
},
);
draw_point(
d,
Vec3 {
x: -0.5,
y: 0.5,
z: dist,
},
);
}
fn main() { fn main() {
let (mut rl, thread) = raylib::init().size(WINDOW_WIDTH, WINDOW_HEIGHT).build(); let (mut rl, thread) = raylib::init().size(WINDOW_WIDTH, WINDOW_HEIGHT).build();
while !rl.window_should_close() { while !rl.window_should_close() {
// let time = rl.get_time();
let mut d = rl.begin_drawing(&thread); let mut d = rl.begin_drawing(&thread);
draw_point( // draw_square(&mut d, (time - 5.0) / 10.0);
&mut d,
Vec3 { draw_square(&mut d, 1.0);
x: -0.5, draw_square(&mut d, 2.0);
y: -0.5,
z: 1.0,
},
);
draw_point(
&mut d,
Vec3 {
x: 0.5,
y: -0.5,
z: 1.0,
},
);
draw_point(
&mut d,
Vec3 {
x: 0.5,
y: 0.5,
z: 1.0,
},
);
draw_point(
&mut d,
Vec3 {
x: -0.5,
y: 0.5,
z: 1.0,
},
);
d.clear_background(Color::WHITE); d.clear_background(Color::WHITE);
} }