function poly = NE_gen_pentagon(b)
% NE_gen_pentagon - generate a tiling pentagon
%     Stahl p.86 Figure 6.7
% On input:
%     b (Boolean): 1: draw pentagon in new figure; else don't
% On output:
%     poly(poly struct): polygon info
%       (k).pts (n_k x 2 array): points in k_th segment
%          .all_pts (nx2 array): all segment points [only in first segment
%          .corners1 (1x10 vector): corners as vector: [x1,y1,...,x5,y5]
%          .corners2 (5x2 array): corners as 2D array
% Call:
%     poly = NE_gen_pentagon(1);
% Author:
%     T. Henderson
%     UU
%     Summer 2023
%

u = cot(36*pi/180);    % 1.3764
r = csc(36*pi/180);    % 1.7013
k = 1.8717;

A = [0;k];
B = [0.9094;1.6359];
C = [0;1];
G = [u;0];
H = [0;0];
thetas_BpB = [pi/2 + 0.5073:-0.01:pi/2-0.5073];  % /_AB = 0.5073
x_BpB = k*cos(thetas_BpB);
y_BpB = k*sin(thetas_BpB);
pts_BpB = [x_BpB',y_BpB'];

circleCB = [G',r];
thetas_CB = [2.5133:-0.01:1.8489];
x_CB = u + r*cos(thetas_CB);
y_CB = r*sin(thetas_CB);

pts_BD = NE_inversion_all(B,C,circleCB,pts_BpB);
pts_BpDp = pts_BD;
pts_BpDp(:,1) = -pts_BpDp(:,1);

Dp = pts_BpDp(1,:)';
M = (Dp+C)/2;
dir = C - Dp;
dir = dir/norm(dir);
dir = [dir(2);-dir(1)];
M2 = M + dir;
lineM = [M',0; M2',0];
linex = [0,0,0; 1,0,0];
[int_pt,status] = cv_int_line_line(lineM,linex,0.0001);
I = int_pt(1:2)';
Ir = norm(Dp-I);
circleDpC = [I',Ir];
pts_DpE = NE_inversion_all(B,C,circleDpC,pts_BpDp);
pts_DE = pts_DpE;
pts_DE(:,1) = -pts_DE(:,1);

poly(1).pts = pts_BpB;
poly(2).pts = pts_BD;
poly(3).pts = pts_DE;
poly(4).pts = pts_DpE;
poly(5).pts = pts_BpDp;
poly(1).all_pts = [pts_BpB; pts_BD; pts_DE; pts_DpE; pts_BpDp];
poly(1).corners1 = [pts_BpB(end,1:2),pts_BpB(1,1:2),pts_DpE(1,1:2),...
    pts_DpE(end,1:2),pts_BD(1,1:2)];
c = poly(1).corners1;
c1 = c(1:2:end);
c2 = c(2:2:end);
poly(1).corners2 = [c1',c2'];

if b==0
    return
end

clf
hold on
axis equal
plot(pts_BpB(:,1),pts_BpB(:,2),'k.');
plot(pts_BD(:,1),pts_BD(:,2),'k.');
plot(pts_BpDp(:,1),pts_BpDp(:,2),'k.');
plot(pts_DE(:,1),pts_DE(:,2),'k.');
plot(pts_DpE(:,1),pts_DpE(:,2),'k.');

tch = 0;
