---
title: "iemisc: secprop Example (R and GNU Octave)"
author: "Irucka Embry, E.I.T. (EcoC²S)"
date: "`r Sys.Date()`"
lang: en-us
urlcolor: blue
output:
  rmarkdown::pdf_document:
    highlight: kate
    toc: true
    latex_engine: xelatex
vignette: >
  %\VignetteIndexEntry{iemisc secprop Example (R and GNU Octave)}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

\bigskip

# secprop Example (R style)

```{r, warning = FALSE, message = FALSE, tidy = TRUE}

# Example 1 (Caprani Reference)

library("iemisc")
import::from(ramify, mat)

vo <- mat("0, 0; 600, 0; 630, 580; 1200, 650; 1200, 920; 900, 920; 900, 845; 0, 845")
vo

vi <- mat("0, 300; 300, 300; 300, 695; 0, 695")
vi

# The following will plot both the original and the final (transformed plot)
SP <- secprop(outer = vo, inner = vi, original_plot = 1, final_plot = 1)
SP
```

\bigskip
\bigskip
\bigskip
\bigskip

# secprop Example (GNU Octave style)

```{octave eval = FALSE}
%
%    Copyright (C) 2008  Colin Caprani - www.colincaprani.com
%
%    This program is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.

function SP = secprop(outer,inner,plotflag)
% Section Properties Calculator
% outer is a matrix of outer coordinates (x,y)
% inner is a matrix of coordinates for a void
% SP is a vector of A,I,yt and yb properties of the section

    [nOC n] = size(outer); [nIC n] = size(inner);
    outer(nOC+1,:) = outer(1,:);
    inner(nIC+1,:) = inner(1,:);
   
    propsOC = zeros(1,3); propsIC = propsOC;
    propsOC = algor(outer);
    if(nIC > 2); propsIC = algor(inner); end;
    A = propsOC(1) - propsIC(1);
    I = propsOC(2) - propsIC(2);
    ybar = propsOC(3) - propsIC(3);
    
    ybar = ybar/A;
    I = I-A*ybar^2;   
    A = 2*A; I = 2*I;
    yt = max(outer(:,2)) - ybar;
    yb = ybar;
    SP = [A,I,yt,yb]; SP = SP';
    
    if plotflag == 1
        outer(nOC+1:2*nOC-1,1) = -outer(nOC:-1:2,1);
        outer(nOC+1:2*nOC-1,2) =  outer(nOC:-1:2,2);
        inner(nIC+1:2*nIC-1,1) = -inner(nIC:-1:2,1);
        inner(nIC+1:2*nIC-1,2) =  inner(nIC:-1:2,2);
        h = fill(outer(:,1),outer(:,2),'r'); hold on;
        fill(inner(:,1),inner(:,2),'w'); axis equal;

        s1 = sprintf('Area = %d mm2 | Height to N-A = %3.1f mm',A,round(ybar*10)/10);
        s2 = sprintf('Second Moment of Area = %d mm4',I);
        s3 = sprintf('Elastic Moduli, t = %d mm3; b = %d mm3',I/yt,I/yb);
        annotation1 = annotation('textbox',[0.15 0.12 0.4 0.14],
        {s1,s2,s3});
% annotation1 line modified by Irucka Embry to avoid the error message associated with
% set: unknown hggroup property FitHeightToText
    end
end

function props = algor(vc)
    A = 0; ybar = 0; I = 0;
    x = vc(:,1); y = vc(:,2);
    n = length(x);
    for i = 1:(n-1)
        A = A + 0.5*(x(i)-x(i+1))*(y(i)+y(i+1));
        ybar = ybar + (1/6)*(x(i)-x(i+1))*(y(i)^2 + y(i)*y(i+1) + y(i+1)^2);
        I = I + (1/12)*(x(i)-x(i+1))*(y(i)^3 + y(i)^2*y(i+1)+y(i)*y(i+1)^2 + y(i+1)^3);
    end
    props = [A,I,ybar];
end


% check against GNU Octave

vo = [0, 0; 600, 0; 630, 580; 1200, 650; 1200, 920; 900, 920; 900, 845; 0, 845];
vi = [0, 300; 300, 300; 300, 695; 0, 695];
SP = secprop(vo, vi, 1)

print("file.png")

% Results
warning: annotation: couldn't parse PROP/VAL pairs, skipp
warning: called from
    annotation at line 218 column 7
    secprop at line 36 column 21

SP =

   1.1175e+06
   8.6279e+10
   4.1937e+02
   5.0063e+02
```

\bigskip
\bigskip

```{r, echo = FALSE, out.width = '100%'}
linguisticsdown::include_graphics2("secprop_gnu-octave_figure.png")
```

\bigskip
\bigskip
\bigskip
\bigskip

## Works Cited

Colin Caprani, "Section Properties Calculator", https://www.colincaprani.com/programming/matlab/.

\bigskip
\bigskip

# EcoC²S Links

EcoC²S Home -- https://www.ecoccs.com/  
About EcoC²S -- https://www.ecoccs.com/about_ecoc2s.html  
Services -- https://www.ecoccs.com/services.html  
Products -- https://www.questionuniverse.com/products.html  
EcoC²S Media -- https://www.ecoccs.com/media.html  
EcoC²S Resources -- https://www.ecoccs.com/resources.html  
R Trainings and Resources provided by EcoC²S (Irucka Embry, E.I.T.) -- https://www.ecoccs.com/rtraining.html  

\bigskip
\bigskip

# Copyright and License

All R code written by Irucka Embry is distributed under the GPL-3 (or later) license, see the [GNU General Public License {GPL} page](https://www.gnu.org/licenses/gpl-3.0.html).  

All written content originally created by Irucka Embry is copyrighted under the Creative Commons Attribution-ShareAlike 4.0 International License. All other written content retains the copyright of the original author(s).  

This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).  
