Post: [C#] Some Silly Shape Stuff
06-20-2011, 06:58 PM #1
kiwimoosical
Bounty hunter
(adsbygoogle = window.adsbygoogle || []).push({}); Vector2Sad Awesome
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Math2D
{
public class Vector2D : ICloneable
{
public Vector2D(double x, double y)
{
vectors.Add(0, x);
vectors.Add(1, y);
}

private Dictionary<int, double> vectors = new Dictionary<int, double>(); //Store the 2 coords.

public object Clone()
{
return new Vector2D(this[0], this[1]);
}

public double this[int c] //Indexing
{
get
{
if (c == 0 || c == 1)
return vectors[c];
else
throw new IndexOutOfRangeException();
}

set
{
if (c == 0 || c == 1)
vectors[c] = value;
else
throw new IndexOutOfRangeException();
}
}
}
}


Line:
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Math3D
{
public class Line : ICloneable
{
private Vector2D pointOne;
private Vector2D pointTwo;

public double X1
{
get { return pointOne[0]; }
set { pointOne[0] = value; }
}

public double X2
{
get { return pointTwo[0]; }
set { pointTwo[0] = value; }
}

public double Y1
{
get { return pointOne[1]; }
set { pointOne[1] = value; }
}

public double Y2
{
get { return pointTwo[1]; }
set { pointTwo[1] = value; }
}

public double Length
{
get
{
double xl = Math.Pow(pointTwo[0] - pointOne[0], 2);
double yl = Math.Pow(pointTwo[1] - pointOne[1], 2) ;
double results = xl + yl;
return Math.Sqrt(results);
}
}

public Line(Vector2D p1, Vector2D p2)
{
pointOne = p1;
pointTwo = p2;
}

public static bool IsPerpendicular(Line a, Line b)
{
return (a.Y2 - a.Y1) / (a.X2 - a.X1)
==
-1 * (1 / ((b.Y2 - b.Y1) / (b.X2 - b.X1)));
}

public static bool IsParallel(Line a, Line b)
{
return (a.Y2 - a.Y1) / (a.X2 - a.X2)
==
(b.Y2 - b.Y1) / (b.X2 - b.X1);
}

public object Clone()
{
Line line = new Line(pointOne, pointTwo);
return (object)line;
}
}
}


Square:
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Math3D
{
public class Square
{
private Vector2D p1;
private Vector2D p2;
private Vector2D p3;
private Vector2D p4;
private List<Line> lines = new List<Line>();

public Line this[int index]
{
get
{
if (index <= 3 && index >= 0)
return lines[index];
else
throw new IndexOutOfRangeException();
}
}

public Square(Vector2D p1, Vector2D p2, Vector2D p3, Vector2D p4)
{
Line[] Lines = new Line[]
{
new Line(p1,p2),
new Line(p2,p3),
new Line(p3,p4),
new Line(p4,p1)
};

for (int x = 0; x < 2; x++)
{
lines.Add(Lines[x]);
lines.Add(Lines[x + 2]);

if (!Line.IsParallel(Lines[x], Lines[x + 2]))
throw new NotASquareException("The lines do not form 90 degree angles.");
}

if (Lines[0].Length != Lines[1].Length || Lines[0].Length != Lines[2].Length || Lines[0].Length != Lines[3].Length)
throw new NotASquareException("The length of the lines are not equal.");
if(Lines[1].Length != Lines[2].Length ||Lines[1].Length != Lines[3].Length)
throw new NotASquareException("The length of the lines are not equal.");
if(Lines[2].Length != Lines[3].Length)
throw new NotASquareException("The length of the lines are not equal.");

this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
}
}
}


NotASquareException:
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Math3D
{
public class NotASquareException : Exception
{
public NotASquareException() : base() { }
public NotASquareException(string msg) : base(msg) { }
}
}


Do with it what you want :P

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo