Skip to content

Commit f57a628

Browse files
committed
Added map projection class.
1 parent 0f4cbc0 commit f57a628

File tree

3 files changed

+113
-16
lines changed

3 files changed

+113
-16
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*##########################################################################################
2+
3+
PAX SAPIENTICA Library 💀🌿🌏
4+
5+
[Planning] 2023 As Project
6+
[Production] 2023 As Project
7+
[Contact Us] wanotaitei@gmail.com https://github.com/AsPJT/PAX_SAPIENTICA
8+
[License] Distributed under the CC0 1.0. https://creativecommons.org/publicdomain/zero/1.0/
9+
10+
##########################################################################################*/
11+
12+
#ifndef PAX_SAPIENTICA_TYPE_MAP_PROJECTION_HPP
13+
#define PAX_SAPIENTICA_TYPE_MAP_PROJECTION_HPP
14+
15+
/*##########################################################################################
16+
17+
##########################################################################################*/
18+
19+
#include <PAX_SAPIENTICA/Math.hpp>
20+
21+
namespace paxs {
22+
23+
struct MercatorDeg;
24+
struct EquirectangularDeg;
25+
26+
struct MercatorDeg : paxs::Vector2<double> {
27+
public:
28+
double toEquirectangularRadY() const {
29+
return static_cast<double>(std::asin(std::tanh(paxs::MathF64::degToRad(this->y))));
30+
}
31+
double toEquirectangularDegY() const {
32+
return static_cast<double>(paxs::MathF64::radToDeg(toEquirectangularRadY()));
33+
}
34+
operator EquirectangularDeg() const;
35+
};
36+
37+
struct EquirectangularDeg : paxs::Vector2<double> {
38+
public:
39+
double toMercatorRadY() const {
40+
return static_cast<double>((y >= 0 ? 1 : -1) * std::abs(std::log(std::abs(std::tan(paxs::MathF64::pi() / 4.0 - paxs::MathF64::degToRad(y) / 2.0)))));
41+
}
42+
double toMercatorDegY() const {
43+
return static_cast<double>(paxs::MathF64::radToDeg(toMercatorRadY()));
44+
}
45+
operator MercatorDeg() const {
46+
return MercatorDeg(paxs::Vector2<double>(x, toMercatorDegY()));
47+
}
48+
};
49+
MercatorDeg::operator EquirectangularDeg() const {
50+
return EquirectangularDeg(paxs::Vector2<double>(x, toEquirectangularDegY()));
51+
}
52+
53+
// 仮実装
54+
template<typename T>
55+
class MapProjection {
56+
public:
57+
58+
// 正距円筒図法(ラジアン)からメルカトル図法へ変換(ラジアン)
59+
constexpr inline static T equirectangularRadYToMercatorRadY(const T value) {
60+
return static_cast<T>(std::asin(std::tanh(value)));
61+
}
62+
// 正距円筒図法(度)からメルカトル図法へ変換(ラジアン)
63+
constexpr inline static T equirectangularDegYToMercatorRadY(const T value) {
64+
return static_cast<T>(std::asin(std::tanh(paxs::Math<T>::degToRad(value))));
65+
}
66+
// 正距円筒図法(ラジアン)からメルカトル図法へ変換(度)
67+
constexpr inline static T equirectangularRadYToMercatorDegY(const T value) {
68+
return static_cast<T>(paxs::Math<T>::radToDeg(std::asin(std::tanh(value))));
69+
}
70+
// 正距円筒図法(度)からメルカトル図法へ変換(度)
71+
constexpr inline static T equirectangularDegYToMercatorDegY(const T value) {
72+
return static_cast<T>(paxs::Math<T>::radToDeg(std::asin(std::tanh(paxs::Math<T>::degToRad(value)))));
73+
}
74+
75+
};
76+
77+
using MapProjectionF32 = MapProjection<float>;
78+
using MapProjectionF64 = MapProjection<double>;
79+
80+
}
81+
82+
#endif // !PAX_SAPIENTICA_TYPE_MAP_PROJECTION_HPP

Library/PAX_SAPIENTICA/Siv3D/Calendar.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <PAX_SAPIENTICA/Type/Date.hpp>
2121
#include <PAX_SAPIENTICA/TouchManager.hpp>
2222
#include <PAX_SAPIENTICA/Math.hpp> // 数学定数
23+
#include <PAX_SAPIENTICA/MapProjection.hpp> // 地図投影法
2324

2425
namespace paxs {
2526

@@ -285,10 +286,9 @@ namespace paxs {
285286
const s3d::ScopedRenderStates2D sampler{ s3d::SamplerState::ClampLinear };
286287

287288
const double map_view_width = map_view->getWidth();
288-
//const double map_view_height = map_view->getHeight();
289-
const double map_view_center_x = map_view->getCenterX();
290-
const double map_view_center_y = map_view->getCenterY();
291-
const double map_view_center_lat = paxs::MathF64::radToDeg(std::asin(std::tanh(paxs::MathF64::degToRad(map_view_center_y))));
289+
const double map_view_center_lat =
290+
//paxs::MathF64::radToDeg(std::asin(std::tanh(paxs::MathF64::degToRad(map_view->getCenterY()))));
291+
map_view->getCoordinate().toEquirectangularDegY();
292292

293293
/*##########################################################################################
294294
暦関連
@@ -558,13 +558,13 @@ namespace paxs {
558558
if (menu_bar.getPulldown(MenuBarType::view).getIsItems(3)) {
559559
font[language](language_text.get()[map_view_center_x_str_index][language + 1 /* 言語位置調整 */]
560560
).draw(s3d::TextStyle::Outline(0, 0.6, s3d::Palette::White), s3d::Arg::topRight = s3d::Vec2(s3d::Scene::Width() - 160, debug_start_y), s3d::Palette::Black);
561-
font[language](s3d::ToString(map_view_center_x)
561+
font[language](s3d::ToString(map_view->getCenterX())
562562
).draw(s3d::TextStyle::Outline(0, 0.6, s3d::Palette::White), s3d::Vec2(s3d::Scene::Width() - 110, debug_start_y), s3d::Palette::Black);
563563
debug_start_y += debug_move_y;
564564
// マップ中心座標 Y
565565
font[language](language_text.get()[map_view_center_y_str_index][language + 1 /* 言語位置調整 */]
566566
).draw(s3d::TextStyle::Outline(0, 0.6, s3d::Palette::White), s3d::Arg::topRight = s3d::Vec2(s3d::Scene::Width() - 160, debug_start_y), s3d::Palette::Black);
567-
font[language](s3d::ToString(map_view_center_y)
567+
font[language](s3d::ToString(map_view->getCenterY())
568568
).draw(s3d::TextStyle::Outline(0, 0.6, s3d::Palette::White), s3d::Vec2(s3d::Scene::Width() - 110, debug_start_y), s3d::Palette::Black);
569569
debug_start_y += debug_move_y;
570570
font[language](language_text.get()[map_view_center_lat_str_index][language + 1 /* 言語位置調整 */]

Library/PAX_SAPIENTICA/Siv3D/Key.hpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
#include<new>
2121
#include<vector>
2222

23+
#include <PAX_SAPIENTICA/Type/Vector2.hpp>
2324
#include <PAX_SAPIENTICA/Siv3D/Init.hpp>
25+
#include <PAX_SAPIENTICA/MapProjection.hpp> // 地図投影法
2426

2527
namespace paxs {
2628

@@ -65,7 +67,7 @@ namespace paxs {
6567
public:
6668
double movement_size; // マップの移動量
6769

68-
Coordinate(double x, double y, double movement_size) : x(x), y(y), movement_size(movement_size) {
70+
Coordinate(const paxs::MercatorDeg& coordinate_, double movement_size) : coordinate(coordinate_), movement_size(movement_size) {
6971
increase_x_keys.resize(2);
7072
increase_x_keys[0].reset((BaseKey*)new(std::nothrow) Key(s3d::KeyA));
7173
increase_x_keys[1].reset((BaseKey*)new(std::nothrow) Key(s3d::KeyLeft));
@@ -83,20 +85,27 @@ namespace paxs {
8385
decrease_y_keys[1].reset((BaseKey*)new(std::nothrow) Key(s3d::KeyUp));
8486
}
8587
void update(const double width) {
86-
if (pressed(increase_x_keys)) increase_coordinate(x, width);
87-
if (pressed(decrease_x_keys)) decrease_coordinate(x, width);
88-
if (pressed(increase_y_keys)) increase_coordinate(y, width);
89-
if (pressed(decrease_y_keys)) decrease_coordinate(y, width);
88+
if (pressed(increase_x_keys)) increase_coordinate(coordinate.x, width);
89+
if (pressed(decrease_x_keys)) decrease_coordinate(coordinate.x, width);
90+
if (pressed(increase_y_keys)) increase_coordinate(coordinate.y, width);
91+
if (pressed(decrease_y_keys)) decrease_coordinate(coordinate.y, width);
9092
}
9193
double getX() const {
92-
return x;
94+
return coordinate.x;
9395
}
9496
double getY() const {
95-
return y;
97+
return coordinate.y;
9698
}
99+
100+
double toEquirectangularRadY() const {
101+
return coordinate.toEquirectangularRadY();
102+
}
103+
double toEquirectangularDegY() const {
104+
return coordinate.toEquirectangularDegY();
105+
}
106+
97107
private:
98-
double x; // マップ座標の中央X
99-
double y; // マップ座標の中央Y
108+
paxs::MercatorDeg coordinate; // マップ座標の中央
100109
std::vector<std::unique_ptr<BaseKey>> increase_x_keys;
101110
std::vector<std::unique_ptr<BaseKey>> decrease_x_keys;
102111
std::vector<std::unique_ptr<BaseKey>> increase_y_keys;
@@ -118,7 +127,10 @@ namespace paxs {
118127

119128
class MapView {
120129
private:
121-
Coordinate center = Coordinate(135.0, getLatitudeToMercatorY(35.0), 200.0); // マップ座標の中央
130+
Coordinate center = Coordinate(
131+
paxs::EquirectangularDeg(paxs::Vector2<double>(135.0, 35.0)),
132+
//paxs::Vector2(135.0, getLatitudeToMercatorY(35.0)),
133+
200.0); // マップ座標の中央
122134
double width = 2.0; // マップの幅
123135

124136
// 平城京
@@ -169,6 +181,9 @@ namespace paxs {
169181
height = height_;
170182
}
171183

184+
Coordinate& getCoordinate() {
185+
return center;
186+
}
172187
double getCenterX() const {
173188
return center.getX();
174189
}

0 commit comments

Comments
 (0)