到坐标的距离


24

二维平面上有n个人。利用它们之间的距离,我们将找到它们的位置。要获得唯一答案,您必须做出四个假设:

  1. 至少有3个人。
  2. 第一人称位在(0,0)。
  3. 第二人称位置(x,0)对于x> 0。
  4. 第三个人处于(x,y)位置,且y> 0。

因此,您面临的挑战是编写一个程序或函数,该程序或函数将给出二维距离数组(其中D[i][j]给出人i与之间的距离),并j返回其坐标列表。您的答案必须准确到至少6位有效数字。以字节为单位的最短解决方案获胜。


例子

[[0.0, 3.0, 5.0], [3.0, 0.0, 4.0], [5.0, 4.0, 0.0]]

=>

[[0.0, 0.0], [3.0, 0.0], [3.0, 4.0]]


[[0.0, 0.0513, 1.05809686, 0.53741028, 0.87113533], [0.0513, 0.0, 1.0780606,
0.58863967, 0.91899559], [1.05809686, 1.0780606, 0.0, 0.96529704,
1.37140397], [0.53741028, 0.58863967, 0.96529704, 0.0, 0.44501955],
[0.87113533, 0.91899559, 1.37140397, 0.44501955, 0.0]]

=>

[[0.0, 0.0], [0.0513, 0.0], [-0.39, 0.9836], [-0.5366, 0.0295], [-0.8094, -0.3221]]


[[0.0, 41.9519, 21.89390815, 108.37048253, 91.40006121, 49.35063671,
82.20983622, 83.69080223, 80.39436793, 86.5204431, 91.24484876, 22.32327813,
99.5351474, 72.1001264, 71.98278813, 99.8621559, 104.59071383, 108.61475753,
94.91576952, 93.20212636], [41.9519, 0.0, 24.33770482, 144.67214389,
132.28290899, 49.12079288, 85.34321428, 117.39095617, 103.60848008,
79.67795144, 69.52024038, 42.65007733, 105.60007249, 110.50120501,
89.92218111, 60.03623019, 133.61394005, 76.26668715, 130.54041305,
122.74547069], [21.89390815, 24.33770482, 0.0, 130.04213984, 112.98940283,
54.26427666, 71.35378232, 104.72088677, 81.67425703, 90.26668791,
71.13288376, 18.74250061, 109.87223765, 93.96339767, 69.46698314,
84.37362794, 124.38527485, 98.82541733, 116.43603102, 113.07526035],
[108.37048253, 144.67214389, 130.04213984, 0.0, 37.8990613, 111.2161525,
176.70411028, 28.99007398, 149.1355788, 124.17549005, 198.6298252,
126.02950495, 101.55746829, 37.24713176, 152.8114446, 189.29178553,
34.96711005, 180.83483984, 14.33728853, 35.75999058], [91.40006121,
132.28290899, 112.98940283, 37.8990613, 0.0, 111.05881157, 147.27385449,
44.12747289, 115.00173099, 134.19476383, 175.9860033, 104.1315771,
120.19673135, 27.75062658, 120.90347767, 184.88952087, 65.64187459,
183.20903265, 36.35677531, 60.34864715], [49.35063671, 49.12079288,
54.26427666, 111.2161525, 111.05881157, 0.0, 125.59451494, 82.23823276,
129.68328938, 37.23819968, 118.38443321, 68.15130552, 56.84347674,
84.29966837, 120.38742076, 78.30380948, 91.88522811, 72.15031414,
97.00421525, 82.23460459], [82.20983622, 85.34321428, 71.35378232,
176.70411028, 147.27385449, 125.59451494, 0.0, 158.1002588, 45.08950594,
161.43320938, 50.02998891, 59.93581537, 180.43028005, 139.95387244,
30.1390519, 133.42262669, 182.2085151, 158.47101132, 165.61965338,
170.96891788], [83.69080223, 117.39095617, 104.72088677, 28.99007398,
44.12747289, 82.23823276, 158.1002588, 0.0, 136.48099476, 96.57856065,
174.901291, 103.29640959, 77.53059476, 22.95598599, 137.23185588,
160.37639016, 26.14552185, 152.04872054, 14.96145727, 17.29636403],
[80.39436793, 103.60848008, 81.67425703, 149.1355788, 115.00173099,
129.68328938, 45.08950594, 136.48099476, 0.0, 166.89727482, 92.90019808,
63.53459104, 177.66159356, 115.1228903, 16.7609065, 160.79059188,
162.35278463, 179.82760993, 140.44928488, 151.9058635], [86.5204431,
79.67795144, 90.26668791, 124.17549005, 134.19476383, 37.23819968,
161.43320938, 96.57856065, 166.89727482, 0.0, 148.39351779, 105.1934756,
34.72852943, 106.44495924, 157.55442606, 83.19240274, 96.09890812,
61.77726814, 111.24915274, 89.68625779], [91.24484876, 69.52024038,
71.13288376, 198.6298252, 175.9860033, 118.38443321, 50.02998891,
174.901291, 92.90019808, 148.39351779, 0.0, 72.71434547, 175.07913091,
161.59035051, 76.3634308, 96.89392413, 195.433818, 127.21259331,
185.63246606, 184.09218079], [22.32327813, 42.65007733, 18.74250061,
126.02950495, 104.1315771, 68.15130552, 59.93581537, 103.29640959,
63.53459104, 105.1934756, 72.71434547, 0.0, 121.04924013, 88.90999601,
52.48935172, 102.51264644, 125.51831504, 117.54806623, 113.26375241,
114.12813777], [99.5351474, 105.60007249, 109.87223765, 101.55746829,
120.19673135, 56.84347674, 180.43028005, 77.53059476, 177.66159356,
34.72852943, 175.07913091, 121.04924013, 0.0, 93.63052717, 171.17130953,
117.77417844, 69.1477611, 95.81237385, 90.62801636, 65.7996984],
[72.1001264, 110.50120501, 93.96339767, 37.24713176, 27.75062658,
84.29966837, 139.95387244, 22.95598599, 115.1228903, 106.44495924,
161.59035051, 88.90999601, 93.63052717, 0.0, 117.17351252, 159.88686894,
48.89223072, 156.34374083, 25.76186961, 40.13509273], [71.98278813,
89.92218111, 69.46698314, 152.8114446, 120.90347767, 120.38742076,
30.1390519, 137.23185588, 16.7609065, 157.55442606, 76.3634308, 52.48935172,
171.17130953, 117.17351252, 0.0, 145.68608389, 162.51692098, 166.12926334,
142.8970605, 151.6440003], [99.8621559, 60.03623019, 84.37362794,
189.29178553, 184.88952087, 78.30380948, 133.42262669, 160.37639016,
160.79059188, 83.19240274, 96.89392413, 102.51264644, 117.77417844,
159.88686894, 145.68608389, 0.0, 169.4299171, 33.39882791, 175.00707479,
160.25054951], [104.59071383, 133.61394005, 124.38527485, 34.96711005,
65.64187459, 91.88522811, 182.2085151, 26.14552185, 162.35278463,
96.09890812, 195.433818, 125.51831504, 69.1477611, 48.89223072,
162.51692098, 169.4299171, 0.0, 156.08760216, 29.36259602, 11.39668734],
[108.61475753, 76.26668715, 98.82541733, 180.83483984, 183.20903265,
72.15031414, 158.47101132, 152.04872054, 179.82760993, 61.77726814,
127.21259331, 117.54806623, 95.81237385, 156.34374083, 166.12926334,
33.39882791, 156.08760216, 0.0, 167.00907734, 148.3962894], [94.91576952,
130.54041305, 116.43603102, 14.33728853, 36.35677531, 97.00421525,
165.61965338, 14.96145727, 140.44928488, 111.24915274, 185.63246606,
113.26375241, 90.62801636, 25.76186961, 142.8970605, 175.00707479,
29.36259602, 167.00907734, 0.0, 25.82164171], [93.20212636, 122.74547069,
113.07526035, 35.75999058, 60.34864715, 82.23460459, 170.96891788,
17.29636403, 151.9058635, 89.68625779, 184.09218079, 114.12813777,
65.7996984, 40.13509273, 151.6440003, 160.25054951, 11.39668734,
148.3962894, 25.82164171, 0.0]]

=>

[[0.0, 0.0], [41.9519, 0.0], [19.6294, 9.6969], [-88.505, -62.5382],
[-88.0155, -24.6423], [21.2457, -44.5433], [14.7187, 80.8815], [-59.789,
-58.5613], [-29.9331, 74.6141], [34.5297, -79.3315], [62.6017, 66.3826],
[5.2353, 21.7007], [6.1479, -99.3451], [-62.597, -35.7777], [-13.6408,
70.6785], [96.8736, -24.2478], [-61.4216, -84.6558], [92.2547, -57.3257],
[-74.7503, -58.4927], [-55.0613, -75.199]]

2
所以,基本上,您正在寻找DistanceMatrix
inthematica中

在第一个示例中,第三个点可以是(3,4)或(3,-4)。
DavidC

@DavidC您没有足够仔细地阅读这些假设。
orlp

是。我现在知道了。
DavidC

2
是否可以有多个正确答案,或者我做错了什么?我正在获取+0.322第二个示例的最后一个坐标。
Emigna

Answers:


5

Python 2中183 178 166 161 160 159个 158 156字节

@Giuseppe节省了1个字节,@ JonathanFrech节省了2个字节。

def f(D):
 X=D[0][1];o=[0,X];O=[0,0];n=2
 for d in D[2:]:y=d[0]**2;x=(y-d[1]**2)/X/2+X/2;y-=x*x;o+=x,;O+=y**.5*(y>d[2]**2-(x-o[2])**2or-1),;n+=1
 return o,O

在线尝试!

使用前3个点来计算其余部分。返回x-coords, y-coords 注释中允许的一对。


O+=[...]可以O+=...,并且o+=[x]可以o+=x,
乔纳森·弗雷希

@JonathanFrech不起作用。Python仅允许将列表添加到列表。TIO
PurkkaKoodari '10

@ Pietu1998我的意思不是o+=x,而是o+=x,
乔纳森·弗雷希

4

R,107

function(d){y=t(cmdscale(d))
y=y-y[,1]
p=cbind(c(y[3],-y[4]),y[4:3])%*%y/sum(y[,2]^2)^.5
p*c(1,sign(p[6]))}

最重要的起点是第1行,在这里我使用R的函数进行多维缩放(MDS)。其余的可能效率很低(感谢提出改进建议):第2行转换数据,使第一个点位于(0,0);第3行旋转这些点,以使第二个点位于(0,x);第4行翻转所有内容,使第三个点位于y> 0。


R有一个内置的???ang
Giuseppe

3

[R 227个 215 209 176 169字节

function(d){x=y=c(0,0)
x[2]=a=d[1,2]
d=d^2
i=3:nrow(d)
D=d[1,i]
x[i]=(D+a^2-d[2,i])/2/a
y[3]=e=sqrt(d[1,3]-x[3]^2)
y[i]=(D-d[3,i]+x[3]^2+e^2-2*x[3]*x[i])/2/e
Map(c,x,y)}

在线尝试!

从前,我参加了计算几何课程。我想说有帮助,但是我显然什么也没学到。

输入是一个R矩阵,输出是一个由2个元素组成的向量列表(x,y)(更接近规范保存字节)。

当然,这里的问题是前三点。固定三个点后,您可以根据这些点计算所有其他点。

我只是使用了一些代数来简化事情,然后注意到由于我仅使用前3个点来求解其他点,因此所有这些点都非常整齐地矢量化了。

弗洛德(Flodel)


2

的JavaScript(ES7),202个 193字节

d=>{for(k=7;(a=d.map((r,i)=>[x=(r[0]**2-r[1]**2+a*a)/2/a,(d[0][i]**2-x*x)**.5*(k>>i&1||-1)],a=d[0][1])).some(([x,y],i)=>a.some(([X,Y],j)=>(Math.hypot(x-X,y-Y)-d[i][j])**2>1e-6));k+=8);return a}

测试用例

怎么样?

d i,j为输入,x iy i为预期输出。

根据挑战规则,我们知道:

  • 对于任意一对(i,j)d i,j =√((x i -x j)²+(y i -y j)²)
  • x 0 = y 0 = y 1 = 0

我们可以立即推断出:

  1. x 1 = d 0,1

  2. d 0,J =√((X 0 - X Ĵ)2 +(Y 0 - Ÿ Ĵ)²)=√(X Ĵ ²+ Y Ĵ ²)
    d 0,J 2 = X Ĵ ²+ Y Ĵ ²

  3. d 1,J =√((X 1 - X Ĵ)2 +(Y 1 - Y ^ Ĵ)²)=√((X 1 - X Ĵ)²+ Y Ĵ ²)
    d 1,J 2 =(X 1 - X Ĵ)²+ Y Ĵ ²= X 1 2 + X Ĵ 2 + 2× 1 X Ĵ + Y Ĵ ²= d 0,1 2 + X Ĵ 2 + 2D 0,1 X Ĵ + Y Ĵ ²

计算x j

通过使用2和3,我们得到:

X Ĵ ² - (d 0,1 2 + X Ĵ ² - 2D 0,1 X Ĵ)= d 0,J ² - d 1,J ²

这导致:

x j =(d 0, j²-d 1, j²+ d 0,1²)/ 2d 0,1

计算ÿ Ĵ

现在知道x j,我们有:

ÿ Ĵ ²= d 0,J ² - X Ĵ ²

这使:

ÿ Ĵ =±√(d 0,J ² - X Ĵ ²)

我们通过简单地尝试所有可能的组合直到匹配原始距离来确定每个y j的符号。我们还必须确保y 2 > 0

我们通过使用位掩码k来做到这一点,其中1被解释为正,而0被解释为负。我们从k = 7(二进制为111)开始,每次迭代加8。这样,正值ý Ĵ保证被选择用于0≤Ĵ≤2 。(我们也可以从k = 4开始,因为无论如何y 0 = y 1 =0。但是使用7可以防止出现负零。)


我不确定是否会更短,但是计算元素y的符号(在初始3之后)的正确方法k是找到p = (x, y)两个点set p' = (x, -y),并获取第三个已知点j并比较距离d[i][j]dist(p, j)dist(p', j)。顺便说一句,我不认为负零是错误的答案。
orlp

@orlp删除负零不会花费任何字节,因此纯粹是出于美学考虑。:-)(您是对的:这种方法对于最初无法正常工作的解决方案而言效率不高。但是我认为仍然值得发布。)
Arnauld

2

的JavaScript(ES7),140个 139 126 121 118 117字节

@Giuseppe节省了1个字节。

/* this line for testing only */ f =
D=>D.map((d,n)=>n>1?(y=d[0]**2,D[n]=x=(y-d[1]**2)/X/2+X/2,y-=x*x,[x,y**.5*(y>d[2]**2-(x-D[2])**2||-1)]):[X=n*d[0],0])
<!-- HTML for testing only --><textarea id="i" oninput="test()">[[0.0, 0.0513, 1.05809686, 0.53741028, 0.87113533], [0.0513, 0.0, 1.0780606, 0.58863967, 0.91899559], [1.05809686, 1.0780606, 0.0, 0.96529704, 1.37140397], [0.53741028, 0.58863967, 0.96529704, 0.0, 0.44501955], [0.87113533, 0.91899559, 1.37140397, 0.44501955, 0.0]]</textarea><pre id="o"></pre><script>window.onload=test=function(){try{document.querySelector("#o").innerHTML=JSON.stringify(f(JSON.parse(document.querySelector("#i").value)))}catch(e){}}</script>

有点像我的Python答案。结果返回的[x,y]对比JS中单独的X和Y列表短得多。覆盖参数列表,因此请勿多次使用它作为输入。


2
@Giuseppe实际上,我无法得分f=并使其合而为一。:P
PurkkaKoodari

好吧,我不懂JavaScript,所以我不会错过它并不感到惊讶。
朱塞佩

2

Mathematica,160个字节

(s=Table[0{,},n=Tr[1^#]];s[[2]]={#[[1,2]],0};f@i_:=RegionIntersection~Fold~Table[s[[j]]~Circle~#[[j,i]],{j,i-1}];s[[3]]=Last@@f@3;Do[s[[i]]=#&@@f@i,{i,4,n}];s)&

该程序使用内置的功能RegionIntersection来计算圆的交点。程序需要精确的协调才能工作。

这假设RegionIntersection如果x坐标相等,则始终将y坐标较高的点作为结果的最后一个。(至少在Wolfram Sandbox上是这样)

由于某些原因RegionIntersection,如果输入的圆圈过多,则无法使用,因此我必须使用来处理每对Fold

展示截图:屏幕截图

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.