How to calculate a trapezoid with 4x2 input array...

3 ビュー (過去 30 日間)
Peter
Peter 2014 年 2 月 3 日
編集済み: Andrei Bobrov 2014 年 2 月 4 日
Input is a 4x2 array with the x coordinates in the 1st column and y coordinates in the second column. It has to calculate the area and perimeter of a trapezoid. I keep getting a wrong output from the test cases.
>> [A P] = myTrapezoid([[0 0]; [4 0]; [3 2]; [1 2]])
A =
6
P =
10.4721
>> [A P] = myTrapezoid([[2 0]; [4 4]; [1 3]; [0 1]])
A =
7.5000
P =
12.1065
Here is my code.
function [A, P] = myTrapezoid(Coord)
x1=Coord(1,1);
x2=Coord(2,1);
x3=Coord(3,1);
x4=Coord(4,1); % Defines x and y coordinates of input array.
y1=Coord(1,2);
y2=Coord(2,2);
y3=Coord(3,2);
y4=Coord(4,2);
A = ( ( ( abs(x2-x1) ) + ( abs(x3-x4) ) ) /2 ) * ( abs(y4-y1) );
%Calculates area of the trapezoid based on input coordinates.
P = ( abs(x2-x1) ) + ( abs(x3-x4) ) + ( abs(y3-y2) ) + ( abs(y4-y1) );
% Calculates perimeter of the trapezoid based on input coordinates.
end

回答 (2 件)

Roger Stafford
Roger Stafford 2014 年 2 月 3 日
In your formulas for A and P you have made overly simplistic assumptions about the trapezoid that is input. In computing A you assumed that the segment connecting points #1 and #2 and that connecting #3 and #4 were the parallel sides of the trapezoid, and moreover, that these were both parallel to the x-axis. As illustrated by your second example, it ain't necessarily so: in that case it is true that those two sides are also parallel to each other but they are not parallel to either the x-axis or the y-axis. Also it could happen that it is the other pair of sides that are the ones that are parallel. A thoughtful version of 'myTrapezoid' would warn the user if a non-trapezoid were entered - that is, one in which neither pair was parallel.
The formula you have for the perimeter is decidedly flaky. It could only be true if you were dealing with a rectangle whose sides were parallel to the x and y axes.
A valid method would first determine which pair of opposing line segments (if any) were parallel. Then it would determine the average of their two lengths. It would also then calculate the perpendicular distance between the two segments. The product of this distance and that average would give the trapezoid's area.
As for the perimeter, just find the sum of the lengths of the four segments. What could be easier? (But don't forget to invoke the Pythagoras Theorem?)
  3 件のコメント
Image Analyst
Image Analyst 2014 年 2 月 3 日
Are you aware of the polyarea() function? I mean, why do it yourself when there's a function already made for that ?
Roger Stafford
Roger Stafford 2014 年 2 月 3 日
@IA: I have a feeling Peter's instructor would object to that (if I have guessed right, and this is a specific assignment for dealing with trapezoids.) In any case it is good practice handling such problems, and in my opinion time and effort well spent.

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2014 年 2 月 4 日
編集済み: Andrei Bobrov 2014 年 2 月 4 日
function [A, P] = otherTrapezoid(c)
[~,i0] = sort(angle(bsxfun(@minus,c(2:end,:),c(1,:))*[1;1i]));
cx = diff(c([1;i0(:)+1;1],:)*[1;1i]);
a = angle(cx);
t = tan(a);
t1 = abs(bsxfun(@minus,t,t')) < 100*eps;
t1(eye(size(t1)) > 0) = false;
t1 = find(any(t1,2));
d = abs(cx);
A = sin(diff(a(t1(1)+[0;1])))*d(t1(1)+1)*mean(d(t1));
P = sum(d);
end
using
>>c0 = [[2 0]; [4 4]; [1 3]; [0 1]];
>>c = c0(randperm(4),:); % eg
>>
>> [A, P] = otherTrapezoid(c)
A = 7.5000
P = 12.107

カテゴリ

Help Center および File ExchangeComputational Geometry についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by