1.1.32 #
解答 #
绘图结果:
完整的项目代码可以去 Github 上下载。
代码 #
public static void StartDrawing(double[] array, int N, double l, double r)
{
//创建并显示绘图窗口
Form2 DrawPad = new Form2();
DrawPad.Show();
//新建画布
Graphics graphics = DrawPad.CreateGraphics();
//翻转默认坐标系
graphics.TranslateTransform(0, DrawPad.Height);
graphics.ScaleTransform(1, -1);
//对原始数组排序
Array.Sort(array);
//计算各区域的值
int[] counts = new int[N];
int index = 0;
for (int i = 0; i < N; ++i)
{
for (int j = index; j < array.Length; ++j)
{
if (array[j] <= (r - l) * (i + 1) / N)
{
counts[i]++;
index++;
}
else
{
break;
}
}
}
//获取最大值
double max = counts.Max();
//计算间距
double unit = DrawPad.Width / (3.0 * N + 1);
//计算直方图的矩形
Rectangle[] rects = new Rectangle[N];
rects[0].X = (int)unit;
rects[0].Y = 0;
rects[0].Width = (int)(2 * unit);
rects[0].Height = (int)((counts[0] / max) * DrawPad.Height);
for (int i = 1; i < N; ++i)
{
rects[i].X = (int)(rects[i - 1].X + 3 * unit);
rects[i].Y = 0;
rects[i].Width = (int)(2 * unit);
rects[i].Height = (int)((counts[i] / (max + 1)) * DrawPad.Height);
}
//绘图
graphics.FillRectangles(Brushes.Black, rects);
//释放资源
graphics.Dispose();
}