c# – 控制台图表

c# – 控制台图表,第1张

概述我需要一种绘制字典< int,int>进入控制台应用程序 Dictionary<int, int> chartList = new Dictionary<int, int>(){ {50,31}, // x = 50, y = 31 {71,87}, {25,66}, {94,15}, {33,94}};Draw 我需要一种绘制字典< 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方法,它决定了当前的坐标是否应该设置一个点.有人可以帮我吗?它始终是真的.

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,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) + '┘');        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),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# – 控制台图表全部内容,希望文章能够帮你解决c# – 控制台图表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1258555.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存