import numpy as np
from matplotlib import tri
from scipy. linalg import solve
from matplotlib. patches import Circle
import matplotlib. pyplot as plt
def draw_circle ( x, y, r) :
ax = plt. gca( )
cir = Circle( xy= ( x, y) , radius= r, alpha= 0.5 )
ax. add_patch( cir)
ax. plot( )
def plot_triangle ( A, B, C) :
x = [ A[ 0 ] , B[ 0 ] , C[ 0 ] , A[ 0 ] ]
y = [ A[ 1 ] , B[ 1 ] , C[ 1 ] , A[ 1 ] ]
ax = plt. gca( )
ax. plot( x, y, linewidth= 2 )
def get_outer_circle ( A, B, C) :
xa, ya = A[ 0 ] , A[ 1 ]
xb, yb = B[ 0 ] , B[ 1 ]
xc, yc = C[ 0 ] , C[ 1 ]
x1, y1 = ( xa + xb) / 2.0 , ( ya + yb) / 2.0
x2, y2 = ( xb + xc) / 2.0 , ( yb + yc) / 2.0
ka = ( yb - ya) / ( xb - xa) if xb != xa else None
kb = ( yc - yb) / ( xc - xb) if xc != xb else None
alpha = np. arctan( ka) if ka != None else np. pi / 2
beta = np. arctan( kb) if kb != None else np. pi / 2
k1 = np. tan( alpha + np. pi / 2 )
k2 = np. tan( beta + np. pi / 2 )
y, x = solve( [ [ 1.0 , - k1] , [ 1.0 , - k2] ] , [ y1 - k1 * x1, y2 - k2 * x2] )
r1 = np. sqrt( ( x - xa) ** 2 + ( y - ya) ** 2 )
return ( x, y, r1)
import matplotlib. pyplot as plt
A = ( 1 . , 1 . )
B = ( 5 . , 2 . )
C = ( 5 . , 5 . )
plt. axis( 'equal' )
plt. axis( 'off' )
plot_triangle( A, B, C)
x, y, r1 = get_outer_circle( A, B, C)
plt. plot( x, y, 'rs' )
draw_circle( x, y, r1)
plt. show( )
def strtoarray ( filename) :
data= [ ]
with open ( filename, 'r' ) as f:
lines = f. readlines( )
for line in lines:
value = [ float ( s) for s in line. split( ) ]
data. append( value[ : 2 ] )
return ( data)
x= [ ]
y= [ ]
Index= [ ]
filename= 'C:/Users/liupengyu/Desktop/8-93.txt'
points= strtoarray( filename)
for i in range ( len ( points) ) :
x. append( points[ i] [ 0 ] )
y. append( points[ i] [ 1 ] )
Index. append( i)
import matplotlib
triangles = tri. Triangulation( x, y)
plt. triplot( triangles)
plt. figure( figsize= ( 12 , 9 ) )
plt. show( )
T = triangles. triangles
n = T. shape[ 0 ]
C = np. zeros( ( n, 3 ) )
for i in range ( n) :
C[ i] = get_outer_circle( points[ T[ i, 0 ] ] , points[ T[ i, 1 ] ] , points[ T[ i, 2 ] ] )
X, Y = C[ : , 0 ] , C[ : , 1 ]
segments = [ ]
for i in range ( n) :
for j in range ( 3 ) :
k = triangles. neighbors[ i] [ j]
if k != - 1 :
segments. append( [ ( X[ i] , Y[ i] ) , ( X[ k] , Y[ k] ) ] )
print ( C)
plt. figure( figsize= ( 12 , 9 ) )
plt. scatter( x, y, color= "blue" , marker= "s" )
lines = matplotlib. collections. LineCollection( segments, color= 'red' )
plt. gca( ) . add_collection( lines)
plt. axis( ( 53.6 , 54.7 , 54.9 , 56.1 ) )
for i in range ( len ( Index) ) :
plt. text( x[ i] , y[ i] + 0.02 , Index[ i] , ha = 'center' , va = 'bottom' , fontsize= 8 )
plt. show( )