【问题标题】:Create polygon area based on center of points根据点的中心创建多边形区域
【发布时间】:2015-10-16 04:22:55
【问题描述】:

我对 R 很陌生,我想创建一个函数,它需要几个点,找到这些点的中心(比如质心),然后从这些点画出分隔组的线点,该中心位于点的中间。类似于制作馅饼片:我们从中心分割馅饼,得到等量的部分。

我用于查找中心和绘图本身的代码如下:

distance <- function(points1, points2) {
  distanceMatrix <- matrix(NA, nrow=dim(points1)[1], ncol=dim(points2)[1])
  for(i in 1:nrow(points2)) {
    distanceMatrix[,i] <- sqrt(rowSums(t(t(points1)-points2[i,])^2))
  }
  distanceMatrix
}

find_cluster <- function(x, centers, distFun, nItter=10) {
  clusterHistory <- vector(nItter, mode="list")
  centerHistory <- vector(nItter, mode="list")

  for(i in 1:nItter) {
    distsToCenters <- distFun(x, centers)
    clusters <- apply(distsToCenters, 1, which.min)
    centers <- apply(x, 2, tapply, clusters, mean)
    # Saving history
    clusterHistory[[i]] <- clusters
    centerHistory[[i]] <- centers
  }

  list(clusters=clusterHistory, centers=centerHistory)
}

a3=as.matrix(test)
centers <- a3[sample(nrow(a3), 5),]

theResult <- find_cluster(a3, centers, myEuclid, 10)

剧情:

plot(a3, col=theResult$clusters[[i]],
 main=paste("itteration:", i), xlab="x", ylab="y")
points(theResult$centers[[i]],
 cex=1, pch=19, col=1:nrow(theResult$centers[[i]]))

所以函数应该这样做:

  1. 输入中心
  2. 找到这些点(即聚类中心)的质心(或质点)
  3. 从主中心(即质心)开始,绘制线条或多边形,以便将集群分开

测试数据集可以在pastebin找到。我想要的一个例子是here(及以下):

【问题讨论】:

    标签: r function plot polygon k-means


    【解决方案1】:

    你可以这样做(n 是你想要的集群数量)

    dat <- read.table(file="test.txt", header=T)
    
    separateClusts <- function(n, dat) {
        ## Cartesian to polar (is there a function for this?)
        cart2pol <- function(x, y, deg = FALSE) {
            r <- sqrt(x^2 + y^2)
            theta <- atan(y / x)
            theta[x < 0] <- theta[x < 0] + pi
            theta[x >= 0 & y < 0] <- theta[x >= 0 & y < 0] + 2*pi
            if (deg) theta <- theta * 180/pi
            out <- cbind(r, theta)
            names(out) <- c("r", "theta")
            return( out )
        }
    
        ## Get clusters
        clusts <- kmeans(dat, n)
        centers <- clusts$centers
    
        ## Center of mass of clusters
        com <- matrix(colMeans(centers), ncol=2)
    
        ## Order them
        cent <- t(t(centers) - c(com))  # center
        pol <- cart2pol(cent[,1], cent[,2])
        ord <- sort(pol[,2], index=T)$ix
        ordered <- as.data.frame(centers[ord, ])
    
        ## Get midpoints
        mids <- with(ordered, {
            data.frame(
                xmid=c(x[-1] - x[-length(x)], x[1]-x[length(x)])/2 + x,
                ymid=c(y[-1] - y[-length(y)], y[1]-y[length(y)])/2 + y
            )
        })
    
        ## Plot
        plot(dat, col=clusts$cluster)
        points(com, col="blue", pch=16, cex=2)
        points(centers, col="red", pch=16, cex=2)
        points(mids, col="orange", pch=16, cex=2)
    
        ## Draw line segments
        ms <- (tmp <- t(t(mids) - c(com)))[,2] / tmp[,1]
        for (i in 1:nrow(mids))
            segments(com[,1], com[,2],
                     com[,1] + (s <- sign(mids$x[i]-com[,1]))*5,
                     com[,2] + s*ms[i]*5, col="orange", lwd=2)
    }
    
    separateClusts(5, dat)
    

    红点是聚类中心,橙色点是连续中心之间的中点。中心的顺序是通过将它们转换为极坐标并使用角度来确定的。

    【讨论】:

    • 但是是否可以将金色线条移动到图形窗口的末端,使每条金色线条从中间的蓝点移动到各个轴?跨度>
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    相关资源
    最近更新 更多