using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using DataPerformer;
namespace DataPerformerUI.Performers
{
/// <summary>
/// Performer of operations with 2D table
/// </summary>
public class Table2DPerformer
{
/// <summary>
/// Draws two parameter table
/// </summary>
/// <param name="t">The table to draw</param>
/// <param name="g">Graphics to draw</param>
/// <param name="x">Left corner coordinate</param>
/// <param name="y">Top corner coordinate</param>
/// <param name="w">Width</param>
/// <param name="h">Height</param>
public static void Draw(Table2D t, Graphics g, int x, int y, int w, int h)
{
if (t == null)
{
return;
}
double[,] b = t.Bounds;
double[] e = t.Extremums;
int xc = t.XCount;
int yc = t.YCount;
double ww = w;
double hh = h;
bool bx = ww > hh;
double coeff = 1 / (1 + Math.Sqrt(0.5));
double delta = (bx ? ww : hh) * coeff;
double w1 = w - delta;
double h1 = h - delta;
double vScale = 0;
if (e[1] != e[0])
{
vScale = h1 / (e[1] - e[0]);
}
double yScale = 0;
if (b[1, 1] != b[1, 0])
{
yScale = delta / (b[1, 1] - b[1, 0]);
}
double xScale = 0;
if (t.XCount > 1)
{
if (b[0, 1] != b[0, 0])
{
xScale = w1 / ((2 * Math.Sqrt(0.5)) * (b[0, 1] - b[0, 0]));
}
}
Pen pen = new Pen(Color.Magenta);
double x0 = x + w1;
double y0 = y + h1;
for (int i = 0; i < t.XCount; i++)
{
double xx = t.GetX(i);
double dx = xx - b[0, 0];
double x1 = x0 - dx * xScale;
double y1 = y0 + dx * xScale;
float xOld = 0;
float yOld = 0;
for (int j = 0; j < t.YCount; j++)
{
double v = t[i, j];
double vc = v - e[0];
vc = -vc * vScale;
vc += y1;
double yf = t.GetY(j);
yf = (yf - b[1, 0]) * yScale;
yf += x1;
float xd = (float)yf;
float yd = (float)vc;
if (j > 0)
{
g.DrawLine(pen, xOld, yOld, xd, yd);
}
xOld = xd;
yOld = yd;
}
}
}
}
}