Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change MapProjector.ToMap() to avoid unnecessary memory alloc. #1272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/LiveChartsCore/Geo/ControlCoordinatesProjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ public ControlCoordinatesProjector(float mapWidth, float mapHeight, float offset
/// </value>
public static float[] PreferredRatio => new[] { 2f, 1f };

/// <inheritdoc cref="MapProjector.ToMap(double[])"/>
/// <inheritdoc cref="MapProjector.ToMap(LvcPointD)"/>
public override float[] ToMap(double[] point)
{
// simplified formula
return new[]
return new LvcPoint
{
// x' =
(float)(_ox + (point[0] + 180) / 360d * _w),
// x' =
X = (float)(_ox + (point.X + 180) / 360d * _w),

// y' =
(float)(_oy + (90 - point[1]) / 180d * _h)
};
// y' =
Y = (float)(_oy + (90 - point.Y) / 180d * _h)
};

// the following code explains the formula better:

Expand Down
2 changes: 1 addition & 1 deletion src/LiveChartsCore/Geo/MapProjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ public abstract class MapProjector
/// </summary>
/// <param name="point">The point.</param>
/// <returns></returns>
public abstract float[] ToMap(double[] point);
public abstract LvcPoint ToMap(LvcPointD point);
}
14 changes: 7 additions & 7 deletions src/LiveChartsCore/Geo/MercatorProjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ public MercatorProjector(float mapWidth, float mapHeight, float offsetX, float o
/// </value>
public static float[] PreferredRatio => new[] { 1f, 1f };

/// <inheritdoc cref="MapProjector.ToMap(double[])"/>
public override float[] ToMap(double[] point)
/// <inheritdoc cref="MapProjector.ToMap(LvcPointD)"/>
public override LvcPoint ToMap(LvcPointD point)
{
var lat = point[1];
var lon = point[0];
var lat = point.Y;
var lon = point.X;

var latRad = lat * Math.PI / 180d;
var mercN = Math.Log(Math.Tan(Math.PI / 4d + latRad / 2d), Math.E);
var y = _h / 2d - _h * mercN / (2 * Math.PI);

return new[]
return new LvcPoint
{
// x' =
(float)((lon + 180) * (_w / 360d) + _ox),
X = (float)((lon + 180) * (_w / 360d) + _ox),

// y' =
(float) y + _oy
Y = (float) y + _oy
};
}
}
6 changes: 3 additions & 3 deletions src/skiasharp/LiveChartsCore.SkiaSharp/MapFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public void GenerateLands(MapContext<SkiaSharpDrawingContext> context)

foreach (var point in landData.Coordinates)
{
var p = projector.ToMap(new double[] { point.X, point.Y });
var p = projector.ToMap(point);

var x = p[0];
var y = p[1];
var x = p.X;
var y = p.Y;

if (isFirst)
{
Expand Down