Replace macros for 2d literals by functions.
This commit is contained in:
parent
9877915249
commit
af5e1db20a
@ -8,9 +8,9 @@ use rand::thread_rng;
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use pixelfoo::color::Color;
|
use pixelfoo::color::Color;
|
||||||
use pixelfoo::p2;
|
use pixelfoo::point2d::p2d;
|
||||||
use pixelfoo::point2d::Point2d;
|
use pixelfoo::point2d::Point2d;
|
||||||
use pixelfoo::v2;
|
use pixelfoo::vec2d::v2d;
|
||||||
use pixelfoo::vec2d::Vec2d;
|
use pixelfoo::vec2d::Vec2d;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
@ -118,20 +118,20 @@ impl Board {
|
|||||||
fn draw_horizontal_segment(&mut self, pos: Point2d, size: Vec2d) {
|
fn draw_horizontal_segment(&mut self, pos: Point2d, size: Vec2d) {
|
||||||
for x in 0..size.x {
|
for x in 0..size.x {
|
||||||
let xn = size.x - 1 - x;
|
let xn = size.x - 1 - x;
|
||||||
self.set_horizontal(pos + v2!(x, 0));
|
self.set_horizontal(pos + v2d(x, 0));
|
||||||
for y in 1..x.min(xn).min(size.y / 2) + 1 {
|
for y in 1..x.min(xn).min(size.y / 2) + 1 {
|
||||||
self.set_horizontal(pos + v2!(x, y));
|
self.set_horizontal(pos + v2d(x, y));
|
||||||
self.set_horizontal(pos + v2!(x, -y));
|
self.set_horizontal(pos + v2d(x, -y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn draw_vertical_segment(&mut self, pos: Point2d, size: Vec2d) {
|
fn draw_vertical_segment(&mut self, pos: Point2d, size: Vec2d) {
|
||||||
for y in 0..size.y {
|
for y in 0..size.y {
|
||||||
let yn = size.y - 1 - y;
|
let yn = size.y - 1 - y;
|
||||||
self.set_vertical(pos + v2!(0, y));
|
self.set_vertical(pos + v2d(0, y));
|
||||||
for x in 1..y.min(yn).min(size.x / 2) + 1 {
|
for x in 1..y.min(yn).min(size.x / 2) + 1 {
|
||||||
self.set_vertical(pos + v2!(x, y));
|
self.set_vertical(pos + v2d(x, y));
|
||||||
self.set_vertical(pos + v2!(-x, y));
|
self.set_vertical(pos + v2d(-x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,28 +139,28 @@ impl Board {
|
|||||||
let length = size.x;
|
let length = size.x;
|
||||||
let width = size.y;
|
let width = size.y;
|
||||||
let delta = length + 1;
|
let delta = length + 1;
|
||||||
let hsize = v2!(length, width);
|
let hsize = v2d(length, width);
|
||||||
let vsize = v2!(width, length);
|
let vsize = v2d(width, length);
|
||||||
if (segments & (1 << 0)) != 0 {
|
if (segments & (1 << 0)) != 0 {
|
||||||
self.draw_horizontal_segment(pos + v2!(1, 0), hsize);
|
self.draw_horizontal_segment(pos + v2d(1, 0), hsize);
|
||||||
}
|
}
|
||||||
if (segments & (1 << 1)) != 0 {
|
if (segments & (1 << 1)) != 0 {
|
||||||
self.draw_vertical_segment(pos + v2!(delta, 1), vsize);
|
self.draw_vertical_segment(pos + v2d(delta, 1), vsize);
|
||||||
}
|
}
|
||||||
if (segments & (1 << 2)) != 0 {
|
if (segments & (1 << 2)) != 0 {
|
||||||
self.draw_vertical_segment(pos + v2!(delta, delta + 1), vsize);
|
self.draw_vertical_segment(pos + v2d(delta, delta + 1), vsize);
|
||||||
}
|
}
|
||||||
if (segments & (1 << 3)) != 0 {
|
if (segments & (1 << 3)) != 0 {
|
||||||
self.draw_horizontal_segment(pos + v2!(1, 2 * delta), hsize);
|
self.draw_horizontal_segment(pos + v2d(1, 2 * delta), hsize);
|
||||||
}
|
}
|
||||||
if (segments & (1 << 4)) != 0 {
|
if (segments & (1 << 4)) != 0 {
|
||||||
self.draw_vertical_segment(pos + v2!(0, 1), vsize);
|
self.draw_vertical_segment(pos + v2d(0, 1), vsize);
|
||||||
}
|
}
|
||||||
if (segments & (1 << 5)) != 0 {
|
if (segments & (1 << 5)) != 0 {
|
||||||
self.draw_vertical_segment(pos + v2!(0, delta + 1), vsize);
|
self.draw_vertical_segment(pos + v2d(0, delta + 1), vsize);
|
||||||
}
|
}
|
||||||
if (segments & (1 << 6)) != 0 {
|
if (segments & (1 << 6)) != 0 {
|
||||||
self.draw_horizontal_segment(pos + v2!(1, delta), hsize);
|
self.draw_horizontal_segment(pos + v2d(1, delta), hsize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -201,9 +201,9 @@ fn main() -> std::io::Result<()> {
|
|||||||
let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
|
let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
|
||||||
let mut t_wait = 0.0; // s
|
let mut t_wait = 0.0; // s
|
||||||
|
|
||||||
let board_size = v2!(x_size as i32, y_size as i32);
|
let board_size = v2d(x_size as i32, y_size as i32);
|
||||||
// round down to odd size for maze
|
// round down to odd size for maze
|
||||||
let maze_size = v2!(
|
let maze_size = v2d(
|
||||||
(board_size.x - 1) / 2 * 2 + 1,
|
(board_size.x - 1) / 2 * 2 + 1,
|
||||||
(board_size.y - 1) / 2 * 2 + 1
|
(board_size.y - 1) / 2 * 2 + 1
|
||||||
);
|
);
|
||||||
@ -219,31 +219,31 @@ fn main() -> std::io::Result<()> {
|
|||||||
board = Board::new(board_size, maze_size);
|
board = Board::new(board_size, maze_size);
|
||||||
|
|
||||||
// TODO draw stuff in prios
|
// TODO draw stuff in prios
|
||||||
let segment_size = v2!(11, 5);
|
let segment_size = v2d(11, 5);
|
||||||
board.draw_7_segments(p2!(6, 8), segment_size, 0x06);
|
board.draw_7_segments(p2d(6, 8), segment_size, 0x06);
|
||||||
board.draw_7_segments(p2!(24, 8), segment_size, 0x4f);
|
board.draw_7_segments(p2d(24, 8), segment_size, 0x4f);
|
||||||
board.draw_7_segments(p2!(42, 8), segment_size, 0x4f);
|
board.draw_7_segments(p2d(42, 8), segment_size, 0x4f);
|
||||||
board.draw_7_segments(p2!(60, 8), segment_size, 0x07);
|
board.draw_7_segments(p2d(60, 8), segment_size, 0x07);
|
||||||
|
|
||||||
// start building walls from the border
|
// start building walls from the border
|
||||||
for x in (2..(maze_size.x - 2)).step_by(2) {
|
for x in (2..(maze_size.x - 2)).step_by(2) {
|
||||||
add_move(&board, &mut open, &mut rng, p2!(x, 0), v2!(0, 1));
|
add_move(&board, &mut open, &mut rng, p2d(x, 0), v2d(0, 1));
|
||||||
add_move(
|
add_move(
|
||||||
&board,
|
&board,
|
||||||
&mut open,
|
&mut open,
|
||||||
&mut rng,
|
&mut rng,
|
||||||
p2!(x, maze_size.y - 1),
|
p2d(x, maze_size.y - 1),
|
||||||
v2!(0, -1),
|
v2d(0, -1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
for y in (2..(maze_size.y - 2)).step_by(2) {
|
for y in (2..(maze_size.y - 2)).step_by(2) {
|
||||||
add_move(&board, &mut open, &mut rng, p2!(0, y), v2!(1, 0));
|
add_move(&board, &mut open, &mut rng, p2d(0, y), v2d(1, 0));
|
||||||
add_move(
|
add_move(
|
||||||
&board,
|
&board,
|
||||||
&mut open,
|
&mut open,
|
||||||
&mut rng,
|
&mut rng,
|
||||||
p2!(maze_size.x - 1, y),
|
p2d(maze_size.x - 1, y),
|
||||||
v2!(-1, 0),
|
v2d(-1, 0),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -282,8 +282,8 @@ fn main() -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if open.is_empty() {
|
if open.is_empty() {
|
||||||
board.set(p2!(1, 1), Square::Start);
|
board.set(p2d(1, 1), Square::Start);
|
||||||
board.set(p2!(maze_size.x - 2, maze_size.y - 2), Square::Finish);
|
board.set(p2d(maze_size.x - 2, maze_size.y - 2), Square::Finish);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,9 +8,9 @@ use rand::thread_rng;
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use pixelfoo::color::Color;
|
use pixelfoo::color::Color;
|
||||||
use pixelfoo::p2;
|
use pixelfoo::point2d::p2d;
|
||||||
use pixelfoo::point2d::Point2d;
|
use pixelfoo::point2d::Point2d;
|
||||||
use pixelfoo::v2;
|
use pixelfoo::vec2d::v2d;
|
||||||
use pixelfoo::vec2d::Vec2d;
|
use pixelfoo::vec2d::Vec2d;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
@ -103,14 +103,14 @@ fn main() -> std::io::Result<()> {
|
|||||||
let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
|
let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
|
||||||
let mut t_wait = 0.0; // s
|
let mut t_wait = 0.0; // s
|
||||||
|
|
||||||
let board_size = v2!(x_size as i32, y_size as i32);
|
let board_size = v2d(x_size as i32, y_size as i32);
|
||||||
// round down to odd size for maze
|
// round down to odd size for maze
|
||||||
let maze_size = v2!(
|
let maze_size = v2d(
|
||||||
(board_size.x - 1) / 2 * 2 + 1,
|
(board_size.x - 1) / 2 * 2 + 1,
|
||||||
(board_size.y - 1) / 2 * 2 + 1
|
(board_size.y - 1) / 2 * 2 + 1
|
||||||
);
|
);
|
||||||
// pick odd position at or near the middle
|
// pick odd position at or near the middle
|
||||||
let maze_mid = p2!((maze_size.x - 1) / 4 * 2 + 1, (maze_size.y - 1) / 4 * 2 + 1);
|
let maze_mid = p2d((maze_size.x - 1) / 4 * 2 + 1, (maze_size.y - 1) / 4 * 2 + 1);
|
||||||
|
|
||||||
let mut board = Board(Vec::new());
|
let mut board = Board(Vec::new());
|
||||||
let mut open = Vec::new();
|
let mut open = Vec::new();
|
||||||
@ -124,10 +124,10 @@ fn main() -> std::io::Result<()> {
|
|||||||
|
|
||||||
// start in the middle
|
// start in the middle
|
||||||
board.set(maze_mid, Square::Corridor);
|
board.set(maze_mid, Square::Corridor);
|
||||||
add_move(&mut open, arg, maze_mid, v2!(1, 0));
|
add_move(&mut open, arg, maze_mid, v2d(1, 0));
|
||||||
add_move(&mut open, arg, maze_mid, v2!(0, 1));
|
add_move(&mut open, arg, maze_mid, v2d(0, 1));
|
||||||
add_move(&mut open, arg, maze_mid, v2!(-1, 0));
|
add_move(&mut open, arg, maze_mid, v2d(-1, 0));
|
||||||
add_move(&mut open, arg, maze_mid, v2!(0, -1));
|
add_move(&mut open, arg, maze_mid, v2d(0, -1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,8 +180,8 @@ fn main() -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if open.is_empty() {
|
if open.is_empty() {
|
||||||
board.set(p2!(1, 1), Square::Start);
|
board.set(p2d(1, 1), Square::Start);
|
||||||
board.set(p2!(maze_size.x - 2, maze_size.y - 2), Square::Finish);
|
board.set(p2d(maze_size.x - 2, maze_size.y - 2), Square::Finish);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ use rand::Rng;
|
|||||||
use pixelfoo::color::Color;
|
use pixelfoo::color::Color;
|
||||||
use pixelfoo::point2d::Point2d;
|
use pixelfoo::point2d::Point2d;
|
||||||
use pixelfoo::rect2d::Rect2d;
|
use pixelfoo::rect2d::Rect2d;
|
||||||
use pixelfoo::v2;
|
use pixelfoo::vec2d::v2d;
|
||||||
use pixelfoo::vec2d::Vec2d;
|
use pixelfoo::vec2d::Vec2d;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
@ -39,7 +39,7 @@ impl Board {
|
|||||||
pub fn size(&self) -> Vec2d {
|
pub fn size(&self) -> Vec2d {
|
||||||
let x_size = self.0[0].len() as i32;
|
let x_size = self.0[0].len() as i32;
|
||||||
let y_size = self.0.len() as i32;
|
let y_size = self.0.len() as i32;
|
||||||
v2!(x_size, y_size)
|
v2d(x_size, y_size)
|
||||||
}
|
}
|
||||||
pub fn rect(&self) -> Rect2d {
|
pub fn rect(&self) -> Rect2d {
|
||||||
Rect2d::new(0, self.size().x, 0, self.size().y)
|
Rect2d::new(0, self.size().x, 0, self.size().y)
|
||||||
@ -92,7 +92,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
eprintln!("screen size {}x{}, arg {}", x_size, y_size, arg);
|
eprintln!("screen size {}x{}, arg {}", x_size, y_size, arg);
|
||||||
|
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let size = v2!(x_size as i32, y_size as i32);
|
let size = v2d(x_size as i32, y_size as i32);
|
||||||
|
|
||||||
let p_empty = 0.25;
|
let p_empty = 0.25;
|
||||||
let p_grass = 0.25;
|
let p_grass = 0.25;
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
pub mod color;
|
pub mod color;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
pub mod vec2d;
|
pub mod vec2d;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
pub mod point2d;
|
pub mod point2d;
|
||||||
|
|
||||||
pub mod rect2d;
|
pub mod rect2d;
|
||||||
|
@ -2,13 +2,6 @@ use std::ops::Add;
|
|||||||
|
|
||||||
use crate::vec2d::Vec2d;
|
use crate::vec2d::Vec2d;
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! p2 {
|
|
||||||
( $x:expr, $y:expr ) => {
|
|
||||||
Point2d::new($x, $y)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct Point2d {
|
pub struct Point2d {
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
@ -25,6 +18,10 @@ impl Point2d {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn p2d(x: i32, y: i32) -> Point2d {
|
||||||
|
Point2d::new(x, y)
|
||||||
|
}
|
||||||
|
|
||||||
impl Add<Vec2d> for Point2d {
|
impl Add<Vec2d> for Point2d {
|
||||||
type Output = Point2d;
|
type Output = Point2d;
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use crate::p2;
|
use crate::point2d::p2d;
|
||||||
use crate::point2d::Point2d;
|
use crate::point2d::Point2d;
|
||||||
use crate::v2;
|
use crate::vec2d::v2d;
|
||||||
use crate::vec2d::Vec2d;
|
use crate::vec2d::Vec2d;
|
||||||
|
|
||||||
pub struct Rect2d {
|
pub struct Rect2d {
|
||||||
@ -13,8 +13,8 @@ pub struct Rect2d {
|
|||||||
impl Rect2d {
|
impl Rect2d {
|
||||||
pub fn new(x0: i32, x1: i32, y0: i32, y1: i32) -> Rect2d {
|
pub fn new(x0: i32, x1: i32, y0: i32, y1: i32) -> Rect2d {
|
||||||
assert!(x0 <= x1 && y0 <= y1);
|
assert!(x0 <= x1 && y0 <= y1);
|
||||||
let origin = p2!(x0, y0);
|
let origin = p2d(x0, y0);
|
||||||
let size = v2!(x1 - x0, y1 - y0);
|
let size = v2d(x1 - x0, y1 - y0);
|
||||||
Rect2d { origin, size }
|
Rect2d { origin, size }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +49,10 @@ impl Rect2d {
|
|||||||
{
|
{
|
||||||
let x = rng.gen_range(self.left(), self.right());
|
let x = rng.gen_range(self.left(), self.right());
|
||||||
let y = rng.gen_range(self.bottom(), self.top());
|
let y = rng.gen_range(self.bottom(), self.top());
|
||||||
p2!(x, y)
|
p2d(x, y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn r2d(x0: i32, x1: i32, y0: i32, y1: i32) -> Rect2d {
|
||||||
|
Rect2d::new(x0, x1, y0, y1)
|
||||||
|
}
|
||||||
|
13
src/vec2d.rs
13
src/vec2d.rs
@ -1,12 +1,5 @@
|
|||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! v2 {
|
|
||||||
( $x:expr, $y:expr ) => {
|
|
||||||
Vec2d::new($x, $y)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct Vec2d {
|
pub struct Vec2d {
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
@ -19,10 +12,14 @@ impl Vec2d {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn directions() -> Vec<Vec2d> {
|
pub fn directions() -> Vec<Vec2d> {
|
||||||
vec![v2!(1, 0), v2!(0, 1), v2!(-1, 0), v2!(0, -1)]
|
vec![v2d(1, 0), v2d(0, 1), v2d(-1, 0), v2d(0, -1)]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn v2d(x: i32, y: i32) -> Vec2d {
|
||||||
|
Vec2d::new(x, y)
|
||||||
|
}
|
||||||
|
|
||||||
impl Add for Vec2d {
|
impl Add for Vec2d {
|
||||||
type Output = Vec2d;
|
type Output = Vec2d;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user