我需要一种方法将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));
}
答案 0 :(得分:7)
下面的代码应该会给你一些想法。
首先需要引入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();
}
结果:
答案 1 :(得分:2)
真正的错误是具有非常长的单线功能。这使得它很难阅读,即使是像你这样聪明的人也会引起问题。
IsHit()是一个相当复杂的函数,所以将它展开......
您需要缩放坐标,以便它们适合控制台窗口。
我假设窗口坐标是(1,1) - (consoleWidth,consoleHeight)
private static bool IsConsoleHit(Dictionary<int, int> dict,
int consoleWidth,
int consoleHeight,
int hx,
int hy)
{
int minX = dict.Min(x => x.Key);
int maxX = dict.Max(x => x.Key);
int minY = dict.Min(x => x.Value);
int maxY = dict.Max(x => x.Value);
foreach (KeyValuePair<int, int> pair in dict)
{
// (x,y) starting at (0,0)
int x = pair.Key - minX;
int y = pair.Value - minY;
// convert to (0..1.0, 0..1.0)
double dx = x / Math.Max(maxX - minX, 1.0);
double dy = y / Math.Max(maxY - minY, 1.0);
// Scale to (1,1) upto (consoleWidth, consoleHeight)
int sx = (int)(dx * (consoleWidth - 1)) + 1;
int sy = (int)(dy * (consoleHeight - 1)) + 1;
if (hx == sx && hy == sy)
{
return true; // Its a hit
}
}
return false;
}
然后用它制作一个lambda函数:
Func<int, int, bool> IsHit = ((hx, hy) => IsConsoleHit(dict, consoleWidth, consoleHeight, hx, hy);
所以将来,我永远不会使用单行函数来计算事物。 使用常规方法,为您提供展开和工作的空间。
答案 2 :(得分:1)
至少我没有解决问题的答案,但有一些提示可能会导致麻烦:
IsHit()
方法接收左上角(开始绘制的地方)坐标0/0,但它应该是0/19,最后一个元素是77/19,但它应该是77/0。 / LI>
也许您应该编写一个翻译方法,它将从光标位置转换为x / y值,然后针对翻译位置进行测试,而不是尝试在一个步骤中同时执行这两个操作。我认为通过使用这种方法,您将能够解决自己的问题。