代码之家  ›  专栏  ›  技术社区  ›  betamax

绘制点,使它们在坐标相同时不会重叠

  •  1
  • betamax  · 技术社区  · 16 年前

    我想确保两个点不在同一个地方。在一组结果中,大约有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];
    
    }
    

    这是输出: output

    以前有人这样做过吗?你建议用什么方法?

    2 回复  |  直到 16 年前
        1
  •  1
  •   Ernelli    16 年前

    首先将坐标分组。你的长,纬度->x、 直到最后一步才需要y转换。一种方法是创建一个哈希映射,在其中存储每个long/lat位置和每个位置的计数。

    然后将每个长纬度位置转换为x,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
    
        2
  •  1
  •   betamax    16 年前

    Ernelli的解决方案在第二个for循环中有一个错误:

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

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

    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)

    推荐文章