function [] = test_patch_v2 ()

  ncircumference = 500;
  nz = 200;

  phi = linspace (0, 2*pi (), ncircumference);
  cp = 4*cos (phi);
  sp = 4*sin (phi);
  h = [];
  px = [];
  py = [];
  pz = [];
  for i = 1:nz
     XS = [cp + 5; sp + 8; ones(1,length (phi)) + 2 * i];
     XE = [cp + 5; sp + 8; ones(1,length (phi)) + 2 * (i + 1)];
     [tx, ty, tz] = segment_patch_data (XS, XE);
     px = [px, tx];
     py = [py, ty];
     pz = [pz, tz];
  endfor

  [n, nfaces] = size (px);
  printf ("number of facets: %d x %d\n", n,nfaces);
  
  h = patch (px, py, pz, ones (size (px)));

  camlight ();
  lighting gouraud;

  view (3);
  axis equal;

endfunction

function [px, py, pz] = segment_patch_data (XS, XE)

  [~, npoly] = size (XS);
  px = zeros (4, npoly);
  py = zeros (4, npoly);
  pz = zeros (4, npoly);

  px(1, :) = XS(1, :);
  px(2, :) = XE(1, :);
  px(3, :) = [XE(1, 2:end), XE(1, 1)];
  px(4, :) = [XS(1, 2:end), XS(1, 1)];

  py(1, :) = XS(2, :);
  py(2, :) = XE(2, :);
  py(3, :) = [XE(2, 2:end), XE(2, 1)];
  py(4, :) = [XS(2, 2:end), XS(2, 1)];

  pz(1, :) = XS(3, :);
  pz(2, :) = XE(3, :);
  pz(3, :) = [XE(3, 2:end), XE(3, 1)];
  pz(4, :) = [XS(3, 2:end), XS(3, 1)];

endfunction
