Skip to content

Commit 11d4e0e

Browse files
committed
Rename Point2D to Vec2 and Point3D to Vec3
1 parent 760f5b7 commit 11d4e0e

File tree

16 files changed

+112
-110
lines changed

16 files changed

+112
-110
lines changed

crates/utils/src/point.rs renamed to crates/utils/src/geometry.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//! 2D & 3D point implementations.
1+
//! 2D & 3D vector implementations.
22
33
use crate::number::{Integer, Number, Signed, UnsignedInteger};
44
use std::fmt::Debug;
55
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
66

7-
macro_rules! point_impl {
7+
macro_rules! vec_impl {
88
($n:literal, $tuple:tt =>
99
$(#[$m:meta])* $v:vis struct $s:ident{$($i:tt => $f:ident),+}
1010
) => {
@@ -42,7 +42,7 @@ macro_rules! point_impl {
4242
T::Unsigned::ZERO $(+ self.$f.abs_diff(rhs.$f))+
4343
}
4444

45-
/// Add the provided signed point, wrapping on overflow.
45+
/// Add the provided signed vector, wrapping on overflow.
4646
///
4747
/// Useful for adding a signed direction onto an unsigned position.
4848
#[inline]
@@ -145,12 +145,13 @@ macro_rules! point_impl {
145145
};
146146
}
147147

148-
point_impl! {2, (T, T) =>
149-
/// Struct representing a 2D point or vector.
150-
pub struct Point2D{0 => x, 1 => y}
148+
vec_impl! {2, (T, T) =>
149+
/// Struct representing a 2D vector or point.
150+
#[doc(alias("Vector", "Vector2", "Point", "Point2", "Point2D"))]
151+
pub struct Vec2{0 => x, 1 => y}
151152
}
152153

153-
impl<T: Signed> Point2D<T> {
154+
impl<T: Signed> Vec2<T> {
154155
pub const UP: Self = Self {
155156
x: T::ZERO,
156157
y: T::ONE,
@@ -189,7 +190,8 @@ impl<T: Signed> Point2D<T> {
189190
}
190191
}
191192

192-
point_impl! {3, (T, T, T) =>
193-
/// Struct representing a 3D point or vector.
194-
pub struct Point3D{0 => x, 1 => y, 2 => z}
193+
vec_impl! {3, (T, T, T) =>
194+
/// Struct representing a 3D vector or point.
195+
#[doc(alias("Vector3", "Point3", "Point3D"))]
196+
pub struct Vec3{0 => x, 1 => y, 2 => z}
195197
}

crates/utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod array;
55
pub mod bit;
66
pub mod date;
77
mod framework;
8+
pub mod geometry;
89
pub mod graph;
910
pub mod grid;
1011
pub mod input;
@@ -14,7 +15,6 @@ pub mod multithreading;
1415
pub mod multiversion;
1516
pub mod number;
1617
pub mod parser;
17-
pub mod point;
1818
pub mod simd;
1919
#[cfg(target_family = "wasm")]
2020
pub mod wasm;

crates/year2015/src/day03.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::collections::HashSet;
2-
use utils::point::Point2D;
2+
use utils::geometry::Vec2;
33
use utils::prelude::*;
44

55
/// Counting unique points.
66
#[derive(Clone, Debug)]
77
pub struct Day03 {
8-
directions: Vec<Point2D<i32>>,
8+
directions: Vec<Vec2<i32>>,
99
}
1010

1111
impl Day03 {
@@ -14,19 +14,19 @@ impl Day03 {
1414
directions: input
1515
.chars()
1616
.map(|c| match c {
17-
'^' => Ok(Point2D::UP),
18-
'>' => Ok(Point2D::RIGHT),
19-
'v' => Ok(Point2D::DOWN),
20-
'<' => Ok(Point2D::LEFT),
17+
'^' => Ok(Vec2::UP),
18+
'>' => Ok(Vec2::RIGHT),
19+
'v' => Ok(Vec2::DOWN),
20+
'<' => Ok(Vec2::LEFT),
2121
_ => Err(InputError::new(input, c, "expected one of ^>v<")),
2222
})
23-
.collect::<Result<Vec<Point2D<i32>>, InputError>>()?,
23+
.collect::<Result<Vec<Vec2<i32>>, InputError>>()?,
2424
})
2525
}
2626

2727
#[must_use]
2828
pub fn part1(&self) -> usize {
29-
let mut pos = Point2D::default();
29+
let mut pos = Vec2::default();
3030

3131
self.count_positions(|dir| {
3232
pos += dir;
@@ -45,9 +45,9 @@ impl Day03 {
4545
})
4646
}
4747

48-
fn count_positions(&self, mut f: impl FnMut(Point2D<i32>) -> Point2D<i32>) -> usize {
48+
fn count_positions(&self, mut f: impl FnMut(Vec2<i32>) -> Vec2<i32>) -> usize {
4949
let mut set = HashSet::with_capacity(self.directions.len());
50-
set.insert(Point2D::default());
50+
set.insert(Vec2::default());
5151

5252
for &dir in &self.directions {
5353
set.insert(f(dir));

crates/year2016/src/day01.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::HashSet;
2-
use utils::point::Point2D;
2+
use utils::geometry::Vec2;
33
use utils::prelude::*;
44

55
/// Calculating Manhattan distance.
@@ -26,8 +26,8 @@ impl Day01 {
2626

2727
#[must_use]
2828
pub fn part1(&self) -> u32 {
29-
let mut pos = Point2D::ORIGIN;
30-
let mut dir = Point2D::UP;
29+
let mut pos = Vec2::ORIGIN;
30+
let mut dir = Vec2::UP;
3131

3232
for &(turn, steps) in &self.instructions {
3333
dir = match turn {
@@ -42,8 +42,8 @@ impl Day01 {
4242

4343
#[must_use]
4444
pub fn part2(&self) -> u32 {
45-
let mut pos: Point2D<i32> = Point2D::ORIGIN;
46-
let mut dir = Point2D::UP;
45+
let mut pos: Vec2<i32> = Vec2::ORIGIN;
46+
let mut dir = Vec2::UP;
4747
let mut visited = HashSet::new();
4848

4949
for &(turn, steps) in &self.instructions {

crates/year2016/src/day13.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::{HashSet, VecDeque};
2-
use utils::point::Point2D;
2+
use utils::geometry::Vec2;
33
use utils::prelude::*;
44

55
/// Finding the shortest path.
@@ -12,18 +12,18 @@ pub struct Day13 {
1212
impl Day13 {
1313
pub fn new(input: &str, input_type: InputType) -> Result<Self, InputError> {
1414
let favorite_number = parser::u32().parse_complete(input)?;
15-
let target: Point2D<u32> = if input_type == InputType::Real {
16-
Point2D::new(31, 39)
15+
let target: Vec2<u32> = if input_type == InputType::Real {
16+
Vec2::new(31, 39)
1717
} else {
18-
Point2D::new(7, 4)
18+
Vec2::new(7, 4)
1919
};
2020

2121
// Use a hashset to store visited nodes to avoid having a fixed grid size, as theoretically
2222
// the shortest route to the target may first go a long way down/right.
2323
let mut visited = HashSet::new();
24-
visited.insert(Point2D::new(1, 1));
24+
visited.insert(Vec2::new(1, 1));
2525
let mut queue = VecDeque::new();
26-
queue.push_back((Point2D::new(1, 1), 0));
26+
queue.push_back((Vec2::new(1, 1), 0));
2727

2828
let (mut part1, mut part2) = (0, 0);
2929
while let Some((p, steps)) = queue.pop_front() {
@@ -37,11 +37,11 @@ impl Day13 {
3737
break;
3838
}
3939

40-
for next @ Point2D { x, y } in [
41-
Point2D::new(p.x.saturating_sub(1), p.y),
42-
Point2D::new(p.x.saturating_add(1), p.y),
43-
Point2D::new(p.x, p.y.saturating_sub(1)),
44-
Point2D::new(p.x, p.y.saturating_add(1)),
40+
for next @ Vec2 { x, y } in [
41+
Vec2::new(p.x.saturating_sub(1), p.y),
42+
Vec2::new(p.x.saturating_add(1), p.y),
43+
Vec2::new(p.x, p.y.saturating_sub(1)),
44+
Vec2::new(p.x, p.y.saturating_add(1)),
4545
] {
4646
let n = (x * x) + (3 * x) + (2 * x * y) + y + (y * y) + favorite_number;
4747
if n.count_ones().is_multiple_of(2) && !visited.contains(&next) {

crates/year2016/src/day22.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use utils::point::Point2D;
1+
use utils::geometry::Vec2;
22
use utils::prelude::*;
33

44
/// Solving a sliding puzzle.
@@ -11,7 +11,7 @@ pub struct Day22 {
1111
max_x: u32,
1212
max_y: u32,
1313
wall_x: u32,
14-
empty: Point2D<u32>,
14+
empty: Vec2<u32>,
1515
}
1616

1717
#[derive(Copy, Clone, Debug)]
@@ -100,7 +100,7 @@ impl Day22 {
100100
Ok(Self {
101101
max_x,
102102
max_y,
103-
empty: Point2D::new(empty.x, empty.y),
103+
empty: Vec2::new(empty.x, empty.y),
104104
wall_x,
105105
})
106106
}

crates/year2017/src/day11.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use utils::point::Point2D;
1+
use utils::geometry::Vec2;
22
use utils::prelude::*;
33

44
/// Calculating distances in a hexagonal grid.
@@ -15,17 +15,17 @@ pub struct Day11 {
1515
impl Day11 {
1616
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
1717
let steps = parser::literal_map!(
18-
"ne" => Point2D::new(1, -1),
19-
"nw" => Point2D::new(-1, 0),
20-
"se" => Point2D::new(1, 0),
21-
"sw" => Point2D::new(-1, 1),
22-
"n" => Point2D::new(0, -1),
23-
"s" => Point2D::new(0, 1),
18+
"ne" => Vec2::new(1, -1),
19+
"nw" => Vec2::new(-1, 0),
20+
"se" => Vec2::new(1, 0),
21+
"sw" => Vec2::new(-1, 1),
22+
"n" => Vec2::new(0, -1),
23+
"s" => Vec2::new(0, 1),
2424
)
2525
.with_suffix(b','.or(parser::eof()))
2626
.parse_iterator(input);
2727

28-
let mut pos = Point2D::new(0, 0);
28+
let mut pos = Vec2::new(0, 0);
2929
let mut max = 0;
3030
for step in steps {
3131
pos += step?;
@@ -38,7 +38,7 @@ impl Day11 {
3838
})
3939
}
4040

41-
fn hex_dist_to_origin(p: Point2D<i32>) -> u32 {
41+
fn hex_dist_to_origin(p: Vec2<i32>) -> u32 {
4242
(p.x.unsigned_abs() + (p.x + p.y).unsigned_abs() + p.y.unsigned_abs()) / 2
4343
}
4444

crates/year2017/src/day19.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use utils::point::Point2D;
1+
use utils::geometry::Vec2;
22
use utils::prelude::*;
33

44
/// Following a path.
@@ -11,7 +11,7 @@ pub struct Day19 {
1111
impl Day19 {
1212
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
1313
let lines = input.lines().collect::<Vec<_>>();
14-
let lookup = |p: Point2D<usize>| {
14+
let lookup = |p: Vec2<usize>| {
1515
if p.y < lines.len() && p.x < lines[p.y].len() {
1616
Some(lines[p.y].as_bytes()[p.x])
1717
} else {
@@ -22,8 +22,8 @@ impl Day19 {
2222
let Some(start_col) = lines.first().and_then(|l| l.find('|')) else {
2323
return Err(InputError::new(input, 0, "expected '|' on the first line"));
2424
};
25-
let mut pos = Point2D::new(start_col, 0);
26-
let mut dir = Point2D::new(0, 1);
25+
let mut pos = Vec2::new(start_col, 0);
26+
let mut dir = Vec2::new(0, 1);
2727

2828
let mut letters = String::new();
2929
let mut steps = 1;

crates/year2017/src/day20.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::HashMap;
2-
use utils::point::Point3D;
2+
use utils::geometry::Vec3;
33
use utils::prelude::*;
44

55
/// Simulating colliding particles.
@@ -10,14 +10,14 @@ pub struct Day20 {
1010

1111
#[derive(Clone, Debug)]
1212
struct Particle {
13-
position: Point3D<i64>,
14-
velocity: Point3D<i64>,
15-
acceleration: Point3D<i64>,
13+
position: Vec3<i64>,
14+
velocity: Vec3<i64>,
15+
acceleration: Vec3<i64>,
1616
}
1717

1818
impl Day20 {
1919
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
20-
let vector = parser::i64().repeat_n(b',').map(Point3D::from);
20+
let vector = parser::i64().repeat_n(b',').map(Vec3::from);
2121

2222
Ok(Self {
2323
particles: vector
@@ -79,7 +79,7 @@ impl Day20 {
7979
}
8080

8181
impl Particle {
82-
fn position_at_time(&self, time: u64) -> Point3D<i64> {
82+
fn position_at_time(&self, time: u64) -> Vec3<i64> {
8383
self.position
8484
+ (self.velocity * time as i64)
8585
+ (self.acceleration * (time as i64 * time as i64 / 2))

crates/year2018/src/day06.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use utils::point::Point2D;
1+
use utils::geometry::Vec2;
22
use utils::prelude::*;
33

44
/// Measuring regions using Manhattan distance.
55
#[derive(Clone, Debug)]
66
pub struct Day06 {
7-
locations: Vec<Point2D<usize>>,
7+
locations: Vec<Vec2<usize>>,
88
region_threshold: u32,
99
}
1010

@@ -14,7 +14,7 @@ impl Day06 {
1414
pub fn new(input: &str, input_type: InputType) -> Result<Self, InputError> {
1515
let locations = parser::number_range(0..=999)
1616
.repeat_n(", ")
17-
.map(Point2D::from)
17+
.map(Vec2::from)
1818
.parse_lines(input)?;
1919
if locations.is_empty() {
2020
return Err(InputError::new(input, 0, "expected at least one location"));
@@ -29,9 +29,9 @@ impl Day06 {
2929
return Err(InputError::new(input, 0, "coordinate range too large"));
3030
}
3131

32-
let min_point = Point2D::new(min, min);
32+
let min_loc = Vec2::new(min, min);
3333
Ok(Self {
34-
locations: locations.into_iter().map(|p| p - min_point).collect(),
34+
locations: locations.into_iter().map(|p| p - min_loc).collect(),
3535
region_threshold: match input_type {
3636
InputType::Example => 32,
3737
InputType::Real => 10000,

0 commit comments

Comments
 (0)