Private
Public Access
Implemented RegularPolygons
This commit is contained in:
1 parent
ae701dfb05
commit
5ea0f49bb1
2 files changed
+61
-15
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.CodeDom;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Advanced_Software_Engineering {
|
namespace Advanced_Software_Engineering {
|
||||||
class VerbFactory {
|
class VerbFactory {
|
||||||
@@ -9,7 +11,7 @@ namespace Advanced_Software_Engineering {
|
|||||||
Dictionary<string, string[]> commandAndParameters = SettingsAndHelperFunctions.CommandAndParameterParser(fullCommand);
|
Dictionary<string, string[]> commandAndParameters = SettingsAndHelperFunctions.CommandAndParameterParser(fullCommand);
|
||||||
|
|
||||||
string command = commandAndParameters["command"][0];
|
string command = commandAndParameters["command"][0];
|
||||||
string[] parameters = commandAndParameters["parameters"];
|
string[] parameters = commandAndParameters.Keys.Contains("parameters") ? commandAndParameters["parameters"] : new string[] { };
|
||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "moveto":
|
case "moveto":
|
||||||
@@ -41,14 +43,30 @@ namespace Advanced_Software_Engineering {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
} else throw new Exception("Command has an incorrect number of parameters");
|
} else throw new Exception("Command has an incorrect number of parameters");
|
||||||
|
|
||||||
case "dot":
|
|
||||||
case "clear":
|
|
||||||
case "shape":
|
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 "square":
|
||||||
case "rectangle":
|
case "rectangle":
|
||||||
case "circle":
|
case "circle":
|
||||||
case "triangle":
|
case "triangle":
|
||||||
|
|
||||||
|
|
||||||
|
case "dot":
|
||||||
|
case "clear":
|
||||||
case "pen":
|
case "pen":
|
||||||
case "fill":
|
case "fill":
|
||||||
case "color":
|
case "color":
|
||||||
@@ -91,7 +109,7 @@ namespace Advanced_Software_Engineering {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DrawTo : Verb {
|
class DrawTo : Verb {
|
||||||
protected Drawer drawer;
|
protected Drawer drawer;
|
||||||
protected Point moveToPoint;
|
protected Point moveToPoint;
|
||||||
|
|
||||||
@@ -138,23 +156,43 @@ namespace Advanced_Software_Engineering {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A class that generates sets
|
/// A class that generates a RegularPolygon verb.
|
||||||
/// </summary>
|
/// </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() {
|
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() {
|
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) {
|
public Commander(Graphics graphics) {
|
||||||
this.graphics = graphics;
|
this.graphics = graphics;
|
||||||
this.drawer = new Drawer(this.graphics);
|
this.drawer = new Drawer(this.graphics);
|
||||||
|
drawer.ResetDrawer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Commander(Graphics graphics, string rawCommands) {
|
public Commander(Graphics graphics, string rawCommands) {
|
||||||
this.graphics = graphics;
|
this.graphics = graphics;
|
||||||
this.drawer = new Drawer(this.graphics);
|
this.drawer = new Drawer(this.graphics);
|
||||||
this.ProcessCommands(rawCommands);
|
this.ProcessCommands(rawCommands);
|
||||||
|
drawer.ResetDrawer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddCommand(Verb command) {
|
public void AddCommand(Verb command) {
|
||||||
@@ -78,6 +80,7 @@ namespace Advanced_Software_Engineering {
|
|||||||
|
|
||||||
public void RemoveAllCommands() {
|
public void RemoveAllCommands() {
|
||||||
commands = new List<Verb>();
|
commands = new List<Verb>();
|
||||||
|
drawer.ResetDrawer();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -97,13 +100,18 @@ namespace Advanced_Software_Engineering {
|
|||||||
|
|
||||||
public Drawer(Graphics graphics) {
|
public Drawer(Graphics graphics) {
|
||||||
this.graphics = graphics;
|
this.graphics = graphics;
|
||||||
|
ResetDrawer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ResetDrawer() {
|
||||||
|
graphics.Clear(Color.White);
|
||||||
|
penPosition = new Point(0, 0);
|
||||||
pen = new Pen(defaultColor, defaultWidth);
|
pen = new Pen(defaultColor, defaultWidth);
|
||||||
brush = new SolidBrush(defaultColor);
|
brush = new SolidBrush(defaultColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MovePen(Point point) {
|
public void MovePen(Point point) => penPosition = point;
|
||||||
this.penPosition = point;
|
public Point GetPenPosition() => new Point(penPosition.X, penPosition.Y);
|
||||||
}
|
|
||||||
|
|
||||||
public bool GetFill() => fill;
|
public bool GetFill() => fill;
|
||||||
public void EnableFill() => fill = false;
|
public void EnableFill() => fill = false;
|
||||||
@@ -139,7 +147,7 @@ namespace Advanced_Software_Engineering {
|
|||||||
|
|
||||||
//Doesn't move pen
|
//Doesn't move pen
|
||||||
public void DrawLines(GraphicsPath path) {
|
public void DrawLines(GraphicsPath path) {
|
||||||
path.Widen(pen);
|
//path.Widen(pen);
|
||||||
if (fill) graphics.FillPath(brush, path);
|
if (fill) graphics.FillPath(brush, path);
|
||||||
graphics.DrawPath(pen, path);
|
graphics.DrawPath(pen, path);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user