Add Assignment 3, MATLAB stuff
This commit is contained in:
parent
10c0d9d5dd
commit
1bf67d7ee3
14 changed files with 843 additions and 0 deletions
46
Assignment3/Circle.m
Normal file
46
Assignment3/Circle.m
Normal file
|
@ -0,0 +1,46 @@
|
|||
% Represents a circle.
|
||||
classdef Circle < Shape & ColorMixin
|
||||
properties
|
||||
Radius
|
||||
end
|
||||
|
||||
methods
|
||||
% The constructor takes a radius and calculates the area.
|
||||
function obj = Circle(radius)
|
||||
obj = obj@Shape("Circle")
|
||||
obj@ColorMixin("blue")
|
||||
obj.Radius = radius;
|
||||
obj.Area = obj.CalculateArea();
|
||||
end
|
||||
|
||||
% pi * r^2
|
||||
function area = CalculateArea(obj)
|
||||
obj.Area = pi * obj.Radius^2;
|
||||
area = obj.Area;
|
||||
end
|
||||
|
||||
function Display(obj)
|
||||
% Call the super method
|
||||
Display@Shape(obj)
|
||||
% Add on the radius property
|
||||
fprintf("\tRadius = %f\n", obj.Radius);
|
||||
end
|
||||
|
||||
function Draw(obj)
|
||||
Draw@Shape(obj);
|
||||
% Use a curved rectangle to draw a circle
|
||||
rectangle( ...
|
||||
'Position',[0 0 obj.Radius*2 obj.Radius*2],...
|
||||
'Curvature', [1 1], ...
|
||||
'FaceColor', obj.Color);
|
||||
% Center dot
|
||||
plot(obj.Radius, obj.Radius, '.', Color='black');
|
||||
% Radius line
|
||||
plot([obj.Radius obj.Radius*2], [obj.Radius obj.Radius]);
|
||||
% Put text denoting the radius above that line
|
||||
text(obj.Radius*1.5,obj.Radius, sprintf("r = %g", obj.Radius), HorizontalAlignment="center", VerticalAlignment="bottom");
|
||||
% Put text denoting the area
|
||||
text(obj.Radius, obj.Radius*1.5, sprintf("A = %g", obj.Area), HorizontalAlignment="center", VerticalAlignment="middle");
|
||||
end
|
||||
end
|
||||
end
|
BIN
Assignment3/Circle.png
Normal file
BIN
Assignment3/Circle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.8 KiB |
23
Assignment3/ColorMixin.m
Normal file
23
Assignment3/ColorMixin.m
Normal file
|
@ -0,0 +1,23 @@
|
|||
classdef ColorMixin < handle
|
||||
properties
|
||||
Color
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = ColorMixin(color)
|
||||
if nargin == 0
|
||||
obj.Color = "red";
|
||||
else
|
||||
obj.Color = color;
|
||||
end
|
||||
end
|
||||
|
||||
function color = GetColor(obj)
|
||||
color = obj.Color;
|
||||
end
|
||||
|
||||
function SetColor(obj, color)
|
||||
obj.Color = color;
|
||||
end
|
||||
end
|
||||
end
|
BIN
Assignment3/EquiTri.png
Normal file
BIN
Assignment3/EquiTri.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.3 KiB |
24
Assignment3/EquilateralTriangle.m
Normal file
24
Assignment3/EquilateralTriangle.m
Normal file
|
@ -0,0 +1,24 @@
|
|||
classdef EquilateralTriangle < Triangle & ColorMixin
|
||||
properties
|
||||
SideLength
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = EquilateralTriangle(sidelength)
|
||||
obj@Triangle(sidelength, sqrt(3) * sidelength/2);
|
||||
obj@ColorMixin("cyan")
|
||||
obj.Name = "Equilateral Triangle";
|
||||
obj.SideLength = sidelength;
|
||||
end
|
||||
|
||||
function Display(obj)
|
||||
Display@Triangle(obj);
|
||||
fprintf("\tSide Length: %f units\n", obj.SideLength);
|
||||
end
|
||||
|
||||
function Draw(obj)
|
||||
Draw@Triangle(obj);
|
||||
text(cos(pi/3)*obj.SideLength/2, sin(pi/3)*obj.SideLength/2, sprintf("l = %g", obj.SideLength), HorizontalAlignment="center", VerticalAlignment="bottom", Rotation=60);
|
||||
end
|
||||
end
|
||||
end
|
29
Assignment3/MyShapes.m
Normal file
29
Assignment3/MyShapes.m
Normal file
|
@ -0,0 +1,29 @@
|
|||
disp("Welcome to the Shapes script.")
|
||||
disp("Here are the following shapes you can make:")
|
||||
disp("Circle, Square, Rectangle, EquiTri, Triangle.")
|
||||
shapen = input("Which shape? ", "s");
|
||||
color = input("Color (use quotes or rgb vector)? ");
|
||||
shape = Shape("BAD SHAPE.");
|
||||
|
||||
switch (shapen)
|
||||
case "Circle"
|
||||
radius = input("Radius? ");
|
||||
shape = Circle(radius);
|
||||
case "Square"
|
||||
len = input("Side length? ");
|
||||
shape = Square(len);
|
||||
case "Rectangle"
|
||||
len = input("Length? ");
|
||||
h = input("Height? ");
|
||||
shape = Rectangle(len, h);
|
||||
case "EquiTri"
|
||||
len = input("Side Length? ");
|
||||
shape = EquilateralTriangle(len);
|
||||
case "Triangle"
|
||||
base = input("Base? ");
|
||||
height = input("Height? ");
|
||||
shape = Triangle(base, height);
|
||||
end
|
||||
|
||||
shape.SetColor(color)
|
||||
shape.Draw
|
39
Assignment3/Rectangle.m
Normal file
39
Assignment3/Rectangle.m
Normal file
|
@ -0,0 +1,39 @@
|
|||
classdef Rectangle < Shape & ColorMixin
|
||||
properties
|
||||
Length, Width
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = Rectangle(l,w)
|
||||
obj@Shape("Rectangle")
|
||||
obj@ColorMixin("red")
|
||||
obj.Length = l;
|
||||
obj.Width = w;
|
||||
obj.Area = obj.CalculateArea;
|
||||
end
|
||||
|
||||
function area = CalculateArea(obj)
|
||||
area = obj.Length*obj.Width;
|
||||
obj.Area = area;
|
||||
end
|
||||
|
||||
function Display(obj)
|
||||
Display@Shape(obj)
|
||||
fprintf("\tLength = %f\n", obj.Length);
|
||||
fprintf("\tWidth = %f\n", obj.Width);
|
||||
end
|
||||
|
||||
function Draw(obj)
|
||||
Draw@Shape(obj);
|
||||
% Draw a rectangle
|
||||
rectangle('Position',[0 0 obj.Length obj.Width], ...
|
||||
'FaceColor', obj.Color);
|
||||
% Draw length text
|
||||
text(obj.Length / 2, 0, sprintf("l = %g", obj.Length), HorizontalAlignment="center", VerticalAlignment="bottom");
|
||||
% Draw width text
|
||||
text(obj.Length, obj.Width/2, sprintf("w = %g", obj.Width), Rotation=90, HorizontalAlignment="center", VerticalAlignment="bottom");
|
||||
% Draw area text
|
||||
text(obj.Length/2, obj.Width/2, sprintf("A = %g", obj.Area), HorizontalAlignment="center", VerticalAlignment="middle");
|
||||
end
|
||||
end
|
||||
end
|
BIN
Assignment3/Rectangle.png
Normal file
BIN
Assignment3/Rectangle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.6 KiB |
43
Assignment3/Shape.m
Normal file
43
Assignment3/Shape.m
Normal file
|
@ -0,0 +1,43 @@
|
|||
% To let the shapes be in an array, they have to derive from the
|
||||
% Hetereogeneous mixin
|
||||
classdef Shape < ColorMixin & matlab.mixin.Heterogeneous & handle
|
||||
% These properties are shared by all Shapes.
|
||||
properties
|
||||
Name, Area
|
||||
end
|
||||
|
||||
methods
|
||||
% Base constructor to create a new shape with a specific name
|
||||
function obj = Shape(name)
|
||||
obj.Name = name;
|
||||
end
|
||||
|
||||
% Base method to display the shape in a readable format.
|
||||
function Display(obj)
|
||||
fprintf("%s shape with\n\tArea = %f square units\n\tColor = ", obj.Name, obj.Area);
|
||||
disp(obj.Color);
|
||||
end
|
||||
|
||||
% Base method that creates a figure with the title being the
|
||||
% shape's name.
|
||||
function Draw(obj)
|
||||
figure('Name',obj.Name)
|
||||
% Equal axes to preserve shape.
|
||||
axis equal
|
||||
% Prevents plot commands from resetting the figure.
|
||||
hold on
|
||||
end
|
||||
end
|
||||
|
||||
methods (Static)
|
||||
function CalculateStatistics(shapes)
|
||||
% Shapes input is an array of shape values.
|
||||
% Collect all their areas into an array.
|
||||
areas = [shapes.Area];
|
||||
% Call datastats with the column vector of all the areas.
|
||||
stats = datastats(areas(:));
|
||||
% Show output
|
||||
fprintf("Mean area: %f square units\nMedian area: %f square units\nStandard deviation: %f square units\n", stats.mean, stats.median, stats.std);
|
||||
end
|
||||
end
|
||||
end
|
20
Assignment3/Square.m
Normal file
20
Assignment3/Square.m
Normal file
|
@ -0,0 +1,20 @@
|
|||
classdef Square < Rectangle & ColorMixin
|
||||
properties
|
||||
SideLength
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = Square(sidelen)
|
||||
obj@Rectangle(sidelen, sidelen);
|
||||
obj@ColorMixin("green");
|
||||
obj.Name = "Square";
|
||||
obj.SideLength = sidelen;
|
||||
obj.Area = obj.CalculateArea;
|
||||
end
|
||||
|
||||
function Display(obj)
|
||||
Display@Shape(obj)
|
||||
fprintf("\tSide Length = %f\n", obj.SideLength);
|
||||
end
|
||||
end
|
||||
end
|
36
Assignment3/Triangle.m
Normal file
36
Assignment3/Triangle.m
Normal file
|
@ -0,0 +1,36 @@
|
|||
classdef Triangle < Shape & ColorMixin
|
||||
properties
|
||||
Base, Height
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = Triangle(base,height)
|
||||
obj@Shape("Triangle")
|
||||
obj@ColorMixin("yellow")
|
||||
obj.Base = base;
|
||||
obj.Height = height;
|
||||
obj.Area = obj.CalculateArea;
|
||||
end
|
||||
|
||||
function area = CalculateArea(obj)
|
||||
area = (obj.Base*obj.Height)/2;
|
||||
obj.Area = area;
|
||||
end
|
||||
|
||||
function Display(obj)
|
||||
Display@Shape(obj)
|
||||
fprintf("\tBase = %f units\n", obj.Base);
|
||||
fprintf("\tHeight = %f units\n", obj.Height);
|
||||
end
|
||||
|
||||
function Draw(obj)
|
||||
Draw@Shape(obj);
|
||||
ps = polyshape([0 0; obj.Base/2 obj.Height; obj.Base 0; 0 0]);
|
||||
plot(ps, FaceColor=obj.Color);
|
||||
plot([obj.Base/2 obj.Base/2],[0 obj.Height]);
|
||||
text(obj.Base/2, obj.Height/2, sprintf("h = %g", obj.Height), Rotation=90, HorizontalAlignment="center", VerticalAlignment="bottom");
|
||||
text(obj.Base/4,0,sprintf("b = %g", obj.Base), HorizontalAlignment="center", VerticalAlignment="bottom");
|
||||
text(obj.Base*(3/4),0,sprintf("A = %g", obj.Area), HorizontalAlignment="center", VerticalAlignment="bottom");
|
||||
end
|
||||
end
|
||||
end
|
BIN
Assignment3/Triangle.png
Normal file
BIN
Assignment3/Triangle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6 KiB |
BIN
Assignment3/report3.pdf
Normal file
BIN
Assignment3/report3.pdf
Normal file
Binary file not shown.
583
Assignment3/report3.tex
Normal file
583
Assignment3/report3.tex
Normal file
|
@ -0,0 +1,583 @@
|
|||
%! TeX program = lualatex
|
||||
\RequirePackage[l2tabu,orthodox]{nag}
|
||||
\DocumentMetadata{lang=en-US}
|
||||
\documentclass[a4paper]{scrartcl}
|
||||
\usepackage{geometry}
|
||||
\usepackage{graphicx}
|
||||
%\usepackage{tikz}
|
||||
%\usepackage{tikz-uml}
|
||||
\usepackage{hyperref}
|
||||
\usepackage{caption}
|
||||
\usepackage{subcaption}
|
||||
\usepackage{newfloat}
|
||||
\usepackage{fancyvrb}
|
||||
\usepackage{minted}[newfloat=true]
|
||||
\usepackage{bookmark}
|
||||
\usepackage{fontspec}
|
||||
\usepackage{microtype}
|
||||
|
||||
% Math packages
|
||||
\usepackage{amsmath}
|
||||
%\usepackage{mathtools}
|
||||
%\usepackage{amsthm}
|
||||
%\usepackage{thmtools}
|
||||
\usepackage{lualatex-math}
|
||||
\usepackage[warnings-off={mathtools-colon,mathtools-overbracket},math-style=ISO,bold-style=ISO]{unicode-math}
|
||||
|
||||
% Fonts
|
||||
\usepackage{newcomputermodern}
|
||||
\setmonofont{0xProto}[Scale=MatchLowercase]
|
||||
|
||||
\newcommand*{\figref}[2][]{%
|
||||
\hyperref[{fig:#2}]{%
|
||||
Figure~\ref*{fig:#2}%
|
||||
\ifx\\#1\\%
|
||||
\else
|
||||
\,#1%
|
||||
\fi
|
||||
}%
|
||||
}
|
||||
|
||||
\newcommand*{\lstref}[2][]{%
|
||||
\hyperref[{lst:#2}]{%
|
||||
Listing~\ref*{lst:#2}%
|
||||
\ifx\\#1\\%
|
||||
\else
|
||||
\,#1%
|
||||
\fi
|
||||
}%
|
||||
}
|
||||
|
||||
\setminted{breaklines=true,frame=single}
|
||||
\newenvironment{longlisting}{\captionsetup{type=listing}}{}
|
||||
\newenvironment{longfigure}{\captionsetup{type=figure}}{}
|
||||
\DefineVerbatimEnvironment{CommandWindow}{Verbatim}{frame=lines,
|
||||
label={Command Window}}
|
||||
\setlength{\belowcaptionskip}{10pt}
|
||||
\setlength{\abovecaptionskip}{8pt}
|
||||
%\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
|
||||
%\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
|
||||
|
||||
%\declaretheorem[within=chapter]{definition}
|
||||
%\declaretheorem[sibling=definition]{theorem}
|
||||
%\declaretheorem[sibling=definition]{corollary}
|
||||
%\declaretheorem[sibling=definition]{principle}
|
||||
|
||||
\usepackage{polyglossia}
|
||||
\usepackage[backend=biber]{biblatex}
|
||||
|
||||
\setdefaultlanguage[variant=american,ordinalmonthday=true]{english}
|
||||
|
||||
\day=15
|
||||
\month=4
|
||||
\year=2024
|
||||
|
||||
\title{Programming Assignment 3}
|
||||
\subtitle{CS-420 Spring 2024}
|
||||
\author{Juan Pablo Zendejas}
|
||||
\date{\today}
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
%\listoftheorems[ignoreall,onlynamed={theorem,corollary,principle}]
|
||||
%\listoftheorems[ignoreall,onlynamed={definition},title={List of Definitions}]
|
||||
%\tableofcontents
|
||||
|
||||
\section{Introduction}
|
||||
|
||||
For this assignment, I was tasked with implementing object-oriented
|
||||
programming principles in the MATLAB language. This includes inheritance
|
||||
in single, multiple, and multi-level forms. I also had to practice the
|
||||
concept of calling super-class methods and overriding methods. This
|
||||
involved creating a group of classes that represented different shapes,
|
||||
and implementing methods and providing properties for these classes.
|
||||
|
||||
\section{Task One: Class Creation and Constructors}
|
||||
|
||||
My first task was to create a base class called \texttt{Shape}. It holds
|
||||
properties \texttt{Name} and \texttt{Area}, a constructor to set the
|
||||
name, and a \texttt{Display} method. The code is displayed in
|
||||
\lstref{shape}.
|
||||
|
||||
Originally, it consisted of just the properties, the constructor, and
|
||||
the \texttt{Display} method. The other methods were added in the next
|
||||
tasks and will be discussed then. The output for the task is given in
|
||||
\figref{taskone}.
|
||||
|
||||
\begin{longfigure}
|
||||
\begin{CommandWindow}
|
||||
>> shape1 = Shape("Timmy")
|
||||
|
||||
shape1 =
|
||||
|
||||
Shape with properties:
|
||||
|
||||
Name: "Timmy"
|
||||
Area: []
|
||||
Color: "red"
|
||||
|
||||
>> shape2 = Shape("Jimmy")
|
||||
|
||||
shape2 =
|
||||
|
||||
Shape with properties:
|
||||
|
||||
Name: "Jimmy"
|
||||
Area: []
|
||||
Color: "red"
|
||||
|
||||
>> shape1.Display
|
||||
Timmy shape with
|
||||
Area = square units
|
||||
Color = red
|
||||
>> shape2.Display
|
||||
Jimmy shape with
|
||||
Area = square units
|
||||
Color = red
|
||||
>>
|
||||
\end{CommandWindow}
|
||||
\caption{Output from testing Task 1.}
|
||||
\label{fig:taskone}
|
||||
\end{longfigure}
|
||||
|
||||
\section{Task Two: Inheritance and Constructor Overloading}
|
||||
|
||||
This task involved creating new classes that derived from Shape, calling
|
||||
the super constructor to set the name. The code listings for the 3 classes are
|
||||
shown in \lstref{circle}, \lstref{rectangle}, and \lstref{triangle}.
|
||||
|
||||
|
||||
|
||||
When I started this task, it was a bit difficult at first to figure out
|
||||
how MATLAB's class inheritance works. A lot of MATLAB syntax is very
|
||||
different to me coming from other languages. Eventually, I figured out
|
||||
that you need to use the \texttt{<} less than symbol and the
|
||||
\texttt{\&}
|
||||
ampersand to define inheritance with the \texttt{classdef}.
|
||||
|
||||
All of the three constructors call the \texttt{Shape} constructor using
|
||||
the \texttt{@}-sign notation. This was another strange thing to me
|
||||
coming from other languages, and I'm still getting used to it. In
|
||||
MATLAB, the super-constructor calls have to come first and can't be in
|
||||
conditionals. However, all I needed it to do was set the shape name, so
|
||||
that wasn't a problem here.
|
||||
|
||||
Finally, I had to add a \texttt{CalculateArea} method to all of the
|
||||
classes. This method would calculate the area of the shape and save it
|
||||
to the \texttt{Area} property on the object. While attempting to
|
||||
implement this method, I found out that by default MATLAB classes are
|
||||
passed by value. This was annoying because the area wasn't being saved
|
||||
by the CalculateArea class and I couldn't figure out why. Eventually, I
|
||||
found out that to get behavior like which I'm used to from other
|
||||
languages, \texttt{Shape} had to derive from the \texttt{handle} class
|
||||
built-in to MATLAB. Deriving the \texttt{handle} class, as shown in
|
||||
\lstref{shape} on line 3, makes the class' instances pass by reference
|
||||
instead of by value. Then, assigning class properties became easy.
|
||||
|
||||
This \texttt{CalculateArea} method was called in the constructor to
|
||||
assign the correct value right after the object was constructed. See
|
||||
\figref{tasktwo} for the command window output and testing.
|
||||
|
||||
\begin{longfigure}
|
||||
\begin{CommandWindow}
|
||||
>> rect1 = Rectangle(5,6)
|
||||
|
||||
rect1 =
|
||||
|
||||
Rectangle with properties:
|
||||
|
||||
Length: 5
|
||||
Width: 6
|
||||
Name: "Rectangle"
|
||||
Area: 30
|
||||
Color: "red"
|
||||
|
||||
>> rect2 = Rectangle(6,7)
|
||||
|
||||
rect2 =
|
||||
|
||||
Rectangle with properties:
|
||||
|
||||
Length: 6
|
||||
Width: 7
|
||||
Name: "Rectangle"
|
||||
Area: 42
|
||||
Color: "red"
|
||||
|
||||
>> tri1 = Triangle(5,6)
|
||||
|
||||
tri1 =
|
||||
|
||||
Triangle with properties:
|
||||
|
||||
Base: 5
|
||||
Height: 6
|
||||
Name: "Triangle"
|
||||
Area: 15
|
||||
Color: "yellow"
|
||||
|
||||
>> tri2 = Triangle(2,7)
|
||||
|
||||
tri2 =
|
||||
|
||||
Triangle with properties:
|
||||
|
||||
Base: 2
|
||||
Height: 7
|
||||
Name: "Triangle"
|
||||
Area: 7
|
||||
Color: "yellow"
|
||||
|
||||
>> circ1 = Circle(5)
|
||||
|
||||
circ1 =
|
||||
|
||||
Circle with properties:
|
||||
|
||||
Radius: 5
|
||||
Name: "Circle"
|
||||
Area: 78.5398
|
||||
Color: "blue"
|
||||
|
||||
>> circ2 = Circle(10)
|
||||
|
||||
circ2 =
|
||||
|
||||
Circle with properties:
|
||||
|
||||
Radius: 10
|
||||
Name: "Circle"
|
||||
Area: 314.1593
|
||||
Color: "blue"
|
||||
\end{CommandWindow}
|
||||
\caption{Testing for Task Two.}
|
||||
\label{fig:tasktwo}
|
||||
\end{longfigure}
|
||||
|
||||
\section{Task Three: Method Overriding}
|
||||
|
||||
In this task, I wrote the \texttt{Display} methods for the
|
||||
\texttt{Circle}, \texttt{Rectangle}, and \texttt{Triangle} class. These
|
||||
methods override the original \texttt{Display} method defined in
|
||||
\texttt{Shape}, and are shown in the corresponding listings
|
||||
\lstref{circle}, \lstref{rectangle}, and \lstref{triangle}.
|
||||
|
||||
To make the implementation more consistent, I created them as an
|
||||
extension of the \texttt{Shape}'s \texttt{Display} method. I had to
|
||||
figure out the syntax for calling super methods in MATLAB, which was
|
||||
very similar to the method of calling super constructors. Thus, I had to
|
||||
write \texttt{Display@Shape(obj)} to first call the super method. Then,
|
||||
I used more \texttt{fprintf} function calls to add on appropriate
|
||||
information for the class. The test output is shown in
|
||||
\figref{taskthree}.
|
||||
|
||||
\begin{longfigure}
|
||||
\begin{CommandWindow}
|
||||
>> rect1.Display
|
||||
Rectangle shape with
|
||||
Area = 30.000000 square units
|
||||
Color = red
|
||||
Length = 5.000000
|
||||
Width = 6.000000
|
||||
>> circ1.Display
|
||||
Circle shape with
|
||||
Area = 78.539816 square units
|
||||
Color = blue
|
||||
Radius = 5.000000
|
||||
>> tri1.Display
|
||||
Triangle shape with
|
||||
Area = 15.000000 square units
|
||||
Color = yellow
|
||||
Base = 5.000000 units
|
||||
Height = 6.000000 units
|
||||
\end{CommandWindow}
|
||||
\caption{Test output for Task 3.}
|
||||
\label{fig:taskthree}
|
||||
\end{longfigure}
|
||||
|
||||
\section{Task Four: Multi-Level Inheritance}
|
||||
|
||||
Multi-level inheritance is when you create a subclass of a subclass,
|
||||
which increases the level of separation and grouping. For our Shapes, I
|
||||
had to create classes that represent an Equilateral Triangle and a
|
||||
Square. Thus, they would derive from \texttt{Triangle} and
|
||||
\texttt{Rectangle} respectively. The code for these classes is shown in
|
||||
\lstref{equitri} and \lstref{square}.
|
||||
|
||||
In these classes, the constructor called their respective super class
|
||||
constructor, with appropriate properties calculated from the given
|
||||
value. Thus, the area would be properly assigned. All that was left was
|
||||
to change the name. While I would have simply called the \texttt{Shape}
|
||||
constructor, it was already in use by the super class. And, as explained
|
||||
in the static method section, it would have caused issues with the later
|
||||
implementation. Thus, I decided to simply overwrite it with an
|
||||
assignment for these classes.
|
||||
|
||||
I did not have to override the \texttt{CalculateArea} method since the
|
||||
calculation method is the same for the derived shapes. However, I did
|
||||
override the \texttt{Display} method, calling the super-class version
|
||||
and adding on the specialized properties. The test output is shown in
|
||||
\figref{taskfour}.
|
||||
|
||||
\begin{longfigure}
|
||||
\begin{CommandWindow}
|
||||
>> eqtri1 = EquilateralTriangle(5);
|
||||
>> sq1 = Square(4);
|
||||
>> eqtri1.Display
|
||||
Equilateral Triangle shape with
|
||||
Area = 10.825318 square units
|
||||
Color = cyan
|
||||
Base = 5.000000 units
|
||||
Height = 4.330127 units
|
||||
Side Length: 5.000000 units
|
||||
>> sq1.Display
|
||||
Square shape with
|
||||
Area = 16.000000 square units
|
||||
Color = green
|
||||
Side Length = 4.000000
|
||||
\end{CommandWindow}
|
||||
\caption{Test output for Task 4.}
|
||||
\label{fig:taskfour}
|
||||
\end{longfigure}
|
||||
|
||||
\section{Task Five: Multiple Inheritance}
|
||||
|
||||
Multiple inheritance differs from multi-level inheritance, as multiple
|
||||
inheritance is when one class derives from multiple super-classes. For
|
||||
this task, I had to create a new class called \texttt{ColorMixin} that
|
||||
simply defined a new property, \texttt{Color}, and some methods to
|
||||
change it. The class is shown in \lstref{color}. As I fixed bugs, it
|
||||
turned out that all classes on an object had to be \texttt{handle}s, so
|
||||
I had to make \texttt{ColorMixin} derive from that class.
|
||||
|
||||
Also, I had to make \texttt{ColorMixin} have a default color value. This
|
||||
was achieved by reading the MATLAB documentation, and using the
|
||||
\texttt{nargin} variable. This variable contains the number of arguments
|
||||
given to the function. When it's 0, I simply set \texttt{Color} to be
|
||||
red by default.
|
||||
|
||||
In addition, I discovered that MATLAB colors can either be a string or a
|
||||
vector of RGB values. So, the set and get color methods have no
|
||||
restriction on the type of the parameter.
|
||||
|
||||
Finally, I had to make all the shapes derive from the
|
||||
\texttt{ColorMixin} class so I could use its constructor in the shape
|
||||
constructors. Each shape has a different default color based on what I
|
||||
feel that shape's color is. The test output for this task is shown in
|
||||
\figref{taskfive}.
|
||||
|
||||
\begin{longfigure}
|
||||
\begin{CommandWindow}
|
||||
>> circ1.Display
|
||||
Circle shape with
|
||||
Area = 78.539816 square units
|
||||
Color = red
|
||||
Radius = 5.000000
|
||||
>> circ1.SetColor("blue")
|
||||
>> tri1.Display
|
||||
Triangle shape with
|
||||
Area = 15.000000 square units
|
||||
Color = yellow
|
||||
Base = 5.000000 units
|
||||
Height = 6.000000 units
|
||||
>> tri1.SetColor([0.5 0.3 0.9])
|
||||
>> tri1.Display
|
||||
Triangle shape with
|
||||
Area = 15.000000 square units
|
||||
Color = 0.5000 0.3000 0.9000
|
||||
|
||||
Base = 5.000000 units
|
||||
Height = 6.000000 units
|
||||
\end{CommandWindow}
|
||||
\caption{Test output for Task 5.}
|
||||
\label{fig:taskfive}
|
||||
\end{longfigure}
|
||||
|
||||
\section{Task Six: Static Method}
|
||||
|
||||
This task involves creating a static method to determine some statistics
|
||||
about a list of shapes' area values. To achieve this, the \texttt{Shape}
|
||||
class has to derive from the \texttt{Heterogeneous} mixin, in order
|
||||
for heterogeneous arrays of shapes to be created. This also adds the
|
||||
restriction that classes can only derive from one heterogeneous class;
|
||||
which is why Equilateral Triangle and Square could not also derive from
|
||||
Shape.
|
||||
|
||||
To implement this method, I used the MATLAB \texttt{datastats} method,
|
||||
which returns a dictionary containing some common statistic parameters.
|
||||
In particular, we're looking at the mean, median, and standard
|
||||
deviation. Look at \lstref{shape} to see the implementation. In essence,
|
||||
I can use the vectorization properties of MATLAB to use the common
|
||||
``parameter get'' dot notation, which actually collects the
|
||||
\texttt{Area} property of all the shapes in the array. I then collect it
|
||||
into another array by wrapping it in square brackets.
|
||||
|
||||
Finally, to collect the array into a column vector that
|
||||
\texttt{datastats} requires, I use the spread operator \texttt{(:)} with
|
||||
null parameters to get the entire array in a column vector.
|
||||
\texttt{datastats} will calculate the statistics, which are then printed
|
||||
out. An example is shown in \figref{tasksix}.
|
||||
|
||||
\begin{longfigure}
|
||||
\begin{CommandWindow}[breaklines=true]
|
||||
>> shapes = [Triangle(4,5), Circle(5), Rectangle(4,5), EquilateralTriangle(6), Square(10)]
|
||||
|
||||
shapes =
|
||||
|
||||
1x5 heterogeneous Shape (Triangle, Circle, Rectangle, ...) array with properties:
|
||||
|
||||
Name
|
||||
Area
|
||||
Color
|
||||
|
||||
>> Shape.CalculateStatistics(shapes)
|
||||
Mean area: 44.825655 square units
|
||||
Median area: 20.000000 square units
|
||||
Standard deviation: 41.427063 square units
|
||||
\end{CommandWindow}
|
||||
\caption{Test output for Task 6.}
|
||||
\label{fig:tasksix}
|
||||
\end{longfigure}
|
||||
|
||||
\section{Task Seven: Visualization}
|
||||
|
||||
For this task, I had to use MATLAB's built-in visualization methods to
|
||||
create a \texttt{Draw} method that displays a figure representing the
|
||||
shape. I achieved this primarily using the \texttt{plot}, \texttt{text},
|
||||
and \texttt{rectangle} functions included in MATLAB.
|
||||
|
||||
I first created a base \texttt{Draw} method in the \texttt{Shape} class.
|
||||
By reading the documentation, I found out that I could use the
|
||||
\texttt{figure} function to start a new figure and give it a name, which
|
||||
I set to the shape's name. To preserve the outline of the shape, I used
|
||||
\texttt{axis equal}, and to prevent future \texttt{plot} calls from
|
||||
changing the options, I used \texttt{hold on}.
|
||||
|
||||
For the circle in \lstref{circle}, I used a call to \texttt{rectangle}
|
||||
with a curvature of 1. I added some lines to draw a point and a line,
|
||||
then some text for the area and radius.
|
||||
|
||||
For the triangle, I used the \texttt{polyshape} method I found in some
|
||||
documentation examples. I passed it a matrix of coordinates that defined
|
||||
the points of the shape, and then used the \texttt{plot} command to fill
|
||||
it in. Finally, I added some text and lines to represent the base,
|
||||
height, and area. The equilateral triangle class uses the triangle draw
|
||||
method, but adds the side length as text on the figure.
|
||||
|
||||
For the rectangle, I just used the \texttt{rectangle} method to fill in
|
||||
the shape. Then, I had 3 calls to \texttt{text} to add length, width,
|
||||
and area. The square doesn't add anything new to the figure.
|
||||
|
||||
Some example figures are shown in \figref{circle}, \figref{triangle},
|
||||
\figref{equitri}, and \figref{rectangle}.
|
||||
|
||||
\begin{figure}[!hbt]
|
||||
\centering
|
||||
\subcaptionbox{A circle.\label{fig:circle}}[.475\linewidth]
|
||||
{\includegraphics[width=\linewidth]{Circle.png}}\hfill %
|
||||
\subcaptionbox{A triangle.\label{fig:triangle}}[.475\linewidth]
|
||||
{\includegraphics[width=\linewidth]{Triangle.png}}
|
||||
|
||||
\medskip
|
||||
|
||||
\subcaptionbox{An equilateral triangle.\label{fig:equitri}}[.475\linewidth]
|
||||
{\includegraphics[width=\linewidth]{EquiTri.png}}\hfill %
|
||||
\subcaptionbox{A rectangle.\label{fig:rectangle}}[.475\linewidth]
|
||||
{\includegraphics[width=\linewidth]{Rectangle.png}}
|
||||
|
||||
\caption{Shapes as generated by MATLAB.}
|
||||
\label{fig:shapes}
|
||||
\end{figure}
|
||||
|
||||
\section{Task Eight: User Interaction}
|
||||
|
||||
The final task was to create an interactive script that lets a user
|
||||
create a shape and type out the parameters. This code is shown in
|
||||
\lstref{script}. The script is fairly simple. I use the \texttt{input}
|
||||
method of MATLAB to ask for a name of a shape. I need the \texttt{"s"}
|
||||
operator to get a simple string. However, for the color and for the
|
||||
parameter inputs, I don't use that so the user can input an expression,
|
||||
or for the color, a vector of RGB values. An example of its use is found
|
||||
in \figref{script}. At the end, a window opens showing the shape as
|
||||
found in \figref{shapes}.
|
||||
|
||||
\begin{longfigure}
|
||||
\begin{CommandWindow}
|
||||
>> MyShapes
|
||||
Welcome to the Shapes script.
|
||||
Here are the following shapes you can make:
|
||||
Circle, Square, Rectangle, EquiTri, Triangle.
|
||||
Which shape?
|
||||
Circle
|
||||
Color (use quotes or rgb vector)?
|
||||
[0.9 0.4 0.2]
|
||||
Radius?
|
||||
10
|
||||
\end{CommandWindow}
|
||||
\caption{Output of MyShapes script.}
|
||||
\label{fig:script}
|
||||
\end{longfigure}
|
||||
|
||||
\section{Conclusion}
|
||||
|
||||
This was certainly an interesting project. To be honest, the PDF given
|
||||
was a little confusing with the instructions, but I believe I was able
|
||||
to learn all that I needed about MATLAB's standard library and its uses.
|
||||
I had a lot of fun making this document look nice and pretty, especially
|
||||
\figref{shapes} which took a bit of digging. Also, the MATLAB
|
||||
documentation is immense but easy to search through and has plenty of
|
||||
examples.
|
||||
|
||||
\section{Source Code}
|
||||
|
||||
\begin{longlisting}
|
||||
\inputminted[label={Shape.m}]{matlab}{Shape.m}
|
||||
\caption[Shape.m]{Shape class.}
|
||||
\label{lst:shape}
|
||||
\end{longlisting}
|
||||
|
||||
\begin{longlisting}
|
||||
\inputminted[label={Circle.m}]{matlab}{Circle.m}
|
||||
\caption[Circle.m]{Circle class.}
|
||||
\label{lst:circle}
|
||||
\end{longlisting}
|
||||
|
||||
\begin{longlisting}
|
||||
\inputminted[label={Rectangle.m}]{matlab}{Rectangle.m}
|
||||
\caption[Rectangle.m]{Rectangle class.}
|
||||
\label{lst:rectangle}
|
||||
\end{longlisting}
|
||||
|
||||
\begin{longlisting}
|
||||
\inputminted[label={Triangle.m}]{matlab}{Triangle.m}
|
||||
\caption[Triangle.m]{Triangle class.}
|
||||
\label{lst:triangle}
|
||||
\end{longlisting}
|
||||
|
||||
\begin{longlisting}
|
||||
\inputminted[label={EquilateralTriangle.m}]{matlab}{EquilateralTriangle.m}
|
||||
\caption[EquilateralTriangle.m]{EquilateralTriangle class.}
|
||||
\label{lst:equitri}
|
||||
\end{longlisting}
|
||||
|
||||
\begin{longlisting}
|
||||
\inputminted[label={Square.m}]{matlab}{Square.m}
|
||||
\caption[Square.m]{Square class.}
|
||||
\label{lst:square}
|
||||
\end{longlisting}
|
||||
|
||||
\begin{longlisting}
|
||||
\inputminted[label={ColorMixin.m}]{matlab}{ColorMixin.m}
|
||||
\caption[ColorMixin.m]{ColorMixin class.}
|
||||
\label{lst:color}
|
||||
\end{longlisting}
|
||||
|
||||
\begin{longlisting}
|
||||
\inputminted[label={MyShapes.m}]{matlab}{MyShapes.m}
|
||||
\caption[MyShapes.m]{MyShapes script.}
|
||||
\label{lst:script}
|
||||
\end{longlisting}
|
||||
|
||||
\end{document}
|
||||
|
Loading…
Reference in a new issue