151
Computer Projects and Ideas / Re: Random Script Snippets
« on: May 10, 2013, 01:26:08 pm »
Here is something I did in my spare time in C#. It's not really a snippit, but fun anyway^^
Main.cs - Main program code
Turtle.cs - Turtle 'engine'
Display.cs - Allows you to draw to your console
Now, here is what it looks like:
Of course, I set my terminal to have very very many rows and columns so I can simulate pixels. (however you can change the code to work on a 'lower' resolution)
Main.cs - Main program code
Code: [Select]
using System;
namespace Fractal
{
class MainClass
{
public static void TriLine(double distance, Turtle myTurtle)
{
Console.ForegroundColor = ConsoleColor.Magenta;
if (distance < 1)
{
myTurtle.Forward(distance);
}
else
{
TriLine (distance/3, myTurtle);
myTurtle.Left (60);
TriLine (distance/3, myTurtle);
myTurtle.Right (120);
TriLine (distance/3, myTurtle);
myTurtle.Left (60);
TriLine (distance/3, myTurtle);
}
}
public static void Main (string[] args)
{
Console.WriteLine ("Press any key to start");
Console.ReadLine ();
Display myDisplay = new Display ();
Turtle myTurtle = new Turtle (myDisplay);
//myTurtle.HomeXPos = 10;
myTurtle.HomeYPos = 10;
for (int i = 0; i < 3; i++) {
TriLine(300, myTurtle);
myTurtle.Right(120);
}
myDisplay.UpdateDisplay ();
}
}
}
Turtle.cs - Turtle 'engine'
Code: [Select]
using System;
namespace Fractal
{
public class Turtle
{
public double XPos {get; set;}
public double YPos {get; set;}
public double Angle {get; set;}
public bool Pen {get; set;}
public double HomeXPos {get; set;}
public double HomeYPos {get; set;}
private Display myDisplay;
public Turtle(Display myDisplay)
{
XPos = 0;
YPos = 0;
Angle = 0;
Pen = true;
this.myDisplay = myDisplay;
HomeXPos = myDisplay.Width / 2;
HomeYPos = myDisplay.Height / 2;
}
public static double DegToRad(double deg)
{
return deg * Math.PI / 180;
}
public void Forward(double distance)
{
double oldX = XPos;
double oldY = YPos;
double angle = DegToRad(this.Angle);
YPos = oldY + distance * Math.Cos(angle);
XPos = oldX + distance * Math.Sin(angle);
if (Pen)
{
myDisplay.DrawLine (
(int) Math.Round(HomeYPos + oldY),
(int) Math.Round(HomeXPos + oldX),
(int) Math.Round(HomeYPos + YPos),
(int) Math.Round(HomeXPos + XPos)
);
//m
}
}
public void Left(double angleDelta)
{
Angle -= angleDelta;
}
public void Right(double angleDelta)
{
Angle += angleDelta;
}
public override string ToString ()
{
return string.Format ("[Turtle: XPos={0}, YPos={1}, Angle={2}]", XPos, YPos, Angle);
}
}
}
Display.cs - Allows you to draw to your console
Code: [Select]
using System;
namespace Fractal
{
public class Display
{
public const string PIXEL_ON = "\u2588\u2588";
public const string PIXEL_OFF = " ";
public const int PIXEL_CHARS = 2;
public int Height { get; set; }
public int Width { get; set; }
public bool[,] DisplayBuffer;
public Display()
{
SetDisplay ();
}
public void SetDisplay ()
{
Width = Console.BufferWidth / PIXEL_CHARS;
Height = Console.BufferHeight - 2;
DisplayBuffer = new bool[Height, Width];
for (int i = 0; i < DisplayBuffer.Length; i++) {
DisplayBuffer [(int) i/Width, i%Width] = false;
}
}
public void SetPixel (int y, int x, bool state)
{
if (x > 0 && y > 0 && x < Width && y < Height)
DisplayBuffer [y, x] = state;
}
public void DrawLine(int x1, int y1, int x2, int y2) {
int w = x2 - x1;
int h = y2 - y1;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
int longest = Math.Abs(w) ;
int shortest = Math.Abs(h) ;
if (!(longest>shortest)) {
longest = Math.Abs(h) ;
shortest = Math.Abs(w) ;
if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
dx2 = 0 ;
}
int numerator = longest >> 1;
for (int i=0; i<=longest; i++) {
SetPixel(x1 , y1, true) ;
numerator += shortest ;
if (!(numerator<longest)) {
numerator -= longest ;
x1 += dx1 ;
y1 += dy1 ;
} else {
x1 += dx2 ;
y1 += dy2 ;
}
}
}
public void UpdateDisplay()
{
Console.SetCursorPosition (0, 0);
for (int y = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
string pixel = DisplayBuffer[y, x] ? PIXEL_ON : PIXEL_OFF;
Console.Write (pixel);
}
Console.WriteLine ();
}
}
}
}
Now, here is what it looks like:
Of course, I set my terminal to have very very many rows and columns so I can simulate pixels. (however you can change the code to work on a 'lower' resolution)