How can I make a function that has an input that cake take handle both scalar and array values?
Show older comments
function [format,area] = polygonArea(numSides,sideLengths)
L = length(sideLengths);
RightTriangleArea = (L(1)*L(2))/2;
areaRectangle = sideLengths(1)*sideLengths(2);
p = sideLengths*numSides;
a = sideLengths/(2*tan(180/numSides));
regularPolygonArea = (p.*a)/2;
if numSides <3|| numSides <0
disp('invalid 1');
elseif numSides ==3 && L(1) ~= L(2)
area = RightTriangleArea;
format = true;
disp('valid right');
elseif numSides ==4 && L(1) ~= L(2)
area = areaRectangle;
format = true;
disp('valid rectangle');
elseif numSides >4 && size(sideLengths)== 0
area = regularPolygonArea;
format = true;
disp('valid polygon');
else
format = false;
area = false;
disp('invalid 2');
end
4 Comments
Walter Roberson
on 14 Oct 2023
Which inputs do you want to be able to vectorize over?
If you want to vectorize over sideLengths, and if the number of sides can be different for each polygon, then do you want to use a cell array of side lengths, or do you want to pad the shorter sides with NaN, or ... ?
How do you want to handle the possibility that some polygons might be valid but others might not be? For example do you want to create an additional output that has the per-shape message in them?
By the way: if numSides is negative then numSides will be <3 so you do not need to test for both < 3 and < 0
Samiyah
on 14 Oct 2023
Walter Roberson
on 14 Oct 2023
You could normally deduce numSides by looking at the size of the passed sideLengths .
However, it would be reasonable to allow a scalar side length to be passed in, along with the number of sides, with the interpretation to be that there are to be that many sides of equal length all of which are equal to the scalar.
Hi @Samiyah
I don't understand your statement of "I want the input sideLengths to be able to take both scalar and array values". Can you give examples for 4 cases, namely a Triangle, a Square, a Rectangle, a Trapezoid? Ignore other polygons at the moment.
Case 1: Triangle (each side can have different length) -- general case
numSides = 3;
sideLengths = [];
Case 2: Square (all sides have the same length)
numSides = 4;
sideLengths = [];
Case 3: Rectangle (two sides have the same lengths)
numSides = 4;
sideLengths = [];
Case 4: Trapezoid (each side can have different length) -- general case
numSides = 4;
sideLengths = [];
Answers (1)
Hi @Samiyah
Maybe you want to display the result like this?
numSides = 4;
sideLengths = [1, 2];
polygonArea(numSides, sideLengths)
function [format,area] = polygonArea(numSides,sideLengths)
L = sideLengths;
RightTriangleArea = (L(1)*L(2))/2;
areaRectangle = sideLengths(1)*sideLengths(2);
p = sideLengths*numSides;
a = sideLengths/(2*tan(180/numSides));
regularPolygonArea = (p.*a)/2;
if numSides <3|| numSides <0
disp('invalid 1');
elseif numSides ==3 && L(1) ~= L(2)
area = RightTriangleArea;
format = true;
disp('valid right');
elseif numSides ==4 && L(1) ~= L(2)
area = areaRectangle;
format = true;
disp('valid rectangle');
elseif numSides >4 && size(sideLengths)== 0
area = regularPolygonArea;
format = true;
disp('valid polygon');
else
format = false;
area = false;
disp('invalid 2');
end
end
2 Comments
Samiyah
on 14 Oct 2023
Dyuman Joshi
on 16 Oct 2023
Where do you get the error?
What are the input values you have provided? And have you used the same exact code @Sam has provided above?
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!