Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give 3 points and a plot circle

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()

But only gave me two lines.

like image 407
iam_agf Avatar asked Mar 07 '15 02:03

iam_agf


Video Answer


1 Answers

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))

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).

like image 50
Alex Martelli Avatar answered Sep 28 '22 00:09

Alex Martelli