1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-25 04:05:53 +01:00
GPXSee/src/map/rectd.cpp

87 lines
1.9 KiB
C++
Raw Normal View History

2020-12-12 10:19:48 +01:00
#include <cmath>
#include "common/rectc.h"
#include "projection.h"
#include "rectd.h"
static void growRect(const Projection &proj, const Coordinates &c, RectD &rect)
{
PointD p(proj.ll2xy(c));
if (p.x() < rect.left())
rect.setLeft(p.x());
if (p.x() > rect.right())
rect.setRight(p.x());
if (p.y() > rect.top())
rect.setTop(p.y());
if (p.y() < rect.bottom())
rect.setBottom(p.y());
}
static void growRect(const Projection &proj, const PointD &p, RectC &rect)
{
2019-05-10 18:56:19 +02:00
Coordinates c(proj.xy2ll(p));
if (c.lon() < rect.left())
rect.setLeft(c.lon());
if (c.lon() > rect.right())
rect.setRight(c.lon());
if (c.lat() > rect.top())
rect.setTop(c.lat());
if (c.lat() < rect.bottom())
rect.setBottom(c.lat());
2019-05-10 18:56:19 +02:00
}
RectD::RectD(const RectC &rect, const Projection &proj, int samples)
{
if (!rect.isValid())
return;
double dx = rect.width() / samples;
double dy = rect.height() / samples;
2020-12-12 10:19:48 +01:00
PointD tl(proj.ll2xy(rect.topLeft()));
PointD br(proj.ll2xy(rect.bottomRight()));
RectD prect(tl, br);
for (int i = 0; i <= samples; i++) {
double x = remainder(rect.left() + i * dx, 360.0);
growRect(proj, Coordinates(x, rect.bottom()), prect);
growRect(proj, Coordinates(x, rect.top()), prect);
}
2020-12-12 10:19:48 +01:00
for (int i = 0; i <= samples; i++) {
double y = rect.bottom() + i * dy;
growRect(proj, Coordinates(rect.left(), y), prect);
growRect(proj, Coordinates(rect.right(), y), prect);
}
*this = prect;
}
2019-05-10 18:56:19 +02:00
RectC RectD::toRectC(const Projection &proj, int samples) const
{
2020-12-12 10:19:48 +01:00
if (!isValid())
return RectC();
2019-05-10 18:56:19 +02:00
2020-12-12 10:19:48 +01:00
double dx = width() / samples;
double dy = height() / samples;
2019-05-10 18:56:19 +02:00
Coordinates c(proj.xy2ll(center()));
RectC rect(c, c);
2020-12-12 10:19:48 +01:00
for (int i = 0; i <= samples; i++) {
double x = left() + i * dx;
growRect(proj, PointD(x, bottom()), rect);
growRect(proj, PointD(x, top()), rect);
2019-05-10 18:56:19 +02:00
}
2020-12-12 10:19:48 +01:00
for (int i = 0; i <= samples; i++) {
2020-12-12 10:19:48 +01:00
double y = bottom() + i * dy;
growRect(proj, PointD(left(), y), rect);
growRect(proj, PointD(right(), y), rect);
2019-05-10 18:56:19 +02:00
}
2020-12-12 10:19:48 +01:00
return rect;
2019-05-10 18:56:19 +02:00
}