% transforms the lines to a standard position where the two component
% axes intersect at (0,0) and are aligned with the coordinate axes

function [newlines,flag] = standard_position(lines, num,figN)

  % get parallel line sets
 
  flag = 1;
  newlines = lines;
  numset1 = 1;
  numset2 = 0;
  set1 = zeros(4,4);
  set2 = zeros(4,4);
  set1(1,:) = lines(1,:);
  dirone = lines(1,1:2) - lines(1,3:4);
  dirone = dirone/norm(dirone);
  for j = 2 : 8
    dirtwo = lines(j,1:2) - lines(j,3:4);
    dirtwo = dirtwo/norm(dirtwo);
    cosang = dirone*dirtwo';
    if abs(cosang) < 0.7071
      numset2 = numset2 + 1;
      set2(numset2,:) = lines(j,:);
    else
      numset1 = numset1 + 1;
      set1(numset1,:) = lines(j,:);
    end
  
  end
  if numset1 ~= 4 | numset2 ~= 4
    flag=0;
    return
  end

  % assume that the Tee bar is the longest side in one set that has 3 other
  % shorter segments that total about the same length
  % also reorder the set to give the crossbar 1st, the 2 sidebars second and the foot last
  
  [flag,headset] = findheadset(set1,set2);
  if ~flag
    return
  end

  % get the transformation that maps the tee into place
  
  [tr,tc,theta] = findteetransform(headset);

  % apply the transformation
  
  newlines = zeros(8,4);
  R = [ [ cos(theta),-sin(theta)]',[sin(theta),cos(theta)]'];
  for i = 1 : 8
    v1 = [lines(i,1),lines(i,2)]';
    v2 = [lines(i,3),lines(i,4)]';
    tv1 = R*v1 + [tr,tc]';
    tv2 = R*v2 + [tr,tc]';
    newlines(i,:) = [tv1(1),tv1(2),tv2(1),tv2(2)];
  end

  % draw results
  if figN > 0
   figure(figN)
   clf
   axis ij
   hold on
   for i = 1 : num
     plot([newlines(i,2),newlines(i,4)],[newlines(i,1),newlines(i,3)],'*-')
   end
  end
  axis([-150, 150, -100, 250])

