Repository navigation
MillimetersChart
roosit edited this page Oct 16, 2026
·
2 revisions
Up | Previous | Next | Русский
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not stored in the working file MillimetersChart.java: the production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.official.charts;
import org.abricos.geo.GeoUtils;
/*
* Conversion "millimeters -> pixels" for charts around the current point.
* The window is built around the center (centerMmX, centerMmY), the scale is set by the map level.
*/
public class MillimetersChart {
/* Size of the area in pixels. */
private final int mWidth;
private final int mHeight;
/* Map zoom level that determines the number of millimeters per pixel. */
private final int mGeoZoom;
/* Millimeters per pixel at 1:1 scale (for the equator). */
private final long mMmPerPixel;
/* Window center in millimeters along X (distance) and along Y (elevation). */
private final long mCenterMmX;
private final long mCenterMmY;
/* Window size in millimeters. */
private final long mMmWidth;
private final long mMmHeight;
/* Window bounds along X in millimeters. */
private final long mMinMmX;
private final long mMaxMmX;
/* Window bounds along Y in millimeters. */
private final long mMinMmY;
private final long mMaxMmY;
/* "Pixels per millimeter" coefficients. */
private final double mKoefX;
private final double mKoefY;
/* Scale multipliers per axis: how many times the axis is stretched relative to 1:1. */
private final int mScaleX;
private final int mScaleY;
/*
* The latitude parameter is accepted but not used in the calculation:
* millimeters per pixel are taken for the equator.
*/
public MillimetersChart(
int width, int height,
int geoZoom, double latitude,
long centerMmX, long centerMmY,
int scaleX, int scaleY
) {
mWidth = width;
mHeight = height;
mGeoZoom = geoZoom;
mCenterMmX = centerMmX;
mCenterMmY = centerMmY;
/* Millimeters per pixel from the zoom level; the fractional part is discarded. */
mMmPerPixel = ((Double) (GeoUtils.calcMetersPerPixel(geoZoom) * 1000d)).longValue();
/* A zero multiplier is replaced with 1. */
mScaleX = scaleX != 0 ? scaleX : 1;
mScaleY = scaleY != 0 ? scaleY : 1;
/* Window width in mm: pixels * mm per pixel / X multiplier; the middle of the window is the center. */
mMmWidth = mWidth * (long) ((double) mMmPerPixel / (double) mScaleX);
mMinMmX = centerMmX - mMmWidth / 2;
mMaxMmX = mMinMmX + mMmWidth;
/* The same for Y. */
mMmHeight = mHeight * (long) ((double) mMmPerPixel / (double) mScaleY);
mMinMmY = centerMmY - mMmHeight / 2;
mMaxMmY = mMinMmY + mMmHeight;
/* Pixels per millimeter. */
mKoefX = (double) mWidth / (double) mMmWidth;
mKoefY = (double) mHeight / (double) mMmHeight;
}
public int getGeoZoom() {
return mGeoZoom;
}
public int getWidth() {
return mWidth;
}
public int getHeight() {
return mHeight;
}
public long getMmPerPixel() {
return mMmPerPixel;
}
public long getMinMmX() {
return mMinMmX;
}
public long getMaxMmX() {
return mMaxMmX;
}
public long getMmWidth() {
return mMmWidth;
}
public long getMinMmY() {
return mMinMmY;
}
public long getMaxMmY() {
return mMaxMmY;
}
public long getMmHeight() {
return mMmHeight;
}
/* X in pixels: the left edge of the window is 0. */
public int convertMmToPxX(long mm) {
return (int) (mKoefX * (mm - mMinMmX));
}
/* Y in pixels with inversion: a greater elevation gives a smaller Y (higher on the screen). */
public int convertMmToPxY(long mm) {
return (int) (mHeight - mKoefY * (mm - mMinMmY));
}
}MillimetersChart is an auxiliary coordinate system for the charts of SlopeChartIndicator and SpeedChartIndicator. It maps values in millimeters (distance along X, elevation along Y) to layer pixels so that the current point is in the center of the window.
- The default scale (1:1) is the number of millimeters in one pixel of a Web Mercator tile at the
geoZoomlevel:GeoUtils.calcMetersPerPixel(geoZoom) * 1000, with the fractional part discarded. For some levels:
geoZoom |
8 | 10 | 12 | 14 | 16 | 19 |
|---|---|---|---|---|---|---|
| mm per pixel | 611 496 | 152 874 | 38 218 | 9 554 | 2 388 | 298 |
- The multipliers
scaleXandscaleYstretch the axes: the window along an axis coverssize_in_pixels * (long)(mmPerPixel / scale)millimeters, so withscaleY = 10one pixel of height corresponds to a tenth ofmmPerPixel. A zero multiplier is replaced with 1. - The window:
minMmX = centerMmX - mmWidth / 2,maxMmX = minMmX + mmWidth; the same for Y. -
convertMmToPxX(mm)returns(int)(koefX * (mm - minMmX)), wherekoefX = width / mmWidth. -
convertMmToPxY(mm)returns(int)(height - koefY * (mm - minMmY)): the Y axis is inverted, a greater elevation is higher on the screen. - The getters (
getMinMmX(),getMaxMmX(),getMmPerPixel(), etc.) are needed by the indicators to calculate the reserve zone for the data request.
-
The
latitudeparameter is not used. The constructor accepts the latitude but callsGeoUtils.calcMetersPerPixel(geoZoom)without it, that is, it computes for the equator. Actual behavior: the scale depends only ongeoZoom, and no latitude correction is applied. Both indicators passGeoUtils.LAT_EQUATOR, so this does not affect the result. - Values outside the window are not clipped: coordinates may go beyond the layer bounds, and clipping is left to
Graphics2D. - A zero window width or height leads to division by zero (
koefbecomes infinity orNaN); the indicators call the class with layer sizes. - Rounding
mmPerPixeland(long)(mmPerPixel / scale)to whole millimeters makes the scale slightly inaccurate at largegeoZoomvalues and large multipliers. - The class does not store track data: only the window geometry.