我需要一种方法来绘制 Dictionary<int,int>
进入控制台应用程序,例如
Dictionary<int, int> chartList = new Dictionary<int, int>()
{
{50,31}, // x = 50, y = 31
{71,87},
{25,66},
{94,15},
{33,94}
};
DrawChart(chartList);
应该会产生类似的结果
我已经走到这一步了,但我还停留在 IsHit
上方法,它确定是否应在当前坐标处设置一个点。在这一点上有人可以帮助我吗?它始终返回 true。
public static void DrawChart(Dictionary<int, int> dict)
{
int consoleWidth = 78;
int consoleHeight = 20;
Console.WriteLine(dict.Max(x => x.Key).ToString());
Func<int, int, bool> IsHit = (hx, hy) => dict.Any(dct => dct.Key / dict.Max(x => x.Key) == hx / dict.Max(x => x.Key) && dct.Value / dict.Max(x => x.Value) == hy / dict.Max(x => x.Value));
for (int i = 0; i < consoleHeight; i++)
{
Console.Write(i == 0 ? '┌' : '│');
for (int j = 0; j < consoleWidth; j++)
{
int actualheight = i * 2;
if (IsHit(j, actualheight) && IsHit(j, actualheight + 1))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write('█');
}
else if (IsHit(j, actualheight))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write('▀');
}
else if (IsHit(j, actualheight + 1))
{
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.Red;
Console.Write('▀');
}
}
Console.ResetColor();
Console.WriteLine();
}
Console.WriteLine('└' + new string('─', (consoleWidth / 2) - 1) + '┴' + new string('─', (consoleWidth / 2) - 1) + '┘');
Console.Write((dict.Min(x => x.Key) + "/" + dict.Min(x => x.Value)).PadRight(consoleWidth / 3));
Console.Write((dict.Max(x => x.Value) / 2).ToString().PadLeft(consoleWidth / 3 / 2).PadRight(consoleWidth / 3));
Console.WriteLine(dict.Max(x => x.Value).ToString().PadLeft(consoleWidth / 3));
}
最佳答案
下面的代码应该给你一些想法。
首先需要引入一个Point
,因为与Dictionary
一起工作,它是Key
和Value
属性,而不是像普通名称X
和Y
是一场噩梦。此外,在字典中您不能使用相同的 X
坐标存储多个点,这毫无意义。
public struct Point {
public Point(int x, int y) {
this.X = x;
this.Y = y;
}
public int X { get; }
public int Y { get; }
}
然后稍微修改一下DrawChart
:
public static void DrawChart(List<Point> dict)
{
int consoleWidth = 78;
int consoleHeight = 20;
int actualConsoleHeight = consoleHeight * 2;
var minX = dict.Min(c => c.X);
var minY = dict.Min(c => c.Y);
var maxX = dict.Max(c => c.X);
var maxY = dict.Max(c => c.Y);
Console.WriteLine(maxX);
// normalize points to new coordinates
var normalized = dict.
Select(c => new Point(c.X - minX, c.Y - minY)).
Select(c => new Point((int)Math.Round((float) (c.X) / (maxX - minX) * (consoleWidth - 1)), (int)Math.Round((float) (c.Y) / (maxY - minY) * (actualConsoleHeight - 1)))).ToArray();
Func<int, int, bool> IsHit = (hx, hy) => {
return normalized.Any(c => c.X == hx && c.Y == hy);
};
for (int y = actualConsoleHeight - 1; y > 0; y -= 2)
{
Console.Write(y == actualConsoleHeight - 1 ? '┌' : '│');
for (int x = 0; x < consoleWidth; x++)
{
bool hitTop = IsHit(x, y);
bool hitBottom = IsHit(x, y - 1);
if (hitBottom && hitTop)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write('█');
}
else if (hitTop)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write('▀');
}
else if (hitBottom)
{
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.Red;
Console.Write('▀');
}
else
{
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write('▀');
}
}
Console.ResetColor();
Console.WriteLine();
}
Console.WriteLine('└' + new string('─', (consoleWidth / 2) - 1) + '┴' + new string('─', (consoleWidth / 2) - 1) + '┘');
Console.Write((dict.Min(x => x.X) + "/" + dict.Min(x => x.Y)).PadRight(consoleWidth / 3));
Console.Write((dict.Max(x => x.Y) / 2).ToString().PadLeft(consoleWidth / 3 / 2).PadRight(consoleWidth / 3));
Console.WriteLine(dict.Max(x => x.Y).ToString().PadLeft(consoleWidth / 3));
}
和用法:
static void Main(string[] args) {
var chartList = new List<Point> {
new Point(50, 31), // x = 50, y = 31
new Point(71, 87),
new Point(71, 89),
new Point(25, 66),
new Point(94, 15),
new Point(33, 94)
};
DrawChart(chartList);
Console.ReadKey();
}
结果:
关于c# - 控制台图表绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38496889/