Rename everything from suffix 2 to 2d for clarity.

This commit is contained in:
Juergen Stuber 2018-12-01 10:03:58 +01:00
parent c3ca8888fd
commit 3389551f05
7 changed files with 145 additions and 141 deletions

View File

@ -9,9 +9,9 @@ use rand::Rng;
use pixelfoo::color::Color; use pixelfoo::color::Color;
use pixelfoo::p2; use pixelfoo::p2;
use pixelfoo::point2::Point2; use pixelfoo::point2d::Point2d;
use pixelfoo::v2; use pixelfoo::v2;
use pixelfoo::vec2::Vec2; use pixelfoo::vec2d::Vec2d;
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Square { enum Square {
@ -41,7 +41,7 @@ fn send<T: Write>(w: &mut T, board: &Board) -> std::io::Result<()> {
} }
impl Board { impl Board {
fn new(board_size: Vec2, maze_size: Vec2) -> Board { fn new(board_size: Vec2d, maze_size: Vec2d) -> Board {
Board( Board(
(0..board_size.y) (0..board_size.y)
.map(move |y| { .map(move |y| {
@ -67,21 +67,21 @@ impl Board {
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )
} }
fn get(&self, pos: Point2) -> Square { fn get(&self, pos: Point2d) -> Square {
self.0[pos.y as usize][pos.x as usize] self.0[pos.y as usize][pos.x as usize]
} }
fn set(&mut self, pos: Point2, sq: Square) { fn set(&mut self, pos: Point2d, sq: Square) {
self.0[pos.y as usize][pos.x as usize] = sq; self.0[pos.y as usize][pos.x as usize] = sq;
} }
} }
struct Move { struct Move {
from: Point2, from: Point2d,
dir: Vec2, dir: Vec2d,
prio: i32, prio: i32,
} }
fn add_move(open: &mut Vec<Move>, arg: isize, from: Point2, dir: Vec2) { fn add_move(open: &mut Vec<Move>, arg: isize, from: Point2d, dir: Vec2d) {
open.push(Move { from, dir, prio: 0 }); open.push(Move { from, dir, prio: 0 });
open.sort_unstable_by(|Move { prio: pa, .. }, Move { prio: pb, .. }| pa.cmp(pb)); open.sort_unstable_by(|Move { prio: pa, .. }, Move { prio: pb, .. }| pa.cmp(pb));
} }
@ -164,7 +164,7 @@ fn main() -> std::io::Result<()> {
board.set(p1, Square::Corridor); board.set(p1, Square::Corridor);
board.set(p2, Square::Corridor); board.set(p2, Square::Corridor);
for dir1 in Vec2::directions() { for dir1 in Vec2d::directions() {
let p3 = p2 + dir1; let p3 = p2 + dir1;
let p4 = p3 + dir1; let p4 = p3 + dir1;
if board.get(p3) == Square::Unknown { if board.get(p3) == Square::Unknown {

View File

@ -1,5 +1,5 @@
pub mod color; pub mod color;
#[macro_use] #[macro_use]
pub mod point2; pub mod point2d;
#[macro_use] #[macro_use]
pub mod vec2; pub mod vec2d;

View File

@ -1,66 +0,0 @@
use std::ops::Add;
use crate::vec2::Vec2;
#[macro_export]
macro_rules! p2 {
( $x:expr, $y:expr ) => {
Point2::new($x, $y)
};
}
#[derive(Clone, Copy, Debug)]
pub struct Point2 {
pub x: i32,
pub y: i32,
}
impl Point2 {
pub fn new(x: i32, y: i32) -> Point2 {
Point2 { x, y }
}
pub fn neighbours(&self) -> Vec<Point2> {
Vec2::directions().iter().map(|v| self + v).collect()
}
}
impl Add<Vec2> for Point2 {
type Output = Point2;
fn add(self, other: Vec2) -> Point2 {
let x = self.x + other.x;
let y = self.y + other.y;
Point2 { x, y }
}
}
impl<'a> Add<&'a Vec2> for Point2 {
type Output = Point2;
fn add(self, other: &'a Vec2) -> Point2 {
let x = self.x + other.x;
let y = self.y + other.y;
Point2 { x, y }
}
}
impl<'a> Add<Vec2> for &'a Point2 {
type Output = Point2;
fn add(self, other: Vec2) -> Point2 {
let x = self.x + other.x;
let y = self.y + other.y;
Point2 { x, y }
}
}
impl<'a> Add<&'a Vec2> for &'a Point2 {
type Output = Point2;
fn add(self, other: &'a Vec2) -> Point2 {
let x = self.x + other.x;
let y = self.y + other.y;
Point2 { x, y }
}
}

66
src/point2d.rs Normal file
View File

@ -0,0 +1,66 @@
use std::ops::Add;
use crate::vec2d::Vec2d;
#[macro_export]
macro_rules! p2 {
( $x:expr, $y:expr ) => {
Point2d::new($x, $y)
};
}
#[derive(Clone, Copy, Debug)]
pub struct Point2d {
pub x: i32,
pub y: i32,
}
impl Point2d {
pub fn new(x: i32, y: i32) -> Point2d {
Point2d { x, y }
}
pub fn neighbours(&self) -> Vec<Point2d> {
Vec2d::directions().iter().map(|v| self + v).collect()
}
}
impl Add<Vec2d> for Point2d {
type Output = Point2d;
fn add(self, other: Vec2d) -> Point2d {
let x = self.x + other.x;
let y = self.y + other.y;
Point2d { x, y }
}
}
impl<'a> Add<&'a Vec2d> for Point2d {
type Output = Point2d;
fn add(self, other: &'a Vec2d) -> Point2d {
let x = self.x + other.x;
let y = self.y + other.y;
Point2d { x, y }
}
}
impl<'a> Add<Vec2d> for &'a Point2d {
type Output = Point2d;
fn add(self, other: Vec2d) -> Point2d {
let x = self.x + other.x;
let y = self.y + other.y;
Point2d { x, y }
}
}
impl<'a> Add<&'a Vec2d> for &'a Point2d {
type Output = Point2d;
fn add(self, other: &'a Vec2d) -> Point2d {
let x = self.x + other.x;
let y = self.y + other.y;
Point2d { x, y }
}
}

4
src/rect2d.rs Normal file
View File

@ -0,0 +1,4 @@
pub struct Rect2d {
origin: Point2d,
size: Vec2d,
}

View File

@ -1,64 +0,0 @@
use std::ops::Add;
#[macro_export]
macro_rules! v2 {
( $x:expr, $y:expr ) => {
Vec2::new($x, $y)
};
}
#[derive(Clone, Copy, Debug)]
pub struct Vec2 {
pub x: i32,
pub y: i32,
}
impl Vec2 {
pub fn new(x: i32, y: i32) -> Vec2 {
Vec2 { x, y }
}
pub fn directions() -> Vec<Vec2> {
vec![v2!(1, 0), v2!(0, 1), v2!(-1, 0), v2!(0, -1)]
}
}
impl Add for Vec2 {
type Output = Vec2;
fn add(self, other: Vec2) -> Vec2 {
let x = self.x + other.x;
let y = self.y + other.y;
Vec2 { x, y }
}
}
impl<'a> Add<&'a Vec2> for Vec2 {
type Output = Vec2;
fn add(self, other: &'a Vec2) -> Vec2 {
let x = self.x + other.x;
let y = self.y + other.y;
Vec2 { x, y }
}
}
impl<'a> Add<Vec2> for &'a Vec2 {
type Output = Vec2;
fn add(self, other: Vec2) -> Vec2 {
let x = self.x + other.x;
let y = self.y + other.y;
Vec2 { x, y }
}
}
impl<'a> Add<&'a Vec2> for &'a Vec2 {
type Output = Vec2;
fn add(self, other: &'a Vec2) -> Vec2 {
let x = self.x + other.x;
let y = self.y + other.y;
Vec2 { x, y }
}
}

64
src/vec2d.rs Normal file
View File

@ -0,0 +1,64 @@
use std::ops::Add;
#[macro_export]
macro_rules! v2 {
( $x:expr, $y:expr ) => {
Vec2d::new($x, $y)
};
}
#[derive(Clone, Copy, Debug)]
pub struct Vec2d {
pub x: i32,
pub y: i32,
}
impl Vec2d {
pub fn new(x: i32, y: i32) -> Vec2d {
Vec2d { x, y }
}
pub fn directions() -> Vec<Vec2d> {
vec![v2!(1, 0), v2!(0, 1), v2!(-1, 0), v2!(0, -1)]
}
}
impl Add for Vec2d {
type Output = Vec2d;
fn add(self, other: Vec2d) -> Vec2d {
let x = self.x + other.x;
let y = self.y + other.y;
Vec2d { x, y }
}
}
impl<'a> Add<&'a Vec2d> for Vec2d {
type Output = Vec2d;
fn add(self, other: &'a Vec2d) -> Vec2d {
let x = self.x + other.x;
let y = self.y + other.y;
Vec2d { x, y }
}
}
impl<'a> Add<Vec2d> for &'a Vec2d {
type Output = Vec2d;
fn add(self, other: Vec2d) -> Vec2d {
let x = self.x + other.x;
let y = self.y + other.y;
Vec2d { x, y }
}
}
impl<'a> Add<&'a Vec2d> for &'a Vec2d {
type Output = Vec2d;
fn add(self, other: &'a Vec2d) -> Vec2d {
let x = self.x + other.x;
let y = self.y + other.y;
Vec2d { x, y }
}
}