我想从cmb文件中的一组点使用http://mbostock.github.com/d3/ex/voronoi.html绘制voronoi图.
我修改了代码:
var w = 1200,
h = 800;
d3.text("ReMeRi_4_2_2_0.csv", function(datasetText) {
var vertices = d3.csv.parseRows(datasetText);
var svg = d3.select("#chart")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("class", "BuPu");
svg.selectAll("path")
.data(d3.geom.voronoi(vertices))
.enter().append("path")
.attr("class", function(d, i) { return i ? "q" + (i % 9) + "-9" : null; })
.attr("d", function(d) { return "M" + d.join("L") + "Z"; })
svg.selectAll("circle")
.data(vertices.slice(1))
.enter().append("circle")
.attr("transform", function(d) { return "translate(" + d + ")"; })
.attr("r", 2)
text1 = svg.append("svg:text")
.text("control")
.attr("x", 150) …
Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法来确定一个点是否在voronoi单元内?
例如,以下代码生成如下图所示的内容:
using namespace boost::polygon;
point_data<int> p1(0, 0);
point_data<int> p2(-10, 10);
point_data<int> p3(-10, -10);
point_data<int> p4(10, -10);
point_data<int> p5(10, 10);
std::vector<point_data<int>> pts = { p1, p2, p3, p4, p5 };
construct_voronoi(pts.begin(), pts.end(), vd);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我怎样才能知道点(5,5)是否在中心单元内?
我有一个让我想起Voronoi的问题,但是我希望我的变化能让我避免使用Voronoi算法,并且更快地写一些东西.
这是我在Paint中制作的可怕图像来说明我的问题:
说我有一张地图区域.每个点代表一个商店.每个方块代表一个社区.voronoi图显示了最接近每个商店的区域.
如果其中一个区域占据了一个广场,那么整个广场都属于那个商店.
是否有可能确定哪个方块属于哪个商店,而无需计算中间voronoi图?似乎,因为这就像voronoi图的非常粗略的近似,应该有一个超快的快捷方式来生成它.
我想用基于变量的颜色填充voronoi多边形(在我的情况下为z).这是代码:
library(rgdal)
library(deldir)
library(dplyr)
library(ggplot2)
library(UScensus2010)
x<-c(0,55,105,165,270,65,130,155,155,225,250,295,
30,100,110,135,160,190,230,300,30,70,105,170,
210,245,300,0,85,175,300,15,60,90,90,140,210,
260,270,295,5,55,55,90,100,140,190,255,285,270)
y<-c(305,310,305,310,310,260,255,265,285,280,250,
260,210,240,225,225,225,230,210,215,160,190,
190,175,160,160,170,120,135,115,110,85,90,90,
55,55,90,85,50,50,25,30,5,35,15,0,40,20,5,150)
z<-c(870,793,755,690,800,800,730,728,710,780,804,
855,813,762,765,740,765,760,790,820,855,812,
773,812,827,805,840,890,820,873,875,873,865,
841,862,908,855,850,882,910,940,915,890,880,
870,880,960,890,860,830)
dati<-data.frame(x,y,z)
vor_pts <- SpatialPointsDataFrame(cbind(dati$x,dati$y),dati, match.ID=TRUE)
SPointsDF_to_voronoi_SPolysDF <- function(sp) {
# tile.list extracts the polygon data from the deldir computation
vor_desc <- tile.list(deldir(sp@coords[,1], sp@coords[,2]))
lapply(1:(length(vor_desc)), function(i) {
# tile.list gets us the points for the polygons but we
# still have to close them, hence the need for the rbind
tmp <- cbind(vor_desc[[i]]$x, vor_desc[[i]]$y)
tmp <- rbind(tmp, tmp[1,])
# now we can …
Run Code Online (Sandbox Code Playgroud) 是否可以为scipy.spatial.Voronoi
图表着色?我知道它是.
但现在我的目标是根据颜色标度为每个细胞着色以表示物理量.
如下图所示(PRL 107,155704(2011)):
而且我还想知道是否可以计算每个单元格的面积,因为它是我想要计算的数量
我认为我的问题与这个问题或其他问题有一些共同点,但无论如何,我的问题并不是专门针对它们的。
我想,在找到某些点的 voronoi 镶嵌之后,能够检查其他给定点在镶嵌中的位置。特别是:
假设有 50 个额外点,我希望能够计算每个 voronoi 单元包含多少这些额外点。
我的 MWE
from scipy.spatial import ConvexHull, Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt
points = [[0,0], [1,4], [2,3], [4,1], [1,1], [2,2], [5,3]]
#voronoi
vor = Voronoi(points)
voronoi_plot_2d(vor)
plt.show()
Run Code Online (Sandbox Code Playgroud)
现在我得到了加分
extraPoints = [[0.5,0.2], [3, 0], [4,0],[5,0], [4,3]]
# In this case we have that the first point is in the bottom left,
# the successive three are in the bottom right and the last one
# is in the …
Run Code Online (Sandbox Code Playgroud) 遵循这两个资源:
我写了一个Delaunay三角剖分boost
.如果点坐标是积分的,它可以正常工作(我生成了几个随机测试,但没有观察到错误).但是,如果这些点是非整数的,我会发现许多不正确的三角剖分,边缘缺失或边缘错误.
例如,此图像已使用舍入值构建并且是正确的(请参阅下面的代码)
但是这个图像是用原始值构建的并且不正确(参见下面的代码)
此代码重现了这两个示例(没有显示).
#include <boost/polygon/voronoi.hpp>
using boost::polygon::voronoi_builder;
using boost::polygon::voronoi_diagram;
struct Point
{
double a;
double b;
Point(double x, double y) : a(x), b(y) {}
};
namespace boost
{
namespace polygon
{
template <>
struct geometry_concept<Point>
{
typedef point_concept type;
};
template <>
struct point_traits<Point>
{
typedef double coordinate_type;
static inline coordinate_type get(const Point& point, orientation_2d orient)
{
return (orient == HORIZONTAL) ? point.a : point.b;
}
};
} // polygon
} // boost
int …
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的数据框,我想用它创建一个圆形 voronoi 图
df <- data.frame(country = c("Ukraine", "Russia", "Argentina", "China", "Romania", "Other"),
prod = c(11.0, 10.6, 3.1, 2.4, 2.1, 15.3))
df
#> country prod
#> 1 Ukraine 11.0
#> 2 Russia 10.6
#> 3 Argentina 3.1
#> 4 China 2.4
#> 5 Romania 2.1
#> 6 Other 15.3
Run Code Online (Sandbox Code Playgroud)
由reprex 包于 2022 年 4 月 8 日创建(v2.0.1)
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following …
Run Code Online (Sandbox Code Playgroud)