8

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

已知半径的平方为

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

我现在想解决这个非线性方程组以获得两个潜在的圆心。我尝试使用 package 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)

在此处输入图像描述

附录

谢谢大家的宝贵意见和代码。我为那些用代码称赞他们的答案的发布者提供了答案的时间。

    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
4

7 回答 7

9

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

首先,这是一个介绍点名称的 ASCII 艺术图。kK是已知点,B是通过 绘制的水平线上的点kC1C2是您所追求的圆的中心:

        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)

在此处输入图像描述

于 2012-09-04T15:04:28.763 回答
4

这是其他人都提到的解决它的基本几何方法。我使用 polyroot 来得到二次方程的根,但你可以很容易地直接使用二次方程。

# 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)
于 2012-09-04T15:53:33.797 回答
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的中点abde

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

计算并绘制两个中心的位置d以及e从垂线到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")

显示:

在此处输入图像描述

于 2012-09-04T18:11:46.490 回答
3

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

  1. 您知道,由于 A 点和 B 点都位于圆上,因此从每个点到给定中心的距离就是半径 r。
  2. 以两个已知点的弦为底,第三个点为圆心,形成一个等腰三角形。
  3. 将 A 和 B 之间的三角形一分为二,得到一个直角三角形。
  4. http://mathworld.wolfram.com/IsoscelesTriangle.html为您提供基长和半径方面的高度。
  5. 按照 AB 弦的法线(请参阅此 SO 答案),以获取刚刚在每个方向上计算的距该点的高度距离。
于 2012-09-04T14:46:44.387 回答
1

这是答案的基本内容,如果以后有时间,我会充实它们。如果你跟着文字一起画,这应该很容易理解,对不起,我这台电脑上没有合适的软件为你画图。

撇开点相同(无限解)或相距太远而无法位于所选半径的同一圆上(无解)的退化情况。

标记点XY以及 2 个圆的未知中心点c1c2c1并且c2位于 的垂直平分线上XY;调用这条线c1c2,在这个阶段,我们不知道 和 位置的所有细节是无关紧要c1c2

所以,找出线的方程c1c2。它通过 的中点XY(称为该点Z),斜率等于 的负倒数XY。现在你有了等式c1c2(或者如果这些骨头上有任何肉,你就会得到)。

现在构造从一点到直线与其垂直平分线的交点和圆的中心点的三角形(例如XZc1)。您仍然不知道确切的位置c1,但这从未阻止任何人绘制几何图形。你有一个直角三角形,它的两条边长已知 (XZXc1),所以很容易找到Zc1。对另一个三角形和圆心重复该过程。

当然,这种方法与 OP 最初的方法有很大不同,可能没有吸引力。

于 2012-09-04T14:18:22.547 回答
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)
于 2012-09-04T14:22:38.380 回答
0

我希望你知道一些基本的几何图形,因为不幸的是我不会画它。

垂直平分线是穿过 A 和 B 的圆的每个中点所在的线。

现在你有了 AB 和 r 的中点,所以你可以用点 A、AB 的中点和未知的圆的中点画一个直角三角形。

现在使用毕达哥拉斯定理得到 AB 的中点到圆的中点的距离,从这里开始计算圆的位置应该不难,使用基本的 sin/cos 组合。

于 2012-09-04T14:31:21.960 回答