Don't like ads? PRO users don't see any ads ;-)

Untitled

By: stoneharry on Aug 22nd, 2012  |  syntax: Lua  |  size: 2.08 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. TAXIROUTE_LINEFACTOR = 32/30; -- Multiplying factor for texture coordinates
  2. TAXIROUTE_LINEFACTOR_2 = TAXIROUTE_LINEFACTOR / 2; -- Half o that
  3.  
  4. -- T        - Texture
  5. -- C        - Canvas Frame (for anchoring)
  6. -- sx,sy    - Coordinate of start of line
  7. -- ex,ey    - Coordinate of end of line
  8. -- w        - Width of line
  9. -- relPoint - Relative point on canvas to interpret coords (Default BOTTOMLEFT)
  10. function DrawRouteLine(T, C, sx, sy, ex, ey, w, relPoint)
  11.    if (not relPoint) then relPoint = "BOTTOMLEFT"; end
  12.  
  13.    -- Determine dimensions and center point of line
  14.    local dx,dy = ex - sx, ey - sy;
  15.    local cx,cy = (sx + ex) / 2, (sy + ey) / 2;
  16.  
  17.    -- Normalize direction if necessary
  18.    if (dx < 0) then
  19.       dx,dy = -dx,-dy;
  20.    end
  21.  
  22.    -- Calculate actual length of line
  23.    local l = sqrt((dx * dx) + (dy * dy));
  24.  
  25.    -- Quick escape if it's zero length
  26.    if (l == 0) then
  27.       T:SetTexCoord(0,0,0,0,0,0,0,0);
  28.       T:SetPoint("BOTTOMLEFT", C, relPoint, cx,cy);
  29.       T:SetPoint("TOPRIGHT",   C, relPoint, cx,cy);
  30.       return;
  31.    end
  32.  
  33.    -- Sin and Cosine of rotation, and combination (for later)
  34.    local s,c = -dy / l, dx / l;
  35.    local sc = s * c;
  36.  
  37.    -- Calculate bounding box size and texture coordinates
  38.    local Bwid, Bhgt, BLx, BLy, TLx, TLy, TRx, TRy, BRx, BRy;
  39.    if (dy >= 0) then
  40.       Bwid = ((l * c) - (w * s)) * TAXIROUTE_LINEFACTOR_2;
  41.       Bhgt = ((w * c) - (l * s)) * TAXIROUTE_LINEFACTOR_2;
  42.       BLx, BLy, BRy = (w / l) * sc, s * s, (l / w) * sc;
  43.       BRx, TLx, TLy, TRx = 1 - BLy, BLy, 1 - BRy, 1 - BLx;
  44.       TRy = BRx;
  45.    else
  46.       Bwid = ((l * c) + (w * s)) * TAXIROUTE_LINEFACTOR_2;
  47.       Bhgt = ((w * c) + (l * s)) * TAXIROUTE_LINEFACTOR_2;
  48.       BLx, BLy, BRx = s * s, -(l / w) * sc, 1 + (w / l) * sc;
  49.       BRy, TLx, TLy, TRy = BLx, 1 - BRx, 1 - BLx, 1 - BLy;
  50.       TRx = TLy;
  51.    end
  52.  
  53.    -- Set texture coordinates and anchors
  54.    T:ClearAllPoints();
  55.    T:SetTexCoord(TLx, TLy, BLx, BLy, TRx, TRy, BRx, BRy);
  56.    T:SetPoint("BOTTOMLEFT", C, relPoint, cx - Bwid, cy - Bhgt);
  57.    T:SetPoint("TOPRIGHT",   C, relPoint, cx + Bwid, cy + Bhgt);
  58. end