circle and point

Robert Kern robert.kern at gmail.com
Sun Nov 13 00:46:02 EST 2005


Ben Bush wrote:
> is there any code to decide whether a point is located within the circle
> by three other points?

# Converted from my C++ code.
# C.f. http://www.ics.uci.edu/~eppstein/junkyard/circumcenter.html
def circumcenter(x0, y0,
                 x1, y1,
                 x2, y2):
    x0m2 = x0 - x2
    y1m2 = y1 - y2
    x1m2 = x1 - x2
    y0m2 = y0 - y2
    x0p2 = x0 + x2
    y1p2 = y1 + y2
    x1p2 = x1 + x2
    y0p2 = y0 + y2

    D = x0m2*y1m2 - x1m2*y0m2
    # You probably want to test for D being close to 0 here

    centerx = (((x0m2*x0p2 + y0m2*y0p2)/2*y1m2)
              - (x1m2*x1p2 + y1m2*y1p2)/2*y0m2) / D
    centery = (((x1m2*x1p2 + y1m2*y1p2)/2*x0m2)
              - (x0m2*x0p2 + y0m2*y0p2)/2*x1m2) / D

    return centerx, centery

def incircle(x0, y0,
             x1, y1,
             x2, y2,
             x,  y):
    centerx, centery = circumcenter(x0, y0, x1, y1, x2, y2)
    return ((x-centerx)**2 + (y-centery)**2 <=
            (x0-centerx)**2 + (y0-centery)**2)

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list