
function [Configurations]= Rotary_Housing(E,R,W)
%call:  Rotary_Housing(E,R,W)
%This function outputs Data into the respective text files so solidworks
%can read them in solid works
%Sample: R = 10.4, Sample E = 1.5 (12a rotary motor)
%for a convex try : E=2,R=7
format long
A=[0:0.001:(2*pi-0.001)]; %range just befor overlap
X = E*cos(3*A)+R*cos(A);%equation for X values
Y = E*sin(3*A)+R*sin(A); %equation  for Y values
j=1; 
KF=R/E; % K factor of the motor, call KF if needed
for i=2:length(A)
    xv(j)=X(i);
    yv(j)=Y(i);
    zv(j)=0;
    j=j+1;
end %stores x and y values
xv(length(xv)+1)=xv(1);
yv(length(yv)+1)=yv(1);
zv(length(zv)+1)=0;
Data=[];
Data=[xv',yv',zv']; %saves values in a matrix of x and y (and z)
RRS=0; %clears previus info just in case
H=0;
W2=0;
RRS=0;
CT=0;
CT=R-E; %center-edge of combustion chamber
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% rotor (graphs the rotor with no arc)
rx=[];
rv=[];
rx(1)=0;
ry(1)=CT;
rx(2)=-R*sin(pi/3);
ry(2)=-R*cos(pi/3)-E;
rx(3)=R*sin(pi/3);
ry(3)=ry(2);
rx(4)=rx(1);
ry(4)=ry(1);
plot(xv,yv)
hold on
plot(rx,ry)
hold off
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%self intersection test
self_int=0;

for i = 1000:2000
    if xv(i+1)-xv(i)>0
        self_int=1;
    end
end
self_int
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% concavity test
if CT > R*cos(pi/3)+E
concave = 1;
concave
else
concave = 0;
convex=1
end
if concave == 1 && self_int==0
dlmwrite('Housing_Data.txt',Data);

else
dlmwrite('Housing_Data_Conv.txt',Data);

end
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HHS=R+E;%width from center of the housing
W2=2*R*sin(pi/3);
if concave == 1 
H=(CT-E)-R*cos(pi/3);%calculates the needed radius for concave
RRS=(H/2)+(W2^2/(8*H));
else
H=(R*cos(pi/3)+E)-CT;%calculates the needed radius for a convex
RRS=(H/2)+(W2^2/(8*H))+0.1; %intersection issues have been found, hence the increase in radius.
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plot(xv,yv)
hold on
plot(rx,ry)
hold off %plots housing and rotor
Constants=[];
Values=[];
Constants=[E,R,RRS,HHS,W,CT];
Values=["E","R","RRS","HHS","W","CT"];
Explanation=["Eccentricity","Center to Tip of Rotor","Radius of Arc on Sides of Rotor","Center to Side of Housing","width of part","center to tip of housing (not total hieght)"];
Configurations=[Values',Constants',Explanation']; 
a1= sprintf("E = %f", E);
a2= sprintf("R = %f", R);
a3= sprintf("RRS = %f", RRS);
a4= sprintf("HHS = %f", HHS);   
a5= sprintf("W = %f", W);
b=[a1,a2,a3,a4,a5];
if concave == 1 && self_int==0
fid = fopen('Equations.txt', 'wt');
fprintf(fid, '%s\n', b');
fclose(fid);
end
if concave == 0 && self_int==0
fid = fopen('Equationsconv.txt', 'wt');
fprintf(fid, '%s\n', b');
fclose(fid);
end
end