Skip to content

Commit

Permalink
Merge pull request #75 from podusowski/minor-refa
Browse files Browse the repository at this point in the history
Minor refactor
  • Loading branch information
podusowski authored Nov 21, 2023
2 parents 96258a0 + 1d174b8 commit 7b47238
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
3 changes: 2 additions & 1 deletion walkers/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a, 'b> Map<'a, 'b> {
}
}

/// Projects geographical position into screen pixels, suitable for [`egui::Painter`].
/// Projects geographical position into pixels on the viewport, suitable for [`egui::Painter`].
#[derive(Clone)]
pub struct Projector {
clip_rect: Rect,
Expand All @@ -69,6 +69,7 @@ pub struct Projector {
}

impl Projector {
/// Project `position` into pixels on the viewport.
pub fn project(&self, position: Position) -> Vec2 {
// Turn that into a flat, mercator projection.
let projected_position = position.project(self.memory.zoom.round());
Expand Down
16 changes: 5 additions & 11 deletions walkers/src/mercator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Position {

/// Project geographical position into a 2D plane using Mercator.
pub(crate) fn project(&self, zoom: u8) -> Pixels {
let (x, y) = mercator_normalized((*self).into());
let (x, y) = mercator_normalized(*self);

// Map that into a big bitmap made out of web tiles.
let number_of_pixels = 2u32.pow(zoom as u32) * TILE_SIZE;
Expand All @@ -47,7 +47,7 @@ impl Position {

/// Tile this position is on.
pub(crate) fn tile_id(&self, zoom: u8) -> TileId {
let (x, y) = mercator_normalized((*self).into());
let (x, y) = mercator_normalized(*self);

// Map that into a big bitmap made out of web tiles.
let number_of_tiles = 2u32.pow(zoom as u32);
Expand All @@ -58,12 +58,6 @@ impl Position {
}
}

impl From<Position> for (f64, f64) {
fn from(value: Position) -> Self {
(value.lon(), value.lat())
}
}

/// Location projected on the screen or an abstract bitmap.
pub type Pixels = geo_types::Point;

Expand All @@ -82,10 +76,10 @@ impl PixelsExt for Pixels {
/// Size of the tiles used by the services like the OSM.
pub(crate) const TILE_SIZE: u32 = 256;

fn mercator_normalized((x, y): (f64, f64)) -> (f64, f64) {
fn mercator_normalized(position: Position) -> (f64, f64) {
// Project into Mercator (cylindrical map projection).
let x = x.to_radians();
let y = y.to_radians().tan().asinh();
let x = position.lon().to_radians();
let y = position.lat().to_radians().tan().asinh();

// Scale both x and y to 0-1 range.
let x = (1. + (x / PI)) / 2.;
Expand Down

0 comments on commit 7b47238

Please sign in to comment.