等高線生成演算法

可健康了發表於2014-07-16

輸入:離散的取樣點座標和高度值(x_0,y_0,value_0),(x_1,y_1,value_1)......(x_n, y_n, value_n) 

輸出:等高線圖,如下所示

  

  

  wiki上的Marching squares演算法對此有很好的說明,我也是按照wiki上面的步驟來實現這個演算法的,下面對該演算法的步驟進行簡要說明。

輸入引數:

1、點的集合(x_0,y_0,value_0),(x_1,y_1,value_1)......(x_n, y_n, value_n) ;

2、高度值陣列,即每條等高線表示的值,以上圖為例是[0.0, 0.1, 0.2, .... 1.0]共10個區間,9個等高線值;

3、顏色陣列,必須與高度值陣列等長。

步驟:

1、構造矩陣

計算出輸入點的x,y範圍,即找到xmin, ymin, xmax, ymax,形成一個rectangle,然後擬定一個step,形成一個空的矩陣,step越小,等高線越平滑,計算速度也越慢。

2、矩陣插值

由輸入點的值確定矩陣上每一個元素的值,這樣一來,稀疏的輸入點就變成了一個稠密的矩陣。一種簡單的插值方式是:

matrix(x,y) = Sum(1/dist[i]^2 * value[i]) / Sum(1/dist[i]^2),其中dist[i]表示第i個點到(x,y)的歐式距離。

3、畫等高線

到這裡就可以完全按照wiki上的Marching squares來進行了。Marching squares演算法把等高線繪製分成了兩種情況,其一是隻畫線不上色,即isoLine演算法,這種比較簡單,不做討論了;其二是畫等高帶,即isoBand演算法,也就是本文采用的方法,如下說明:

對於每一個等高帶,從高度陣列中取出相臨的兩個閾值(如0.0, 0.1),從顏色陣列中取一個顏色,用兩個閾值把矩陣三值化成為一個只包含0,1,2的矩陣。0表示小於矩陣元素小於這兩個閾值,1表示介於其中間,2表示大於這兩個閾值。

這時,你的矩陣中就只有(0,1,2)三個值了,然後考慮矩陣中的每四個元素組成的單位正方形的狀態,該狀態由其四個角的元素值唯一確定,共3^4 = 81種,然後根據這81種情況分類討論,分別上色了,此處請參考wiki的marching square頁面。

如此,第一種顏色的等高帶就畫完了,把第3步的內容重複10次,畫完所有的等高線。

 

補充一段第3步的actionscript程式碼,是我按照wiki的步驟實現的

 

public function isoband(data:qyMatrix, thArray:Array, 
    dstLayer:GraphicsLayer, extent:Extent):Vector.<Polygon> 
{ 
    var x:int, y:int, k:int; 
    var count:int=0; 
    var squareWidth:Number = extent.width / (data.width - 1); 
    var squareHeight:Number = extent.height / (data.height - 1); 
    var dstPolygonVec:Vector.<Polygon> = new Vector.<Polygon>(thArray.length - 1); 

                // 等值線的每一個閾值 
    for (k=0; k<thArray.length - 1; k++){ 

        // 3值化 
        var stateMat:qyMatrix = new qyMatrix(data.width, data.height); 
        for (y=0; y<data.height; y++){ 
            for (x=0; x<data.width; x++){ 
                if (data.getData(x, y) < thArray[k]){ 
                    stateMat.setData(x, y, 0); 
                } 
                else{ 
                    if (data.getData(x, y) < thArray[k+1]){ 
                        stateMat.setData(x, y, 1); 
                    } 
                    else{ 
                        stateMat.setData(x, y, 2); 
                    } 
                } 
            } 
        } 
        
        var x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number, x4:Number, y4:Number; 
        var polygon:Polygon = new Polygon; 
        
        for (y=0; y<data.height - 1; y++){ 

            var ymin:Number = extent.ymin + y / (data.height - 1) * extent.height; 
            var ymax:Number = extent.ymin + (y+1) / (data.height - 1) * extent.height; 

            for (x=0; x<data.width - 1; x++){ 

                var xmin:Number = extent.xmin + x / (data.width - 1) * extent.width; 
                var xmax:Number = extent.xmin + (x+1) / (data.width - 1) * extent.width; 

                // square 四角座標 
                var p7:MapPoint = new MapPoint (xmin, ymin); 
                var p9:MapPoint = new MapPoint (xmax, ymin); 
                var p3:MapPoint = new MapPoint (xmax, ymax); 
                var p1:MapPoint = new MapPoint (xmin, ymax); 
                
                // square 四角數值 
                var d7:Number = data.getData(x, y); 
                var d9:Number = data.getData(x+1, y); 
                var d3:Number = data.getData(x+1, y+1); 
                var d1:Number = data.getData(x, y+1); 
                var mid:Number; 
                
                // isoband的頂點座標 
                var pt1:MapPoint = null; 
                var pt2:MapPoint = null; 
                var pt3:MapPoint = null; 
                var pt4:MapPoint = null; 
                var pt5:MapPoint = null; 
                var pt6:MapPoint = null; 
                var pt7:MapPoint = null; 
                var pt8:MapPoint = null; 
                
                var squareState:String = getSquareState(stateMat, x, y); 
                switch (squareState)        // total 81 cases 
                { 
                   // no color 
                    case "2222": 
                    case "0000": 
                    break; 
                    // square 
                    case "1111": 
                    polygon.addRing([p7, p9, p3, p1]); 
                    break; 
                    // triangle                8 cases 
                    case "2221": 
                    x1 = p1.x + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    y2 = p1.y - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, p1.y); 
                    pt2 = new MapPoint(p7.x, y2); 
                    polygon.addRing([pt1, p1, pt2]); 
                    break; 
                    case "2212": 
                    y1 = p3.y - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    x2 = p3.x - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(p9.x, y1); 
                    pt2 = new MapPoint(x2, p1.y); 
                    polygon.addRing([pt1, p3, pt2]); 
                    break; 
                    case "2122": 
                    x1 = p9.x - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    y2 = p9.y + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    pt1 = new MapPoint(x1, p7.y); 
                    pt2 = new MapPoint(p9.x, y2) 
                    polygon.addRing([pt1, p9, pt2]); 
                    break; 
                    case "1222": 
                    x1 = p7.x + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y2 = p7.y + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, p7.y); 
                    pt2 = new MapPoint(p7.x, y2); 
                    polygon.addRing([p7, pt1, pt2]); 
                    break; 
                    case "0001": 
                    x1 = p3.x - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    y2 = p7.y + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, p1.y); 
                    pt2 = new MapPoint(p7.x, y2); 
                    polygon.addRing([pt1, p1, pt2]); 
                    break; 
                    case "0010": 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    x2 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(x2, ymax); 
                    polygon.addRing([pt1, p3, pt2]); 
                    break; 
                    case "0100": 
                    x1 = p7.x + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    y2 = p3.y - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    pt1 = new MapPoint(x1, p9.y); 
                    pt2 = new MapPoint(p9.x, y2); 
                    polygon.addRing([pt1, p9, pt2]); 
                    break; 
                    case "1000": 
                    x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmin, y2); 
                    polygon.addRing([p7, pt1, pt2]); 
                    break; 
                    // trapezoid        8 cases 
                    case "2220": 
                    x1 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x2 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y1 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y2 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, p1.y); 
                    pt2 = new MapPoint(x2, p1.y); 
                    pt3 = new MapPoint(p7.x, y1); 
                    pt4 = new MapPoint(p7.x, y2); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "2202": 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x1 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x2 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(xmax, y2); 
                    pt3 = new MapPoint(x1, ymax); 
                    pt4 = new MapPoint(x2, ymax); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "2022": 
                    x1 = p9.x - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = p9.x - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = p9.y + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = p9.y + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    pt1 = new MapPoint(x1, p9.y); 
                    pt2 = new MapPoint(x2, p9.y); 
                    pt3 = new MapPoint(p9.x ,y1); 
                    pt4 = new MapPoint(p9.x, y2); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "0222": 
                    x1 = p7.x + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = p7.x + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = p7.y + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y2 = p7.y + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, p7.y); 
                    pt2 = new MapPoint(x2, p7.y); 
                    pt3 = new MapPoint(p7.x, y1); 
                    pt4 = new MapPoint(p7.x, y2); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "0002": 
                    x1 = p3.x - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x2 = p3.x - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y1 = p7.y + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y2 = p7.y + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, p1.y); 
                    pt2 = new MapPoint(x2, p1.y); 
                    pt3 = new MapPoint(p1.x, y1); 
                    pt4 = new MapPoint(p1.x, y2); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "0020": 
                    y1 = p9.y + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = p9.y + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x1 = p1.x + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x2 = p1.x + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(p3.x, y1); 
                    pt2 = new MapPoint(p3.x, y2); 
                    pt3 = new MapPoint(x1, p3.y); 
                    pt4 = new MapPoint(x2, p3.y); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "0200": 
                    x1 = p7.x + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = p7.x + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = p3.y - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = p3.y - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    pt1 = new MapPoint(x1, p9.y); 
                    pt2 = new MapPoint(x2, p9.y); 
                    pt3 = new MapPoint(p9.x, y1); 
                    pt4 = new MapPoint(p9.x, y2); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    
                    break; 
                    case "2000": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y2 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmin, y1); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    // rectangle 12 cases 
                    case "0011": 
                    y1 = p9.y + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = p7.y + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(p9.x, y1); 
                    pt2 = new MapPoint(p7.x, y2); 
                    polygon.addRing([pt1, p3, p1, pt2]); 
                    break; 
                    case "0110": 
                    x1 = p7.x + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = p1.x + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(x1, p7.y); 
                    pt2 = new MapPoint(x2, p1.y); 
                    polygon.addRing([pt1, p9, p3, pt2]); 
                    break; 
                    case "1100": 
                    y1 = p3.y - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    y2 = p1.y - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(p9.x, y1); 
                    pt2 = new MapPoint(p7.x, y2); 
                    polygon.addRing([p7, p9, pt1, pt2]); 
                    break; 
                    case "1001": 
                    x1 = p9.x - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    x2 = p3.x - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(x1, p7.y); 
                    pt2 = new MapPoint(x2, p1.y); 
                    polygon.addRing([p7, pt1, pt2, p1]); 
                    break; 
                    case "2211": 
                    y1 = p3.y - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = p1.y - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, p3, p1, pt2]); 
                    break; 
                    case "2112": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymax); 
                    polygon.addRing([pt1, p9, p3, pt2]); 
                    break; 
                    case "1122": 
                    y1 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareWidth; 
                    y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareWidth; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(xmin, y2); 
                    polygon.addRing([p7, p9, pt1, pt2]); 
                    break; 
                    case "1221": 
                    x1 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymax); 
                    polygon.addRing([p7, pt1, pt2, p1]); 
                    break; 
                    case "2200": 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    y3 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y4 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(xmax, y2); 
                    pt3 = new MapPoint(xmin, y3); 
                    pt4 = new MapPoint(xmin, y4); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "2002": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    x3 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x4 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(x3, ymax); 
                    pt4 = new MapPoint(x4, ymax); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "0022": 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    y3 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y4 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(xmax, y2); 
                    pt3 = new MapPoint(xmin, y3); 
                    pt4 = new MapPoint(xmin, y4); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    case "0220": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    x3 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x4 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(x3, ymax); 
                    pt4 = new MapPoint(x4, ymax); 
                    polygon.addRing([pt1, pt2, pt3, pt4]); 
                    break; 
                    // hexagon 12 cases 
                    case "0211": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, pt2, pt3, p3, p1, pt4]); 
                    break; 
                    case "2110": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y1 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y2 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymax); 
                    pt3 = new MapPoint(xmin, y1); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, p9, p3, pt2, pt3, pt4]); 
                    break; 
                    case "1102": 
                    y1 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x1 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x2 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(x1, ymax); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([p7, p9, pt1, pt2, pt3, pt4]); 
                    break; 
                    case "1021": 
                    x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(xmax, y2); 
                    pt4 = new MapPoint(x2, ymax); 
                    polygon.addRing([p7, pt1, pt2, pt3, pt4, p1]); 
                    break; 
                    case "2011": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, pt2, pt3, p3, p1, pt4]); 
                    break; 
                    case "0112": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y1 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y2 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymax); 
                    pt3 = new MapPoint(xmin, y1); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, p9, p3, pt2, pt3, pt4]); 
                    break; 
                    case "1120": 
                    y1 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x1 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x2 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(x1, ymax); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([p7, p9, pt1, pt2, pt3, pt4]); 
                    break; 
                    case "1201": 
                    x1 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x2 = xmax -  (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(xmax, y2); 
                    pt4 = new MapPoint(x2, ymax); 
                    polygon.addRing([p7, pt1, pt2, pt3, pt4, p1]); 
                    break; 
                    case "2101": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x2 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    y2 = ymax -  (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, p9, pt2, pt3, p1, pt4]); 
                    break; 
                    case "0121": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    y2 = ymin + (thArray[k] - d7)        / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, p9, pt2, pt3, p1, pt4]); 
                    break; 
                    case "1012": 
                    x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    x2 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([p7, pt1, pt2, p3, pt3, pt4]); 
                    break; 
                    case "1210": 
                    x1 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    x2 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(xmin, y2); 
                    polygon.addRing([p7, pt1, pt2, p3, pt3, pt4]); 
                    break; 
                    // pentagon        24 cases 
                    case "1211": 
                    x1 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y2 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y2); 
                    polygon.addRing([p7, pt1, pt2, p3, p1]); 
                    break; 
                    case "2111": 
                    x1 = xmax -  (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    y2 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, p9, p3, p1, pt2]); 
                    break; 
                    case "1112": 
                    x1 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymax); 
                    pt2 = new MapPoint(xmin, y2); 
                    polygon.addRing([p7, p9, p3, pt1, pt2]); 
                    break; 
                    case "1121": 
                    y1 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(x2, ymax); 
                    polygon.addRing([p7, p9, pt1, pt2, p1]); 
                    break; 
                    case "1011": 
                    x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y2 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y2); 
                    polygon.addRing([p7, pt1, pt2, p3, p1]); 
                    break; 
                    case "0111": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    y2 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmin, y2); 
                    polygon.addRing([pt1, p9, p3, p1, pt2]); 
                    break; 
                    case "1110": 
                    x1 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymax); 
                    pt2 = new MapPoint(xmin, y2); 
                    polygon.addRing([p7, p9, p3, pt1, pt2]); 
                    break; 
                    case "1101": 
                    y1 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x2 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(x2, ymax); 
                    polygon.addRing([p7, p9, pt1, pt2, p1]); 
                    break; 
                    case "1200": 
                    x1 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    y3 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(xmax, y2); 
                    pt4 = new MapPoint(xmin, y3); 
                    polygon.addRing([p7, pt1, pt2, pt3, pt4]); 
                    break; 
                    case "0120": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x3 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(x3, ymax); 
                    polygon.addRing([pt1, p9, pt2, pt3, pt4]); 
                    break; 
                    case "0012": 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    x1 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y3 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(x1, ymax); 
                    pt3 = new MapPoint(xmin, y2); 
                    pt4 = new MapPoint(xmin, y3); 
                    polygon.addRing([pt1, p3, pt2, pt3, pt4]); 
                    break; 
                    case "2001": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    x3 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    y1 = ymax -  (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(x3, ymax); 
                    pt4 = new MapPoint(xmin, y1); 
                    polygon.addRing([pt1, pt2, pt3, p1, pt4]); 
                    break; 
                    case "1022": 
                    x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    y3 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(xmax, y2); 
                    pt4 = new MapPoint(xmin, y3); 
                    polygon.addRing([p7, pt1, pt2, pt3, pt4]); 
                    break; 
                    case "2102": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x2 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x3 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(x3, ymax); 
                    polygon.addRing([pt1, p9, pt2, pt3, pt4]); 
                    break; 
                    case "2210": 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    x1 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y3 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(x1, ymax); 
                    pt3 = new MapPoint(xmin, y2); 
                    pt4 = new MapPoint(xmin, y3); 
                    polygon.addRing([pt1, p3, pt2, pt3, pt4]); 
                    break; 
                    case "0221": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    x3 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    y1 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(x3, ymax); 
                    pt4 = new MapPoint(xmin, y1); 
                    polygon.addRing([pt1, pt2, pt3, p1, pt4]); 
                    break; 
                    case "1002": 
                    x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x3 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y1 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymax); 
                    pt3 = new MapPoint(x3, ymax); 
                    pt4 = new MapPoint(xmin, y1); 
                    polygon.addRing([p7, pt1, pt2, pt3, pt4]); 
                    break; 
                    case "2100": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y3 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(xmin, y2); 
                    pt4 = new MapPoint(xmin, y3); 
                    polygon.addRing([pt1, p9, pt2, pt3, pt4]); 
                    break; 
                    case "0210": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    x3 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(x3, ymax); 
                    polygon.addRing([pt1, pt2, pt3, p3, pt4]); 
                    break; 
                    case "0021": 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x1 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    y3 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(xmax, y2); 
                    pt3 = new MapPoint(x1, ymax); 
                    pt4 = new MapPoint(xmin, y3); 
                    polygon.addRing([pt1, pt2, pt3, p1, pt4]); 
                    break; 
                    case "1220": 
                    x1 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x3 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y1 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymax); 
                    pt3 = new MapPoint(x3, ymax); 
                    pt4 = new MapPoint(xmin, y1); 
                    polygon.addRing([p7, pt1, pt2, pt3, pt4]); 
                    break; 
                    case "0122": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y3 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(xmin, y2); 
                    pt4 = new MapPoint(xmin, y3); 
                    polygon.addRing([pt1, p9, pt2, pt3, pt4]); 
                    break; 
                    case "2012": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    x3 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(x3, ymax); 
                    polygon.addRing([pt1, pt2, pt3, p3, pt4]); 
                    break; 
                    case "2201": 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x1 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    y3 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(xmax, y1); 
                    pt2 = new MapPoint(xmax, y2); 
                    pt3 = new MapPoint(x1, ymax); 
                    pt4 = new MapPoint(xmin, y3); 
                    polygon.addRing([pt1, pt2, pt3, p1, pt4]); 
                    break; 
                    // saddles - 8 sided        2 cases 
                    case "2020": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x3 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x4 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y3 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y4 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(xmax, y2); 
                    pt5 = new MapPoint(x3, ymax); 
                    pt6 = new MapPoint(x4, ymax); 
                    pt7 = new MapPoint(xmin, y3); 
                    pt8 = new MapPoint(xmin, y4); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k]){ 
                        polygon.addRing([pt1, pt2, pt7, pt8]); 
                        polygon.addRing([pt3, pt4, pt5, pt6]); 
                    } 
                    else if (mid < thArray[k+1]){ 
                        polygon.addRing([pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8]); 
                    } 
                    else{ 
                        polygon.addRing([pt1, pt2, pt3, pt4]); 
                        polygon.addRing([pt5, pt6, pt7, pt8]); 
                    } 
                    break; 
                    case "0202": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x3 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x4 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y3 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y4 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(xmax, y2); 
                    pt5 = new MapPoint(x3, ymax); 
                    pt6 = new MapPoint(x4, ymax); 
                    pt7 = new MapPoint(xmin, y3); 
                    pt8 = new MapPoint(xmin, y4); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k]){ 
                        polygon.addRing([pt1, pt2, pt3, pt4]); 
                        polygon.addRing([pt5, pt6, pt7, pt8]); 
                    } 
                    else if (mid < thArray[k+1]){ 
                        polygon.addRing([pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8]); 
                    } 
                    else{ 
                        polygon.addRing([pt1, pt2, pt7, pt8]); 
                        polygon.addRing([pt3, pt4, pt5, pt6]); 
                    } 
                    break; 
                    // saddles:        6 sided                4 cases 
                    case "0101": 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k]){ 
                        x1 = p7.x + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                        y2 = p3.y - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                        pt1 = new MapPoint(x1, p9.y); 
                        pt2 = new MapPoint(p9.x, y2); 
                        polygon.addRing([pt1, p9, pt2]); 

                        x1 = p3.x - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                        y2 = p7.y + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                        pt1 = new MapPoint(x1, p1.y); 
                        pt2 = new MapPoint(p7.x, y2); 
                        polygon.addRing([pt1, p1, pt2]); 
                    } 
                    else{ 
                        x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                        y1 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                        x2 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                        y2 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                        pt1 = new MapPoint(x1, ymin); 
                        pt2 = new MapPoint(xmax, y1); 
                        pt3 = new MapPoint(x2, ymax); 
                        pt4 = new MapPoint(xmin, y2); 
                        polygon.addRing([pt1, p9, pt2, pt3, p1, pt4]); 
                    } 
                    break; 
                    case "1010": 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k]){ 
                        x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                        y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                        pt1 = new MapPoint(x1, ymin); 
                        pt2 = new MapPoint(xmin, y2); 
                        polygon.addRing([p7, pt1, pt2]); 

                        y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                        x2 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                        pt1 = new MapPoint(p9.x, y1); 
                        pt2 = new MapPoint(x2, p1.y); 
                        polygon.addRing([pt1, p3, pt2]); 
                    } 
                    else{ 
                        x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                        y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                        x2 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                        y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                        pt1 = new MapPoint(x1, ymin); 
                        pt2 = new MapPoint(xmax, y1); 
                        pt3 = new MapPoint(x2, ymax); 
                        pt4 = new MapPoint(xmin, y2); 
                        polygon.addRing([p7, pt1, pt2, p3, pt3, pt4]); 
                    } 
                    break; 
                    case "2121": 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k+1]){ 
                        x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                        y1 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                        x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                        y2 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                        pt1 = new MapPoint(x1, ymin); 
                        pt2 = new MapPoint(xmax, y1); 
                        pt3 = new MapPoint(x2, ymax); 
                        pt4 = new MapPoint(xmin, y2); 
                        polygon.addRing([pt1, p9, pt2, pt3, p1, pt4]); 
                    } 
                    else{ 
                        x1 = p9.x - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                        y2 = p9.y + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                        pt1 = new MapPoint(x1, p7.y); 
                        pt2 = new MapPoint(p9.x, y2) 
                        polygon.addRing([pt1, p9, pt2]); 

                        x1 = p1.x + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                        y2 = p1.y - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                        pt1 = new MapPoint(x1, p1.y); 
                        pt2 = new MapPoint(p7.x, y2); 
                        polygon.addRing([pt1, p1, pt2]); 
                    } 
                    break; 
                    case "1212": 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k+1]){ 
                        x1 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                        y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                        x2 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                        y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                        pt1 = new MapPoint(x1, ymin); 
                        pt2 = new MapPoint(xmax, y1); 
                        pt3 = new MapPoint(x2, ymax); 
                        pt4 = new MapPoint(xmin, y2); 
                        polygon.addRing([p7, pt1, pt2, p3, pt3, pt4]); 
                    } 
                    else{ 
                        x1 = p7.x + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                        y2 = p7.y + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                        pt1 = new MapPoint(x1, p7.y); 
                        pt2 = new MapPoint(p7.x, y2); 
                        polygon.addRing([p7, pt1, pt2]); 
                        y1 = p3.y - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                        x2 = p3.x - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                        pt1 = new MapPoint(p9.x, y1); 
                        pt2 = new MapPoint(x2, p1.y); 
                        polygon.addRing([pt1, p3, pt2]); 
                    } 
                    break; 
                    // saddles 7 sided                8 cases 
                    case "2120": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x3 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y3 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(x3, ymax); 
                    pt5 = new MapPoint(xmin, y2); 
                    pt6 = new MapPoint(xmin, y3); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k+1]){ 
                        polygon.addRing([pt1, p9, pt2, pt3, pt4, pt5, pt6]); 
                    } 
                    else{ 
                        polygon.addRing([pt1, p9, pt2]); 
                        polygon.addRing([pt3, pt4, pt5, pt6]); 
                    } 
                    break; 
                    case "2021": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x3 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    y3 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(xmax, y2); 
                    pt5 = new MapPoint(x3, ymax); 
                    pt6 = new MapPoint(xmin, y3); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k+1]){ 
                        polygon.addRing([pt1, pt2, pt3, pt4, pt5, p1, pt6]); 
                    } 
                    else{ 
                        polygon.addRing([pt1, pt2, pt3, pt4]); 
                        polygon.addRing([pt5, p1, pt6]); 
                    } 
                    break; 
                    case "1202": 
                    x1 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x2 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x3 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y3 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(xmax, y2); 
                    pt4 = new MapPoint(x2, ymax); 
                    pt5 = new MapPoint(x3, ymax); 
                    pt6 = new MapPoint(xmin, y3); 

                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k+1]){ 
                        polygon.addRing([p7, pt1, pt2, pt3, pt4, pt5, pt6]); 
                    } 
                    else{ 
                        polygon.addRing([p7, pt1, pt6]); 
                        polygon.addRing([pt2, pt3, pt4, pt5]); 
                    } 
                    break; 
                    case "0212": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    x3 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y3 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(x3, ymax); 
                    pt5 = new MapPoint(xmin, y2); 
                    pt6 = new MapPoint(xmin, y3); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k+1]){ 
                        polygon.addRing([pt1, pt2, pt3, p3, pt4, pt5, pt6]); 
                    } 
                    else{ 
                        polygon.addRing([pt1, pt2, pt5, pt6]); 
                        polygon.addRing([pt3, p3, pt4]); 
                    } 
                    break; 
                    case "0102": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x2 = xmax - (thArray[k] - d3) / (d1 - d3) * squareWidth; 
                    x3 = xmax - (thArray[k+1] - d3) / (d1 - d3) * squareWidth; 
                    y2 = ymin + (thArray[k+1] - d7) / (d1 - d7) * squareHeight; 
                    y3 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(x2, ymax); 
                    pt4 = new MapPoint(x3, ymax); 
                    pt5 = new MapPoint(xmin, y2); 
                    pt6 = new MapPoint(xmin, y3); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k]){ 
                        polygon.addRing([pt1, p9, pt2]); 
                        polygon.addRing([pt3, pt4, pt5, pt6]); 
                    } 
                    else{ 
                        polygon.addRing([pt1, p9, pt2, pt3, pt4, pt5, pt6]); 
                    } 
                    break; 
                    case "0201": 
                    x1 = xmin + (thArray[k] - d7) / (d9 - d7) * squareWidth; 
                    x2 = xmin + (thArray[k+1] - d7) / (d9 - d7) * squareWidth; 
                    y1 = ymax - (thArray[k+1] - d3) / (d9 - d3) * squareHeight; 
                    y2 = ymax - (thArray[k] - d3) / (d9 - d3) * squareHeight; 
                    x3 = xmax - (thArray[k] - d3) / (d1- d3) * squareWidth; 
                    y3 = ymin + (thArray[k] - d7) / (d1 - d7) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(xmax, y2); 
                    pt5 = new MapPoint(x3, ymax); 
                    pt6 = new MapPoint(xmin, y3); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k]){ 
                        polygon.addRing([pt1, pt2, pt3, pt4]); 
                        polygon.addRing([pt5, p1, pt6]); 
                    } 
                    else{ 
                        polygon.addRing([pt1, pt2, pt3, pt4, pt5, p1, pt6]); 
                    } 
                    break; 
                    case "1020": 
                    x1 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    y2 = ymin + (thArray[k+1] - d9) / (d3 - d9) * squareHeight; 
                    x2 = xmin + (thArray[k+1] - d1) / (d3 - d1) * squareWidth; 
                    x3 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y3 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(xmax, y1); 
                    pt3 = new MapPoint(xmax, y2); 
                    pt4 = new MapPoint(x2, ymax); 
                    pt5 = new MapPoint(x3, ymax); 
                    pt6 = new MapPoint(xmin, y3); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k]){ 
                        polygon.addRing([p7, pt1, pt6]); 
                        polygon.addRing([pt2, pt3, pt4, pt5,]); 
                    } 
                    else{ 
                        polygon.addRing([p7, pt1, pt2, pt3, pt4, pt5, pt6]); 
                    } 
                    break; 
                    case "2010": 
                    x1 = xmax - (thArray[k+1] - d9) / (d7 - d9) * squareWidth; 
                    x2 = xmax - (thArray[k] - d9) / (d7 - d9) * squareWidth; 
                    y1 = ymin + (thArray[k] - d9) / (d3 - d9) * squareHeight; 
                    x3 = xmin + (thArray[k] - d1) / (d3 - d1) * squareWidth; 
                    y2 = ymax - (thArray[k] - d1) / (d7 - d1) * squareHeight; 
                    y3 = ymax - (thArray[k+1] - d1) / (d7 - d1) * squareHeight; 
                    pt1 = new MapPoint(x1, ymin); 
                    pt2 = new MapPoint(x2, ymin); 
                    pt3 = new MapPoint(xmax, y1); 
                    pt4 = new MapPoint(x3, ymax); 
                    pt5 = new MapPoint(xmin, y2); 
                    pt6 = new MapPoint(xmin, y3); 
                    mid = (d7 + d9 + d3 + d1) / 4; 
                    if (mid < thArray[k]){ 
                        polygon.addRing([pt1, pt2, pt5, pt6]); 
                        polygon.addRing([pt3, p3, pt4]); 
                    } 
                    else{ 
                        polygon.addRing([pt1, pt2, pt3, p3, pt4, pt5, pt6]); 
                    } 
                    break; 
                    default: 
                    break; 
                } 
            } 
        } 
        dstPolygonVec[k] = polygon; 
        //DrawToolBox.qyPolygon(dstLayer, polygon, 0, 0, 0.8, colorArray[k]); 
    } 
    return dstPolygonVec; 

    function getSquareState(mat:qyMatrix, x:int, y:int):String 
    { 
        var str:String = ''; 
        str += String(mat.getData(x, y)); 
        str += String(mat.getData(x + 1, y)); 
        str += String(mat.getData(x + 1, y + 1)); 
        str += String(mat.getData(x, y + 1)); 
        return str; 
    } 
}
View Code

 

相關文章