1

経度と緯度を取り、それを x と y に変換してプロットする関数があります。X と Y への変換は正常に機能していますが、それは私の問題ではありません。

2 つの点が同じ場所にプロットされないようにしたい。結果の 1 つのセットでは、約 30 個が重なり合っています (緯度と経度が同じであるため)。この数はさらに多くなる可能性があります。

現時点では、ポイントをポイントの左、右、上、または下に移動して正方形を作成することで、これを達成しようとしています。ポイントで構成された正方形が描画されたら、次の行に移動して、前の正方形の周りに別のポイントの正方形を描画します。

コードは Javascript ですが、非常に一般的であるため、あまり関係がないと思います。

私のコードは次のとおりです。

var prevLong, prevLat, rand = 1, line = 1, spread = 8, i = 0;

function plot_points(long, lat){

    // CODE HERE TO CONVERT long and lat into x and y

    // System to not overlap the points 
    if((prevLat == lat) &&  (prevLong == long)) {

        if(rand==1) {
            x += spread*line;
        } else if(rand==2) {
            x -= spread*line;
        }   else if(rand==3) {
            y += spread*line;
        }   else if(rand==4) {
            y -= spread*line;
        }   else if(rand==5) {
            x += spread*line;
            y += spread*line;
        }   else if(rand==6) {
            x -= spread*line;
            y -= spread*line;
        }   else if(rand==7) {
            x += spread*line;
            y -= spread*line;
        }   else if(rand==8) {
            x -= spread*line;
            y += spread*line;
        // x = double
        }   else if(rand==9) {
            x += spread*line;
            y += spread;
        }   else if(rand==10) {
            x += spread;
            y += spread*line;
        }   else if(rand==11) {
            x -= spread*line;
            y -= spread;
        }   else if(rand==12) {
            x -= spread;
            y -= spread*line;
        }   else if(rand==13) {
            x += spread*line;
            y -= spread;
        }   else if(rand==14) {
            x += spread;
            y -= spread*line;
        }   else if(rand==15) {
            x += spread*line;
            y -= spread;
        }   else if(rand==16) {
            x += spread;
            y -= spread*line;
        } else if(rand==17) {
            x -= spread*line;
            y += spread;
        }   else if(rand==18) {
            x -= spread;
            y += spread*line;
        }   else if(rand==19) {
            x -= spread*line;
            y += spread;
        }   else if(rand==20) {
            x -= spread;
            y += spread*line;
        }

        if(rand == 20) {rand = 1; line++; } else { rand++;  }
        i++
    } else {

        line = 1;
        i = 0; 

    }

    prevLat = latitude;
    prevLong = longitude;

    return [x,y];

}

これは出力です:出力

それは正しく機能しておらず、正しい方法で問題に取り組んでいるかどうかさえわかりません。

以前にこれをしなければならなかった人はいますか?どのような方法をお勧めしますか?

4

2 に答える 2

1

Ernelliのソリューションを例として使用して、プロットを正しく機能させることができました。少し長めのようですが、動作し、遅くはありません。

Ernelliのソリューションには、2番目のforループに誤りがあります。

for(j = 0; i < height; i++)

する必要があります:

for(j = 0; j < height; j++)

とにかく、これは私が使用していて機能している私のコードです。pointArrayは、プロットされるすべての要素の配列です。

var countArray = new Array();

// Go through every point and group same co-ordinates together 
for (var i = pointArray.length-1; i > -1; i--) {
    if(pointArray[i] != undefined) 
    { 
        var found = 0;

        // Check if this point is already in array
        for (var j = countArray.length-1; j > -1; j--)
        {
            if(countArray[j] != undefined)
            {
                if((pointArray[i].getBBox().x == countArray[j]["x"]) && (pointArray[i].getBBox().y == countArray[j]["y"]))
                {
                    countArray[j]["points"].push(pointArray[i]);
                    found = 1;
                }
            } 
        }

        // The point is not in the array, so add it
        if(found != 1) 
        {
            var index = countArray.length;
            countArray[index] = new Array();
            countArray[index]["x"] = pointArray[i].getBBox().x;
            countArray[index]["y"] = pointArray[i].getBBox().y;
            countArray[index]["points"] = new Array();
            countArray[index]["points"].push(pointArray[i]);
        }

    }
}

// For each co-ordinate
for (var j = countArray.length-1; j > -1; j--)
{
    if(countArray[j] != undefined)
    {   
        // If there is more than one point
        if(countArray[j]["points"].length > 1) {

            // Calculate the square points
            var count = countArray[j]["points"].length;
            var x = countArray[j]["x"];
            var y = countArray[j]["x"];
            result = [];

            var width = count/Math.sqrt(count);
            width = Math.floor(width);
            height = Math.floor(count/width);
            var remain = count % width;


            var i2,j2, xx, yy;
            var space = 8;
            for(i2 = 0; i2 < width*space; i2+=space)
            {
              for(j2 = 0; j2 < height*space; j2+=space)
                {
                result.push([(Math.floor(width/2)+i2), (Math.floor(height/2)+j2)]); 
                }   
            }
            for(i2 = 0; i2 < remain*space; i2+=space)
            {
              result.push([(Math.floor(width/2)+i2), (Math.floor(height/2)+j2)]);
            }

            // Go through each point and then translate it to it's new position
            for (var jj = countArray[j]["points"].length-1; jj > -1; jj--)
            {
                if(countArray[j]["points"][jj] != undefined)
                {
                    if(result[jj] != undefined)
                    {   
                        countArray[j]["points"][jj].translate(result[jj][0]-((width*8)/2), result[jj][1]-((height*8)/2))
                    }
                }
            }
        } // End if count more than 1
    } // End if undefined
}

これは多くのraphael.js関数(getBBoxやtranslateなど)を使用することに注意してください

于 2010-04-17T16:46:41.840 に答える
1

座標をグループ化することから始めます。long,lat -> x,y 変換は、最後のステップまで必要ない場合があります。1 つの方法は、各ロング/ラット位置と各位置のカウントを格納するハッシュ マップを作成することです。

次に、それぞれの緯度経度位置を ax、y 座標に変換し、そのカウントを使用して、x、y 位置を中心にポイントをレンダリングする方法を決定しますが、ポイントの数に応じたサイズを使用します。

正方形をレンダリングする作業アルゴリズムは次のとおりです。

var count = 30; // example
result = [];

var width = count/Math.sqrt(count);
width = Math.floor(width);
height = Math.floor(count/width);
var remain = count % width;

var i,j;

for(i = 0; i < width; i++)
  for(j = 0; j < height; j++)
    result.push([x-Math.floor(width/2)+i, y-Math.floor(height/2)+j]; 
for(i = 0; i < remain; i++)
  result.push([x-Math.floor(width/2)+i, y-Math.floor(height/2)+j];

入力はドットの数、x、y 中心座標、出力はレンダリングするポイントの配列です。

最後の行は残りのドットです。たとえば、15 ドットの正方形がレンダリングされます。

OOOO
OOOO
OOOO
OOO
于 2010-04-16T18:20:16.953 に答える