Files
Sean 8715bed4cf
Some checks failed
Build .NET Project / build (push) Has been cancelled
Fix ImageHelper not rendering images due to issue in PointMapping.
2026-03-10 23:48:25 -04:00

85 lines
3.5 KiB
C#
Executable File

using SkiaSharp;
namespace MarketData.CNNProcessing
{
/// <summary>
/// This class provides functionality to both scale and map data points from an upper left coordinate system to a lower left coordinate system
/// </summary>
public class PointMapping
{
public PointMapping(double width, double height, double xDataExtent, double xDataExtentMin, double yDataExtent, double yDataExtentMin, double xMarginExtent = 0.00, double yMarginExtent = 0.00)
{
Width = width;
Height = height;
XMargin = xMarginExtent;
YMargin = yMarginExtent;
XDataExtent = xDataExtent;
XDataExtentMin = xDataExtentMin;
YDataExtent = yDataExtent;
YDataExtentMin = yDataExtentMin;
XScalingFactor = (Width - (XMargin * 2.00)) / (XDataExtent - XDataExtentMin);
YScalingFactor = (Height - (YMargin * 2.00)) / (YDataExtent - YDataExtentMin);
}
public PointMapping(PointMapping pointMapping)
{
Width = pointMapping.Width;
Height = pointMapping.Height;
XMargin = pointMapping.XMargin;
YMargin = pointMapping.YMargin;
XDataExtent = pointMapping.XDataExtent;
XDataExtentMin = pointMapping.XDataExtentMin;
YDataExtent = pointMapping.YDataExtent;
YDataExtentMin = pointMapping.YDataExtentMin;
XScalingFactor = pointMapping.XScalingFactor;
YScalingFactor = pointMapping.YScalingFactor;
}
/// <summary>
/// MapPoint will both scale the given point and translate the given point from an upper left origin system to a bottom left origin system
/// Because of the scaling factor (XScalingFactor and YScalingFactor) it is possible for the resulting X,Y values to be rounded out of range.
/// To remedy this we perform a boundary check of the X,Y points against the extents and set them to those extents if they are out of range.
/// </summary>
/// <param name="sourcePoint"></param>
/// <returns></returns>
public SKPoint MapPoint(SKPoint sourcePoint)
{
SKPoint mappedPoint = new SKPoint(
(float)((sourcePoint.X - XDataExtentMin) * XScalingFactor),
(float)((Height - 1) - ((sourcePoint.Y - YDataExtentMin) * YScalingFactor))
);
mappedPoint.X += (float)XMargin;
mappedPoint.Y -= (float)YMargin;
// Clamp to image bounds
if (mappedPoint.X > Width - 1) mappedPoint.X = (float)(Width - 1);
if (mappedPoint.Y > Height - 1) mappedPoint.Y = (float)(Height - 1);
if (mappedPoint.X < 0) mappedPoint.X = 0;
if (mappedPoint.Y < 0) mappedPoint.Y = 0;
return mappedPoint;
}
// TranslatePoint will only translate the given point from an upper left origin system to a bottom left origin system
public SKPoint TranslatePoint(SKPoint sourcePoint)
{
SKPoint mappedPoint=new SKPoint((int)sourcePoint.X,(int)(Height-sourcePoint.Y-1));
return mappedPoint;
}
public double Width { get; private set; }
public double Height{get;private set;}
public double XDataExtent{get;private set;}
public double XDataExtentMin{get;private set;}
public double XRange{get{return XDataExtent-XDataExtentMin;}}
public double YDataExtent{get;private set;}
public double YDataExtentMin{get;private set;}
public double YRange{get{return YDataExtent-YDataExtentMin;}}
public double XMargin{get;private set;}
public double YMargin{get;private set;}
public double XScalingFactor{get;private set;}
public double YScalingFactor{get;private set;}
}
}