using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.TextFormatting;
using Microsoft.Research.DynamicDataDisplay.Common;
namespace Microsoft.Research.DynamicDataDisplay
{
public static class DataRectExtensions
{
internal static bool IsNaN( this DataRect rect )
{
return !rect.IsEmpty &&
(
rect.XMin.IsNaN() ||
rect.YMin.IsNaN() ||
rect.XMax.IsNaN() ||
rect.YMax.IsNaN()
);
}
///
/// Gets the center of specified rectangle.
///
/// The rect.
///
public static Point GetCenter( this DataRect rect )
{
return new Point( rect.XMin + rect.Width * 0.5, rect.YMin + rect.Height * 0.5 );
}
public static DataRect Zoom( this DataRect rect, Point to, double ratio )
{
return CoordinateUtilities.RectZoom( rect, to, ratio );
}
///
/// Zooms out from center.
///
/// The rect.
/// The ratio.
///
public static DataRect ZoomOutFromCenter( this DataRect rect, double ratio )
{
return CoordinateUtilities.RectZoom( rect, rect.GetCenter(), ratio );
}
///
/// Zooms in to center.
///
/// The rect.
/// The ratio.
///
public static DataRect ZoomInToCenter( this DataRect rect, double ratio )
{
return CoordinateUtilities.RectZoom( rect, rect.GetCenter(), 1 / ratio );
}
public static DataRect ZoomX( this DataRect rect, Point to, double ratio )
{
return CoordinateUtilities.RectZoomX( rect, to, ratio );
}
public static DataRect ZoomY( this DataRect rect, Point to, double ratio )
{
return CoordinateUtilities.RectZoomY( rect, to, ratio );
}
///
/// Gets the square of specified DataRect.
///
/// The rect.
///
public static double GetSquare( this DataRect rect )
{
if ( rect.IsEmpty )
return 0;
return rect.Width * rect.Height;
}
///
/// Determines whether one DataRect is close to another DataRect.
///
/// The rect1.
/// The rect2.
/// The difference.
///
/// true if [is close to] [the specified rect1]; otherwise, false.
///
public static bool IsCloseTo( this DataRect rect1, DataRect rect2, double difference )
{
DataRect intersection = DataRect.Intersect( rect1, rect2 );
double square1 = rect1.GetSquare();
double square2 = rect2.GetSquare();
double intersectionSquare = intersection.GetSquare();
bool areClose = MathHelper.AreClose( square1, intersectionSquare, difference ) &&
MathHelper.AreClose( square2, intersectionSquare, difference );
return areClose;
}
public static DataRect WithX( this DataRect rect, double xmin, double xmax )
{
return DataRect.Create( xmin, rect.YMin, xmax, rect.YMax );
}
public static DataRect WithY( this DataRect rect, double ymin, double ymax )
{
return DataRect.Create( rect.XMin, ymin, rect.XMax, ymax );
}
}
}