Private
Public Access
Implemented RegularPolygons
This commit is contained in:
1 parent
ae701dfb05
commit
5ea0f49bb1
2 files changed
+60
-14
No files matched your search
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
|
||||
namespace Advanced_Software_Engineering {
|
||||
class VerbFactory {
|
||||
@@ -9,7 +11,7 @@ namespace Advanced_Software_Engineering {
|
||||
Dictionary<string, string[]> commandAndParameters = SettingsAndHelperFunctions.CommandAndParameterParser(fullCommand);
|
||||
|
||||
string command = commandAndParameters["command"][0];
|
||||
string[] parameters = commandAndParameters["parameters"];
|
||||
string[] parameters = commandAndParameters.Keys.Contains("parameters") ? commandAndParameters["parameters"] : new string[] { };
|
||||
|
||||
switch (command) {
|
||||
case "moveto":
|
||||
@@ -41,14 +43,30 @@ namespace Advanced_Software_Engineering {
|
||||
throw e;
|
||||
}
|
||||
} else throw new Exception("Command has an incorrect number of parameters");
|
||||
|
||||
case "dot":
|
||||
case "clear":
|
||||
case "shape":
|
||||
//Check parameters
|
||||
if (parameters.Length == 2) {
|
||||
return new
|
||||
RegularPolygon(drawer,
|
||||
SettingsAndHelperFunctions.ConvertToInt(parameters[0]),
|
||||
SettingsAndHelperFunctions.ConvertToDouble(parameters[1])
|
||||
);
|
||||
}
|
||||
if (parameters.Length == 3) {
|
||||
return new RegularPolygon(drawer,
|
||||
SettingsAndHelperFunctions.ConvertToInt(parameters[0]),
|
||||
SettingsAndHelperFunctions.ConvertToDouble(parameters[1]),
|
||||
SettingsAndHelperFunctions.ConvertToDouble(parameters[2])
|
||||
);
|
||||
} else throw new Exception("Command has an incorrect number of parameters");
|
||||
case "square":
|
||||
case "rectangle":
|
||||
case "circle":
|
||||
case "triangle":
|
||||
|
||||
|
||||
case "dot":
|
||||
case "clear":
|
||||
case "pen":
|
||||
case "fill":
|
||||
case "color":
|
||||
@@ -138,23 +156,43 @@ namespace Advanced_Software_Engineering {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A class that generates sets
|
||||
/// A class that generates a RegularPolygon verb.
|
||||
/// </summary>
|
||||
class RegularPolygons : Verb {
|
||||
class RegularPolygon : Verb {
|
||||
protected Drawer drawer;
|
||||
protected int sides;
|
||||
protected double scale;
|
||||
protected double offset;
|
||||
List<Point> points;
|
||||
|
||||
List<Verb> verbs;
|
||||
public RegularPolygon(Drawer drawer, int sides, double scale, double offset = 0) {
|
||||
points = new List<Point>();
|
||||
|
||||
RegularPolygons(Drawer drawer, Point origin, int sides, int scale) {
|
||||
double constantAngleDelta = 2 * Math.PI / sides;
|
||||
for (int side = 0; side < sides; side++) {
|
||||
double angle = (constantAngleDelta * side) + offset;
|
||||
|
||||
double dx = scale * Math.Sin(angle);
|
||||
double dy = scale * Math.Cos(angle);
|
||||
|
||||
points.Add(new Point((int)dx, (int)dy));
|
||||
}
|
||||
|
||||
this.sides = sides;
|
||||
this.scale = scale;
|
||||
this.drawer = drawer;
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
public void ExecuteVerb() {
|
||||
|
||||
Point currentOrigin = drawer.GetPenPosition();
|
||||
List<Point> originShiftedPoints = new List<Point>();
|
||||
foreach (Point point in points) originShiftedPoints.Add(new Point(point.X + currentOrigin.X, point.Y + currentOrigin.Y));
|
||||
drawer.DrawLines(originShiftedPoints.ToArray());
|
||||
}
|
||||
|
||||
public string GetDescription() {
|
||||
return null;
|
||||
return "Draws a " + sides + " sided regular polygon, with rotation of " + offset + " radians, with inner radius of " + scale;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,14 @@ namespace Advanced_Software_Engineering {
|
||||
public Commander(Graphics graphics) {
|
||||
this.graphics = graphics;
|
||||
this.drawer = new Drawer(this.graphics);
|
||||
drawer.ResetDrawer();
|
||||
}
|
||||
|
||||
public Commander(Graphics graphics, string rawCommands) {
|
||||
this.graphics = graphics;
|
||||
this.drawer = new Drawer(this.graphics);
|
||||
this.ProcessCommands(rawCommands);
|
||||
drawer.ResetDrawer();
|
||||
}
|
||||
|
||||
public void AddCommand(Verb command) {
|
||||
@@ -78,6 +80,7 @@ namespace Advanced_Software_Engineering {
|
||||
|
||||
public void RemoveAllCommands() {
|
||||
commands = new List<Verb>();
|
||||
drawer.ResetDrawer();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -97,13 +100,18 @@ namespace Advanced_Software_Engineering {
|
||||
|
||||
public Drawer(Graphics graphics) {
|
||||
this.graphics = graphics;
|
||||
ResetDrawer();
|
||||
}
|
||||
|
||||
public void ResetDrawer() {
|
||||
graphics.Clear(Color.White);
|
||||
penPosition = new Point(0, 0);
|
||||
pen = new Pen(defaultColor, defaultWidth);
|
||||
brush = new SolidBrush(defaultColor);
|
||||
}
|
||||
|
||||
public void MovePen(Point point) {
|
||||
this.penPosition = point;
|
||||
}
|
||||
public void MovePen(Point point) => penPosition = point;
|
||||
public Point GetPenPosition() => new Point(penPosition.X, penPosition.Y);
|
||||
|
||||
public bool GetFill() => fill;
|
||||
public void EnableFill() => fill = false;
|
||||
@@ -139,7 +147,7 @@ namespace Advanced_Software_Engineering {
|
||||
|
||||
//Doesn't move pen
|
||||
public void DrawLines(GraphicsPath path) {
|
||||
path.Widen(pen);
|
||||
//path.Widen(pen);
|
||||
if (fill) graphics.FillPath(brush, path);
|
||||
graphics.DrawPath(pen, path);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user