How do I plot the cross sectional area and centroid of an I-Beam using pgon ?
8 views (last 30 days)
Show older comments
Doug Leaffer
on 25 Mar 2024
Commented: Doug Leaffer
on 25 Mar 2024
How do I plot the cross sectional area and centroid of an I-Beam using pgon ? My code, below did not work.
% assuming the x-y plane origin is (0,0)
x = [ 0 0 1 1 2 2 3 3 4 4 5 5]; % x coords
y = [ 9 10 0 1 1 9 1 9 0 1 9 10]; % y coords ];
pgon = polyshape(x,y);
A = area(pgon)
[Cx Cy] = centroid(pgon);
h = plot(pgon)
hold on
plot(Cx,Cy,'r*', 'markerSize', 10)
grid minor
h(1).FaceColor = [0 0 1];
disp('centroid at (x,y) coords:'), disp([Cx Cy])
0 Comments
Accepted Answer
John D'Errico
on 25 Mar 2024
Edited: John D'Errico
on 25 Mar 2024
The funny thing is, your code was indeed essentially correct. You just started out wrongly.
x = [ 0 0 1 1 2 2 3 3 4 4 5 5]; % x coords
y = [ 9 10 0 1 1 9 1 9 0 1 9 10]; % y coords
Do those points, in THAT SPECIFIC sequence, represent the shape in question? NO. Of course not! I'm not even sure where you got that set of points. If you have problems in this, I would recommend you start with a piece of graph paper, draw the object to scale, then take the corner coordinates from there. Remember, you need the points in a SPECIFIC sequence, since they must represent the perimeter of the beam in question. If you choose some random sequence, polyshape will not figure out what you want. Software does not read minds. Ok, not yet. And I'm not totally sure I want to see a future where it does.
If, instead, we do this:
x = [1 4 4 3 3 5 5 0 0 2 2 1]; % x coords
y = [0 0 1 1 9 9 10 10 9 9 1 1]; % y coords
See that I started at one corner of the I-beam cross section, then worked around, IN SEQUENCE. I chose a counter-clockwise sequence. Of course, in this age of digital clocks, do people even know what that means? ;-)
Ibeam = polyshape(x,y);
plot(Ibeam)
axis equal
A = area(Ibeam)
[Cx Cy] = centroid(Ibeam)
hold on
plot(Cx,Cy,'r*', 'markerSize', 10)
That seems to work. The area seems correct too, based on simply counting squares. The centroid seems about right too.
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!