Figure Class

public enum FigureType // FigureType 판별
{
Triangle,
EquilateralTriangle,
IsoscelesTriangle,
ScaleneTriangle,
Quadrilateral,
Square,
Rectangle,
Parallelogram,
Rhombus,
Trapezoid,
None
}
public class FigureTypeResolver : JavaScriptTypeResolver // JSON serialization & deserialization
{
public override Type ResolveType(string id)
{
return Type.GetType(id);
}

public override string ResolveTypeId(Type type)
{
if (type == null)
{
throw new ArgumentNullException(“type”);
}

return type.FullName;
}
}

public class FigureBound // TopLeft Point(X,Y) & Width & Height
{
public int X
{
set;
get;
}

public int Y
{
set;
get;
}

public int Width
{
set;
get;
}

public int Height
{
set;
get;
}

public FigureBound() : this (0, 0, 10, 10)
{
}

public FigureBound(int x, int y, int width, int height)
{
Set(x, y, width, height);
}

public void Set(int x, int y, int width, int height)
{
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
}
public override string ToString() { return (String.Format(“({0}, {1}, {2}, {3})”, X, Y, Width, Height)); } // ToString method override
}

public abstract class Figure
{
public ConsoleColor Color
{
get;
set;
}

protected List<Point> vertices = null;
public List<Point> Vertices // Use Point.cs (Not System.Drawing.Point) for JSON
{
get
{
return vertices;
}
set
{
vertices = value;
UpdateBounds(); // 새 정점에 따른 바운딩박스 계산
UpdateSides(); // 새 정점에 따른 변들 계산
UpdateAngles(); // 새 정점에 따른 내각들 계산
}
}

public FigureBound Bounds = new FigureBound(); // 바운딩박스

public List<double> Sides // 변
{
get;
set;
}

public List<double> Angles // 각도 (radian)
{
get;
set;
}

public abstract void UpdateBounds();
public abstract void UpdateSides();
public abstract void UpdateAngles();

public override string ToString()
{
string listOfVertices = null;
string listOfSides = null;
string listOfAngles = null;
if (Vertices != null)
{
//listOfVertices = string.Join(” , “, Vertices.ToArray()); // .NET4
listOfVertices = string.Join(” , “, Array.ConvertAll(Vertices.ToArray(), x => x.ToString())); // ealier than .NET4
listOfSides = string.Join(” , “, Array.ConvertAll(Sides.ToArray(), x => x.ToString())); // ealier than .NET4
listOfAngles = string.Join(” , “, Array.ConvertAll(Angles.ToArray(), x => x.ToString())); // ealier than .NET4
}
return String.Format(“{0} : {1} : {2} : {3} : {4} : {5}”, GetType().Name, Color, listOfVertices, Bounds, listOfAngles, listOfSides);
} // ToString method override

public void Draw(Graphics g)
{
Brush b = new SolidBrush(System.Drawing.Color.FromName(this.Color.ToString()));
System.Drawing.Point[] points = Array.ConvertAll(Vertices.ToArray(), x => (System.Drawing.Point)x); // Use with Point.cs type conversion operator overload
g.FillPolygon(b, points);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *