绘制点,以便它们在具有相同坐标的情况下不会重叠
我有一个函数,它需要经度和纬度并将其转换为要绘制的 x 和 y。到 X 和 Y 的转换工作正常,这不是我遇到的问题。
我想确保两个点不会绘制在同一个地方。在一组结果中,大约有 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];
}
这是输出:
它无法正常工作,我什至不知道是否我正在以正确的方式处理这个问题。
以前有人必须这样做吗?你会建议什么方法?
I have a function that takes longitude and latitude and converts it to x and y to be plotted. The conversion to X and Y is working fine and that is not what I have the problem with.
I want to ensure that two points are not plotted in the same place. In one set of results there are about 30 on top of each other (because they have the same latitude and longitude), this number could be a lot larger.
At the moment I am trying to achieve this by moving points to the left, right, top or bottom of the point to make a square. Once a square made up of points has been drawn, then moving to the next row on and drawing another square of points around the previous square.
The code is Javascript but it is very generic so I guess it's slightly irrelevant.
My code is as follows:
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];
}
This is the output:
It isn't working correctly and I don't even know if I am approaching the problem in a correct way at all.
Has anyone had to do this before? What method would you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先对坐标进行分组。您的经纬度 ->在最后一步之前可能不需要 x,y 转换。一种方法是创建一个哈希图,在其中存储每个经/纬度位置以及每个位置的计数。
然后,将每个长、纬度位置转换为 ax,y 坐标,并使用计数来决定如何渲染以 x,y 位置为中心的点,但其大小根据点的数量而定。
渲染正方形的工作算法是:
输入是点数、x,y 中心坐标,输出是要渲染的点数组。
最后一行是剩余的点,例如 15 点的正方形渲染:
Start by grouping your coordinates. Your long,lat -> x,y conversion might not be necessary until the last step. One method is to create a hash map where you store each long/lat position and a count for each position.
Then you convert each long,lat position into a x,y coordinate and use the count to decide how to render the points centered around x,y position but with a size according to the number of points.
A working algorithm to render a square is:
Input is count of of dots, x,y center coordinate, output is an array of points to render.
The last line is the remaining dots, e.g a 15 dot square renders:
以 Ernelli 的解决方案为例,我成功地使绘图正常工作。看起来有点啰嗦,但是确实有效,而且速度也不慢。
Ernelli 的解决方案在第二个 for 循环中有一个错误:
应该是:
无论如何,这是我正在使用的代码,并且正在工作。 pointArray 是要绘制的所有元素的数组:
请注意,这使用了许多 raphael.js函数(例如 getBBox 和 translate)
Using Ernelli's solution as an example, I have managed to get the plotting working correctly. It seems a bit long winded, but it works and it is not slow.
Ernelli's solution has a mistake in the second for loop:
Should be:
Anyway, this is my code that I am using and that is working. pointArray is an array of all the elements that are to be plotted:
Note that this uses many raphael.js functions (such as getBBox and translate)