Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void GraphInput()
- {
- //kx+m
- Console.SetCursorPosition(0, 0);
- Console.ForegroundColor = ConsoleColor.Gray;
- string stringK = null;
- string stringM = null;
- Console.WriteLine("Förstagradsgrafritaren v1\n");
- Console.WriteLine("Mata in din graf (kx+m)");
- Console.Write("\ny = ");
- string graph = Console.ReadLine().ToLower();
- foreach (char c in graph)
- {
- if (c == 'x')
- {
- break;
- }
- stringK += c;
- }
- string check = null;
- foreach (char c in graph)
- {
- check += c;
- if (check.Contains('+') || check.Contains('-') && check.Contains(stringK) && check.Contains('x'))
- {
- stringM += c;
- }
- }
- //-1x+3
- string check2 = null;
- string stringM2 = null;
- if (stringM.Contains('x'))
- {
- foreach (char c in stringM)
- {
- check2 += c;
- if (check2.Contains('+') || check2.Contains('-'))
- {
- stringM2 += c;
- }
- }
- }
- int k = Int32.Parse(stringK);
- int m;
- if (stringM2 == null)
- {
- m = Int32.Parse(stringM);
- }
- else
- {
- m = Int32.Parse(stringM2);
- }
- CreateGraph(k, m);
- GraphMenu();
- }
- static void CreateGraph(int k, int m)
- {
- m = Dot.y0 - m;
- Dot skärY = new Dot(Dot.x0, m);
- Dot.graph[0] = skärY;
- int indexCounter = 1;
- //Alla punkter som har x > 0
- for (int i = 1; i < 50; i++)
- {
- Dot.graph[indexCounter] = new Dot(Dot.x0 + i, m - (k * i));
- indexCounter++;
- }
- //Alla punkter som har x < 0
- for (int i = 1; i < 51;i++)
- {
- Dot.graph[indexCounter] = new Dot(Dot.x0 - i, m + (k * i));
- indexCounter++;
- }
- GraphOutput();
- }
- static void GraphOutput()
- {
- for (int i = 0; i < Dot.graph.Length; i++)
- {
- if (Dot.graph[i] != null && Dot.graph[i].posY > 0 && Dot.graph[i].posY < Console.WindowHeight && Dot.graph[i].posX > 0 && Dot.graph[i].posX < Console.WindowWidth)
- {
- Dot.graph[i].PrintDot();
- }
- }
- }
- static void GraphMenu()
- {
- Console.ForegroundColor = ConsoleColor.Gray;
- PrintStringAt("1: Nytt koordinatsystem", 0, 6);
- PrintStringAt("2: Lägg till en graf i samma koordinatsystem", 0, 7);
- PrintStringAt("Val: ", 0, 9);
- string val = Console.ReadLine();
- switch (val)
- {
- case "1":
- Restart();
- break;
- case "2":
- ConsoleClearArea(0, 0, 25, 5);
- ConsoleClearArea(0, 6, 43, 9);
- Console.SetCursorPosition(0, 0);
- GraphInput();
- break;
- }
- }
- //Vilket x den ska börja ifrån, vilket x den ska sluta på och vilken y den ska börja på och vilken y den ska sluta på.
- static void ConsoleClearArea(int xStart, int yStart, int xEnd, int yEnd)
- {
- for (int i = yStart; i <= yEnd; i++)
- {
- for (int j = xStart; j <= xEnd; j++)
- {
- Console.SetCursorPosition(j, i);
- Console.Write(" ");
- }
- }
- }
- static void Restart()
- {
- Console.Clear();
- CoordinateSystem();
- GraphInput();
- }
- static void PrintStringAt(string text, int x, int y)
- {
- Console.SetCursorPosition(x, y);
- Console.Write(text);
- }
- static void CoordinateSystem()
- {
- //X-axeln
- for (int i = 0; i < Console.WindowWidth; i++)
- {
- Console.SetCursorPosition(i, Dot.y0);
- Console.Write("■");
- }
- //Y-axeln
- for (int i = 0; i < Console.WindowHeight; i++)
- {
- Console.SetCursorPosition(Dot.x0, i);
- Console.Write("■");
- }
- }
- }
- class Dot
- {
- public static int x0 = Console.WindowWidth / 2;
- public static int y0 = Console.WindowHeight / 2;
- public static Dot[] graph = new Dot[101];
- public int posX;
- public int posY;
- public Dot(int posX, int posY)
- {
- this.posX = posX;
- this.posY = posY;
- }
- public void PrintInfo()
- {
- Console.WriteLine("posX: {0}, posY: {1}", posX, posY);
- }
- public void PrintDot()
- {
- int randomInt = 0;
- Random rnd = new Random();
- ConsoleColor[] Colors = { ConsoleColor.Red, ConsoleColor.Yellow,ConsoleColor.Magenta,ConsoleColor.Green,ConsoleColor.DarkRed,ConsoleColor.DarkBlue,ConsoleColor.DarkGreen };
- randomInt = rnd.Next(0, 6);
- Console.ForegroundColor = Colors[randomInt];
- Console.SetCursorPosition(posX, posY);
- Console.Write("■");
- }
- }
- class Globals
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement