使用solve / optim基于两个点(已知半径)确定圆心

时间:2012-09-04 13:45:23

标签: r optimization geometry

我有一对积分,我想找到一个由这两点决定的已知r的圆圈。我将在模拟中使用它,xy的可能空间有边界(比如一个-200,200的框)。

It is known半径为

的半径
(x-x1)^2 + (y-y1)^2 = r^2
(x-x2)^2 + (y-y2)^2 = r^2

我现在想解决这个非线性方程组,得到两个潜在的圆心。我尝试使用包BB。这是我的微弱尝试,只给出了一点。我想得到的是两个可能的要点。任何指向正确方向的指针都将在第一时间获得免费啤酒。

library(BB)
known.pair <- structure(c(-46.9531139599816, -62.1874917150412, 25.9011462171242, 
16.7441676243879), .Dim = c(2L, 2L), .Dimnames = list(NULL, c("x", 
"y")))

getPoints <- function(ps, r, tr) {
    # get parameters
     x <- ps[1]
     y <- ps[2]

     # known coordinates of two points
     x1 <- tr[1, 1]
     y1 <- tr[1, 2]
     x2 <- tr[2, 1]
     y2 <- tr[2, 2]

     out <- rep(NA, 2)
     out[1] <- (x-x1)^2 + (y-y1)^2 - r^2
     out[2] <- (x-x2)^2 + (y-y2)^2 - r^2
     out
}

slvd <- BBsolve(par = c(0, 0),
                fn = getPoints,
                method = "L-BFGS-B",
                tr = known.pair,
                r = 40
                )

以图形方式,您可以使用以下代码查看此内容,但您需要一些额外的软件包。

library(sp)
library(rgeos)
plot(0,0, xlim = c(-200, 200), ylim = c(-200, 200), type = "n", asp = 1)
points(known.pair)
found.pt <- SpatialPoints(matrix(slvd$par, nrow = 1))
plot(gBuffer(found.pt, width = 40), add = T)

enter image description here

附录

感谢大家的宝贵意见和代码。我提供了海报的答案,他们用代码称赞他们的答案。

    test replications elapsed relative user.self sys.self user.child sys.child
4   alex          100    0.00       NA      0.00        0         NA        NA
2  dason          100    0.01       NA      0.02        0         NA        NA
3   josh          100    0.01       NA      0.02        0         NA        NA
1 roland          100    0.15       NA      0.14        0         NA        NA

7 个答案:

答案 0 :(得分:9)

以下代码将为您提供两个所需圆圈中心的点数。现在没时间对此进行评论或将结果转换为Spatial*个对象,但这应该会给你一个良好的开端。

首先,这是一个介绍点名称的ASCII艺术图。 kK是已知点,B是横跨k绘制的水平点,C1C2是中心你所追求的圈子:

        C2





                            K


                  k----------------------B






                                       C1

现在代码:

# Example inputs
r <- 40
known.pair <- structure(c(-46.9531139599816, -62.1874917150412, 
25.9011462171242, 16.7441676243879), .Dim = c(2L, 2L), 
.Dimnames = list(NULL, c("x", "y")))

## Distance and angle (/_KkB) between the two known points
d1 <- sqrt(sum(diff(known.pair)^2))
theta1 <- atan(do.call("/", as.list(rev(diff(known.pair)))))

## Calculate magnitude of /_KkC1 and /_KkC2
theta2 <- acos((d1/2)/r)

## Find center of one circle (using /_BkC1)
dx1 <- cos(theta1 + theta2)*r
dy1 <- sin(theta1 + theta2)*r
p1 <- known.pair[2,] + c(dx1, dy1)

## Find center of other circle (using /_BkC2)
dx2 <- cos(theta1 - theta2)*r
dy2 <- sin(theta1 - theta2)*r
p2 <- known.pair[2,] + c(dx2, dy2)

## Showing that it worked
library(sp)
library(rgeos)
plot(0,0, xlim = c(-200, 200), ylim = c(-200, 200), type = "n", asp = 1)
points(known.pair)
found.pt <- SpatialPoints(matrix(slvd$par, nrow = 1))
points(p1[1], p1[2], col="blue", pch=16)
points(p2[1], p2[2], col="green", pch=16)

enter image description here

答案 1 :(得分:4)

这是其他人提到的解决问题的基本几何方式。我使用多根来获得所得二次方程的根,但你可以直接使用二次方程。

# x is a vector containing the two x coordinates
# y is a vector containing the two y coordinates
# R is a scalar for the desired radius
findCenter <- function(x, y, R){
    dy <- diff(y)
    dx <- diff(x)
    # The radius needs to be at least as large as half the distance
    # between the two points of interest
    minrad <- (1/2)*sqrt(dx^2 + dy^2)
    if(R < minrad){
        stop("Specified radius can't be achieved with this data")
    }

    # I used a parametric equation to create the line going through
    # the mean of the two points that is perpendicular to the line
    # connecting the two points
    # 
    # f(k) = ((x1+x2)/2, (y1+y2)/2) + k*(y2-y1, x1-x2)
    # That is the vector equation for our line.  Then we can
    # for any given value of k calculate the radius of the circle
    # since we have the center and a value for a point on the
    # edge of the circle.  Squaring the radius, subtracting R^2,
    # and equating to 0 gives us the value of t to get a circle
    # with the desired radius.  The following are the coefficients
    # we get from doing that
    A <- (dy^2 + dx^2)
    B <- 0
    C <- (1/4)*(dx^2 + dy^2) - R^2

    # We could just solve the quadratic equation but eh... polyroot is good enough
    k <- as.numeric(polyroot(c(C, B, A)))

    # Now we just plug our solution in to get the centers
    # of the circles that meet our specifications
    mn <- c(mean(x), mean(y))
    ans <- rbind(mn + k[1]*c(dy, -dx),
                 mn + k[2]*c(dy, -dx))

    colnames(ans) = c("x", "y")

    ans
}

findCenter(c(-2, 0), c(1, 1), 3)

答案 2 :(得分:4)

按照@ PhilH的解决方案,只需在R中使用三角法

radius=40

在半径上绘制原始点

plot(known.pair,xlim=100*c(-1,1),ylim=100*c(-1,1),asp=1,pch=c("a","b"),cex=0.8)

找到c的中点ab(这也是de两个圆心的中点)

AB.bisect=known.pair[2,,drop=F]/2+known.pair[1,,drop=F]/2
C=AB.bisect
points(AB.bisect,pch="c",cex=0.5)

找到和弦ab

的长度和角度
AB.vector=known.pair[2,,drop=F]-known.pair[1,,drop=F]
AB.len=sqrt(sum(AB.vector^2))
AB.angle=atan2(AB.vector[2],AB.vector[1])
names(AB.angle)<-NULL

计算从c到两个圆的中心的线的长度和角度

CD.len=sqrt(diff(c(AB.len/2,radius)^2))
CD.angle=AB.angle-pi/2

计算并绘制两个中心de从垂直于ab的位置和长度:

center1=C+CD.len*c(x=cos(CD.angle),y=sin(CD.angle))
center2=C-CD.len*c(x=cos(CD.angle),y=sin(CD.angle))
points(center1[1],center1[2],col="blue",cex=0.8,pch="d")
points(center2[1],center2[2],col="blue",cex=0.8,pch="e")

节目:

enter image description here

答案 3 :(得分:3)

无需数值方程求解。只是公式:

  1. 您知道由于A点和B点都位于圆上,因此每个点与给定中心的距离为半径r。
  2. 形成一个等腰三角形,其中两个已知点的弦位于底部,第三个点位于圆心。
  3. 将A和B之间的三角形平分,为您提供一个直角三角形。
  4. http://mathworld.wolfram.com/IsoscelesTriangle.html根据基本长度和半径为您提供高度。
  5. 遵循AB和弦(See this SO Answer)的法线,获得从该点沿每个方向计算的高度距离。

答案 4 :(得分:1)

以下是答案的骨头,如果我有时间,我会把它们充实。如果你画出这些文字,这应该很容易理解,对不起,我没有在这台电脑上找到合适的软件来为你画画。

抛开退化的情况,其中点是相同的(无限解)或太远以至于位于所选半径的同一圆上(无解)。

标记点XY以及2个圈c1c2的未知中心点。 c1c2位于XY的垂直平分线上;请拨打此行c1c2,在此阶段,我们不了解c1c2位置的所有详细信息并不重要。

因此,找出行c1c2的等式。它经过XY的中间点(称此点Z)并且斜率等于XY的负倒数。现在你有c1c2的等式(或者如果这些骨头上有任何肉,你就会这样。)

现在将三角形从一个点构造到线与其垂直平分线的交点和圆的中心点(比如XZc1)。您仍然不知道c1的确切位置,但从未阻止任何人勾画几何图形。您有一个已知(XZXc1)两个边长的直角三角形,因此很容易找到Zc1。对其他三角形和圆心重复此过程。

当然,这种方法与OP的初始方法完全不同,可能没有吸引力。

答案 5 :(得分:1)

要摆脱一些警告,但这应该让你开始。可能存在性能问题,因此使用基本几何完全解决它可能是一种更好的方法。

known.pair <- structure(c(-46.9531139599816, -62.1874917150412, 25.9011462171242, 
                          16.7441676243879), .Dim = c(2L, 2L), .Dimnames = list(NULL, c("x", 
                                                                                        "y")))

findCenter <- function(p,r) {
  yplus <- function(y) {
    ((p[1,1]+sqrt(r^2-(y-p[1,2])^2)-p[2,1])^2+(y-p[2,2])^2-r^2)^2
  }


 yp <- optimize(yplus,interval=c(min(p[,2]-r),max(p[,2]+r)))$minimum
 xp <- p[1,1]+sqrt(r^2-(yp-p[1,2])^2)  
 cp <- c(xp,yp)
 names(cp)<-c("x","y") 

 yminus <- function(y) {
   ((p[1,1]-sqrt(r^2-(y-p[1,2])^2)-p[2,1])^2+(y-p[2,2])^2-r^2)^2
 }


 ym <- optimize(yminus,interval=c(min(p[,2]-r),max(p[,2]+r)))$minimum
 xm <- p[1,1]-sqrt(r^2-(ym-p[1,2])^2)  
 cm <- c(xm,ym)
 names(cm)<-c("x","y")


 list(c1=cp,c2=cm)
}

cent <- findCenter(known.pair,40)

答案 6 :(得分:0)

我希望你知道一些基本的几何学,因为我不能不幸地画它。

垂直平分线是一条圆的每个中点都穿过A和B的线。

现在你有AB和r的中间位置,所以你可以用A点,AB的中间和圆圈的未知中点绘制一个直角三角形。

现在使用毕达哥拉斯定理得到从AB的中点到圆的中点的距离,并使用基本的正弦/余弦组合来计算圆的位置不应该很难。 / p>

相关问题