给3分和一个情节圈 [英] Give 3 points and a plot circle
问题描述
我想将[0,1],[1,0]和[0,-1]点赋予python并绘制通过它们的圆.是否存在制作此功能的python模块?我试过使用 matplotlib:
I want to give the points [0,1],[1,0] and [0,-1] to python and plot the circle that passes over them. Does exists a python module that make this? I have tried using matplotlib:
import matplotlib.pyplot as plt
plt.plot([0,1,0],[1,0,-1])
plt.show()
但只给了我两行.
推荐答案
有一个与此完全匹配的代码高尔夫"问题(除了要求圆的方程,而不是绘制它)--请参阅 https://codegolf.stackexchange.com/questions/2289/circle-through-three-points.将第一个也是最短的(Python)解决方案分解为更具可读性,更不容易破解的形式,以符合您的确切规格-但保留使用复数进行更简单计算的核心思想:
There was a "code golf" question exactly matching this (except that the circle's equation was requested, rather than plotting it) -- see https://codegolf.stackexchange.com/questions/2289/circle-through-three-points . Unraveling the first and shortest (Python) solution into more readable, less-hacky form to match your exact specs - but keeping the core idea of using complex numbers for simpler calculations:
x, y, z = 0+1j, 1+0j, 0-1j
w = z-x
w /= y-x
c = (x-y)*(w-abs(w)**2)/2j/w.imag-x
print '(x%+.3f)^2+(y%+.3f)^2 = %.3f^2' % (c.real, c.imag, abs(c+x))
好的,这仍然是打印方程式",而不是绘制圆",但是,我们越来越接近:-).要在 matplotlib
中实际绘制圆,请参见例如用 pyplot 绘制一个圆——在上面的解决方案中,c
是圆的(否定的)中心(作为一个复数,因此使用 .real 和 .imag 作为x/y坐标),以及 abs(c + x)
半径(实数, abs
如此).
OK, this still "prints the equation" rather than "plotting the circle", but, we're getting close:-). To actually plot the circle in matplotlib
, see e.g plot a circle with pyplot -- in the solution above, c
is the (negated) center of the circle (as a complex number, so use .real and .imag for the x/y coordinates), and abs(c+x)
the radius (a real number, abs
makes it so).
这篇关于给3分和一个情节圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!