var c_addzindex = 2000;

function objArea(l,t,w,h)
{
  this.l = l;
  this.t = t;
  this.w = w;
  this.h = h;
}

function objPoint(x,y)
{
  this.x = x;
  this.y = y;
}

function objPoints(oc,meterperpixel,bpoly)
{
  this.canvas = oc;
  this.name = null;
  this.list = new Array();
  this.bpoly = bpoly;
  this.mpp = meterperpixel;
  this.dirty = true;
  this.destroy  = objPoints_destroy;
  this.addLine  = objPoints_addLine;
  this.undoLine = objPoints_addLineUndo;
  this.setVisible = objPoints_setVisible;
  this.redraw   = objPoints_redraw;
  this.calcDistance = objPoints_calcDistance;
  this.calcAreaSize = objPoints_calcAreaSize;
  this.isCrossed    = objPoints_isCrossed;
  
  if(bpoly)
  {
    this.putHere  = objPoints_putHere_poly;
    this.calc     = objPoints_calcAreaSize;
  }
  else
  {
    this.putHere  = objPoints_putHere_line;
    this.calc     = objPoints_calcDistance;
  }
}

function objPoints_putHere_line(name)
{
  if (! this.name && name)
  {
    var oc = this.canvas;
    this.name = name;
    document.write( '<SPAN ID="'+name+'" style="position:absolute; width:'+oc.w+'; height:'+oc.h+'; left:'+oc.l+'; top:'+oc.t+'; visibility:visible; z-index:'+(c_addzindex+getNextZIndex())+';">' );
    // empty
    document.write( '</SPAN>' );
    document.write( '\n' );
  }
}
function objPoints_putHere_poly(name)
{
  if (! this.name && name)
  {
    var oc = this.canvas;
    this.name = name;
    document.write( '<SPAN ID="'+name+'" style="position:absolute; width:'+oc.w+'; height:'+oc.h+'; left:'+oc.l+'; top:'+oc.t+'; visibility:visible; z-index:'+(c_addzindex+getNextZIndex())+';">' );
    document.write( '<v:shape id="'+name+'_poly" style="width:'+oc.w+'; height:'+oc.h+';"' );
    document.write( ' strokecolor="yellow" strokeweight="2"' );
    document.write( ' coordsize="'+oc.w+' '+oc.h+'" coordorigin="0 0"' );
    document.write( ' path="" >' );
    document.write( '<v:fill id="'+name+'_fill" opacity="0.1" />' );
    document.write( '<v:stroke id="'+name+'_stroke" joinstyle="round" />' );
    document.write( '</v:shape>' );
    document.write( '</SPAN>' );
    document.write( '\n' );
  }
}

function objPoints_setVisible(flg)
{
  var ob = document.all[ this.name ];
  setVisibleLAYER( ob.style, !!flg );
}
function objPoints_destroy()
{
  this.list = null;
  this.list = new Array();
  this.dirty = true;
}
function objPoints_addLine(x,y)
{
  var n = this.list.length;
  this.list[n] = new objPoint(x,y);
  this.dirty = true;
}
function objPoints_addLineUndo()
{
  var n = this.list.length;
  if (n > 0)
  {
    this.list[n-1] = null;
    this.list.length = n-1;
    this.dirty = true;
  }
}
function objPoints_redraw()
{
  if(this.dirty)
  {
    var ob = document.all[ this.name ];
    
    if (this.list.length > 1)
    {
      var p = '';
      
      for (var i=1; i<this.list.length; i++)
      {
        p += '<v:line'
           + ' from="'+ this.list[i-1].x +','+ this.list[i-1].y +'"'
           + ' to="'+ this.list[i].x +','+ this.list[i].y +'"'
           + ' strokecolor="yellow" strokeweight="2" />';
      }
      if (this.bpoly && this.list.length>2)
      {
        p += '<v:line id="'+this.name+'_poly_0"'
           + ' to="'+ this.list[this.list.length-1].x +','+ this.list[this.list.length-1].y +'"'
           + ' from="'+ this.list[0].x +','+ this.list[0].y +'"'
           + ' strokecolor="yellow" strokeweight="2" />';
      }
      this.setVisible( true );
      ob.innerHTML = p;
    }
    else
    {
      this.setVisible( false );
      ob.innerHTML = '';
    }
  }
  this.dirty = false;
}
// 計算:距離
function objPoints_calcDistance()
{
  var n = this.list.length-1;
  var r = 0.0;
  var dx,dy;
  for (var i=0; i<n; i++)
  {
    dx = Math.abs( this.list[i].x - this.list[i+1].x );
    dy = Math.abs( this.list[i].y - this.list[i+1].y );
    r += Math.sqrt( dx*dx + dy*dy );
  }
  return r * this.mpp;
}
// 計算:面積
function objPoints_calcAreaSize()
{
  var n = this.list.length;
  var r = 0.0;
  var lines = new Array();
  var p1 = new objPoint(0,0);
  var p2 = new objPoint(0,0);
  var p3 = new objPoint(0,0);
  var p4 = new objPoint(0,0);
  var id3 = 0;
  var id4 = 0;
  
  if (n < 3) return -1;
  
  for (var i=0; i<n; i++)
  {
    lines[i] = new objPoint( this.list[i].x, this.list[i].y );
  }
  lines[n] = new objPoint( this.list[0].x, this.list[0].y );
  n = lines.length;
  
  // ポリゴンチェック
  for (var i=0; i<n-2; i++)
  {
    if (lines[i].x < lines[i+1].x)
    {
      p1 = lines[i];
      p2 = lines[i+1];
    }
    else
    {
      p1 = lines[i+1];
      p2 = lines[i];
    }
    
    // 他の線分と交差しているかどうかチェック
    id3 = i+2;
    id4 = i+3;
    for (var j=0; j<n-3; j++)
    {
      if (id3 >= n) id3 -= n;
      if (id4 >= n) id4 -= n;
      
      if (lines[id3].x < lines[id4].x)
      {
        p3 = lines[id3];
        p4 = lines[id4];
      }
      else
      {
        p3 = lines[id4];
        p4 = lines[id3];
      }
      
      // 交差チェック
      if (this.isCrossed(p1,p2,p3,p4)) return -1;
      
      id3 ++;
      id4 ++;
    }
  }
  // 面積計算
  r = 0.0;
  for (var i=0; i<n-1; i++)
  {
    r += lines[i].y * lines[i+1].x;
    r -= lines[i].x * lines[i+1].y;
  }
  r = Math.abs(r/2);
  return r * this.mpp * this.mpp;
}
function objPoints_isCrossed(p1,p2,q1,q2)
{
  var c = (p2.x - p1.x) * (q2.y - q1.y) - (p2.y - p1.y) * (q2.x - q1.x);
  if (c == 0)  return false;
  
  var a = ((p2.x - p1.x) * (p1.y - q1.y) - (p2.y - p1.y) * (p1.x - q1.x)) / c;
  var b = ((q2.x - q1.x) * (p1.y - q1.y) - (q2.y - q1.y) * (p1.x - q1.x)) / c;
  
  return (0<a && a<1 && 0<b && b<1)? true: false;
}
