Turtle Class
V triede je implementovaná korytnačia grafika.
C# .NET
class Turtle
{
public List<UIElement> Canvas { get; private set; }
public double Angle { get; private set; }
public Point Position { get; private set; }
public bool IsPenDown { get; private set; }
public bool IsUIThreadFinished { get; private set; }
public Turtle(int angle, Point position)
{
Canvas = new List<UIElement>();
Angle = angle;
Position = position;
IsUIThreadFinished = false;
}
public void DrawLine(double length, Color color, int thickness)
{
double actuallDrawingAngle = GetActuallDrawingAngle();
Point nextPoint;
double offsetY = Math.Cos(actuallDrawingAngle * (Math.PI / 180)) * length;
double offsetX = Math.Sin(actuallDrawingAngle * (Math.PI / 180)) * length;
if (Angle >= 0 && Angle < 90)
{
nextPoint.X = Position.X - offsetY;
nextPoint.Y = Position.Y - offsetX;
}
else if (Angle >= 90 && Angle < 180)
{
nextPoint.X = Position.X + offsetX;
nextPoint.Y = Position.Y - offsetY;
}
else if (Angle >= 180 && Angle < 270)
{
nextPoint.X = Position.X + offsetY;
nextPoint.Y = Position.Y + offsetX;
}
else if (Angle >= 270 && Angle <= 360) //You can also use only "else" statement
{
nextPoint.X = Position.X - offsetX;
nextPoint.Y = Position.Y + offsetY;
}
if (IsPenDown == true)
{
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
Line line = new Line();
line.X1 = Position.X;
line.Y1 = Position.Y;
line.X2 = nextPoint.X;
line.Y2 = nextPoint.Y;
line.StrokeThickness = thickness;
line.Stroke = new SolidColorBrush(color);
Canvas.Add(line);
}).AsTask().Wait();
}
Position = nextPoint;
}
/*public void DrawLine(double length, Color color, int thickness)
{
double actuallDrawingAngle = GetActuallDrawingAngle(); Debug.WriteLine("Angle: " + Angle);
Point nextPoint;
double offsetY = Math.Cos(actuallDrawingAngle * (Math.PI / 180)) * length;
double offsetX = Math.Sin(actuallDrawingAngle * (Math.PI / 180)) * length;
if (Angle >= 0 && Angle < 90)
{
nextPoint.X = Position.X - offsetY;
nextPoint.Y = Position.Y - offsetX;
}
else if (Angle >= 90 && Angle < 180)
{
nextPoint.X = Position.X + offsetX;
nextPoint.Y = Position.Y - offsetY;
}
else if (Angle >= 180 && Angle < 270)
{
nextPoint.X = Position.X + offsetY;
nextPoint.Y = Position.Y + offsetX;
}
else if (Angle >= 270 && Angle <= 360) //You can also use only "else" statement
{
nextPoint.X = Position.X - offsetX;
nextPoint.Y = Position.Y + offsetY;
}
if (IsPenDown == true)
{
Line line = new Line();
line.X1 = Position.X;
line.Y1 = Position.Y;
line.X2 = nextPoint.X;
line.Y2 = nextPoint.Y;
line.StrokeThickness = thickness;
line.Stroke = new SolidColorBrush(color);
Canvas.Add(line);
}
Position = nextPoint;
}*/
public void DrawPolygon(List<Point> pointCollection, Color color)
{
Polygon polygon = new Polygon();
foreach (Point point in pointCollection)
polygon.Points.Add(point);
polygon.Fill = new SolidColorBrush(color);
Canvas.Add(polygon);
}
public void DrawPolygon(List<Point> pointCollection, Color fillColor, double strokeThickness, Color strokeColor)
{
Polygon polygon = new Polygon();
foreach (Point point in pointCollection)
polygon.Points.Add(point);
polygon.Fill = new SolidColorBrush(fillColor);
polygon.StrokeThickness = strokeThickness;
polygon.Stroke = new SolidColorBrush(strokeColor);
Canvas.Add(polygon);
}
public void RotateLeft(double angle)
{
if (Angle - angle < 0)
Angle = Angle - angle + 360;
else
Angle -= angle;
}
public void RotateRight(double angle)
{
if (Angle + angle > 360)
Angle = Angle + angle - 360;
else
Angle += angle;
}
public void RotateAboutFace()
{
RotateRight(180);
}
public void PenUp()
{
IsPenDown = false;
}
public void PenDown()
{
IsPenDown = true;
}
public void SetState(State state)
{
Angle = state.Angle;
Position = state.Position;
}
private double GetActuallDrawingAngle()
{
double actuallDrawingAngle;
if (Angle >= 180)
actuallDrawingAngle = Angle - 180;
else
actuallDrawingAngle = Angle;
if (actuallDrawingAngle >= 90)//'>' -> '>='
actuallDrawingAngle -= 90;
return actuallDrawingAngle;
}
}
Neformátovaný
Přidáno: 23.12.2015
Expirace: Neuvedeno