﻿/* Programming by Greg Thatcher, http://www.GregThatcher.com */

    var itsStageWidth = 50;
    var itsStageHeight = 50;
    var itsPixelSize = 1;
    var itsDivCache;
    var itsStageDiv;
            
    function getElementID (x, y)
    {
        return "div" + y + "_" + x;    
    }

    function SetPixelSize (size)
    {
        itsPixelSize = size;
    }
    function InitializeStage ()
    {
        var parentDiv = document.getElementById("stage");
        parentDiv.style.width = itsStageWidth * itsPixelSize + "px";
        parentDiv.style.height = itsStageHeight * itsPixelSize + "px";
        itsStageDiv = parentDiv;
        
        
    }
    
    function InitializeNextDivRow (backgroundColor)
    {
        var parentDiv = document.getElementById("stage");
        var eDiv;
        var x,y;

        // find next uninitialized row        
        var nextRow = 0;
        for (nextRow = 0; nextRow < itsStageHeight; nextRow++)
        {
            if (itsDivCache[nextRow][0] == null)
            {
                break;
            }
        }        
        if (nextRow >= itsStageHeight || nextRow < 0)
        {
            return true;
        }
        y = nextRow;
        for (x = 0; x < itsStageWidth; x++)
        {
            eDiv=document.createElement("div");
            eDiv.id = getElementID(x, y);
            eDiv.style.width    =   itsPixelSize + "px";
            eDiv.style.height   =   itsPixelSize + "px";
            eDiv.style.position= "absolute";
            eDiv.style.backgroundColor=backgroundColor;
            eDiv.style.left= (x * itsPixelSize) + "px";
            eDiv.style.top = ((itsStageHeight-y-1) * itsPixelSize) + "px";
            parentDiv.appendChild(eDiv);
            itsDivCache[y][x] = eDiv;
        }
        return false;
    }

    function EraseStage(backgroundColor)
    {
        var x, y;
        var eDiv;
        
        for (y = 0; y < itsStageHeight; y++)
        {
            for (x = 0; x < itsStageWidth; x++)
            {
                eDiv = itsDivCache[y][x];
                eDiv.style.backgroundColor=backgroundColor;
            }
        }
    }
    
    function InitializeDivCache ()
    {

        itsDivCache = new Array(itsStageHeight);

        for (y = 0; y < itsStageHeight; y++)
        {
            itsDivCache[y] = new Array(itsStageWidth);
        }

    }
    function SetStageWidth(width)
    {
        itsStageWidth = width;
    }
    function GetStageWidth ()
    {
        return itsStageWidth;
    }
    function SetStageHeight(height)
    {
        itsStageHeight = height;
    }
    function GetStageHeight ()
    {
        return itsStageHeight;
    }
    
    function GetDivElementFromCache (column, row)
    {
        if (row >= itsStageHeight)
        {
            return null;
        }
        if (column >= itsStageWidth)
        {
            return null;
        }
        if (row < 0)
        {
            return null;
        }
        if (column < 0)
        {
            return null;
        }
        
        
        if ((itsDivCache[row] != null) && (itsDivCache [row][column] != null))
        {
            return itsDivCache[row][column];
        }
        return null;
    }
    // var elementOne = document.getElementById("div10_10");
    function PlotPixel(x, y, c) 
    {
        var element;
        // element = document.getElementById("div" + y + "_" + x);
        element = GetDivElementFromCache(x, y);
        // element = elementOne;
        if (element == null)
        {
            return;
        }
        var style = element.style;

        style.backgroundColor = c;
    }
   
    // Bresenham's Line Algorithm, from http://www.davidbetz.net/graphics/

    function DrawLine(x1, y1, x2, y2, c) 
    {
        var steep = Math.abs(y2 - y1) > Math.abs(x2 - x1);

        if (steep) 
        {
            t = y1;
            y1 = x1;
            x1 = t;
            t = y2;
            y2 = x2;
            x2 = t;
        }

        var deltaX = Math.abs(x2 - x1);
        var deltaY = Math.abs(y2 - y1);


        var error = 0;
        var deltaErr = deltaY;
        var xStep;
        var yStep;


        var x = x1;
        var y = y1;


        if (x1 < x2) 
        {
            xStep = 1;
        }
        else 
        {
            xStep = -1;
        }

 

        if(y1 < y2) 
        {
            yStep = 1;
        }
        else 
        {
            yStep = -1;
        }


        if(steep) 
        {
            PlotPixel(y, x, c);
        }
        else 
        {
            PlotPixel(x, y, c);
        }


        while(x != x2) 
        {
            x = x + xStep;
            error = error + deltaErr;

            if(2 * error >= deltaX) 
            {
                y = y + yStep;
                error = error - deltaX;
            }


            if(steep) 
            {
                PlotPixel(y, x, c);
            }
            else 
            {
                PlotPixel(x, y, c);
            }

        }

    }

    function DrawTriangle (triangle, borderColor)
    {
        DrawLine(triangle.vertices[0].x, triangle.vertices[0].y, triangle.vertices[1].x, triangle.vertices[1].y, borderColor) 
        DrawLine(triangle.vertices[1].x, triangle.vertices[1].y, triangle.vertices[2].x, triangle.vertices[2].y, borderColor) 
        DrawLine(triangle.vertices[2].x, triangle.vertices[2].y, triangle.vertices[0].x, triangle.vertices[0].y, borderColor) 
    }

    function DrawSegment (segment, borderColor)
    {
        DrawLine(segment.pt1.x, segment.pt1.y, segment.pt2.x, segment.pt2.y, borderColor) 
    }
    
    function FillTriangle (triangle, borderColor, fillColor)
    {
        
        var square = triangle.GetBoundingRectangle();
        var i, j;
        var iCount = 0;
        var iDone = 1500;
        for (j = square.y2; j >= square.y1 && iCount < iDone; j--)
        {
            for (i = square.x1; i <= square.x2 && iCount < iDone; i++)
            {
                var myPoint = new Point(i, j);
                if (triangle.PointInTriangle(myPoint))
                {
                    PlotPixel(i, j, fillColor);
                    //iCount++;
                }
            }
        }
        // DrawTriangle (triangle, borderColor);
    }