python画出三角形外接圆和内切圆的艺术,轻便完结python画圆功效

ca88会员登录中心

刚看了《最壮大脑》中国和英国对决,个中难度最大的类型需求选手先脑补泰森多边形,再搜索一模一样的三个Tyson多边形。在惊呆且惊叹自身头脑死板的同一时候,不免手痒想要借助计算机弄个图出来看看,闲来无事吹吹嘘也是极好的。

python画出三角形外接圆和内切圆的方法,python外接圆

刚看了《最强大脑》中国和英国对决,个中难度最大的花色供给选手先脑补Tyson多边形,再找寻大同小异的几个Tyson多边形。在惊呆且惊叹自个儿头脑粗笨的同一时间,不免手痒想要借助Computer弄个图出来看看,闲来无事吹说大话也是极好的。

前天先来作画外接圆和内切圆,留个牛池湾前面来填。

外接圆圆心:三角形垂直平分线的交点。
内切圆圆心:三角形角平分线的交点。

有了思路,就足以用万能的python来总括了

import matplotlib.pyplot as plt
from scipy.linalg import solve
import numpy as np
from matplotlib.patches import Circle

'''
求三角形外接圆和内切圆
'''
# 画个三角形
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 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 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)

# 内切圆
def get_inner_circle(A, B, C):
  xa, ya = A[0], A[1]
  xb, yb = B[0], B[1]
  xc, yc = C[0], C[1]

  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

  a = np.sqrt((xb - xc)**2 + (yb - yc)**2)
  b = np.sqrt((xa - xc)**2 + (ya - yc)**2)
  c = np.sqrt((xa - xb)**2 + (ya - yb)**2)

  ang_a = np.arccos((b**2 + c**2 - a**2) / (2 * b * c))
  ang_b = np.arccos((a**2 + c**2 - b**2) / (2 * a * c))

  # 两条角平分线的斜率
  k1 = np.tan(alpha + ang_a / 2)
  k2 = np.tan(beta + ang_b / 2)
  kv = np.tan(alpha + np.pi / 2)

  # 求圆心
  y, x = solve([[1.0, -k1], [1.0, -k2]], [ya - k1 * xa, yb - k2 * xb])
  ym, xm = solve([[1.0, -ka], [1.0, -kv]], [ya - ka * xa, y - kv * x])
  r1 = np.sqrt((x - xm)**2 + (y - ym)**2)

  return(x, y, r1)

if __name__ == '__main__':
  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, 'ro')
  draw_circle(x, y, r1)

  x_inner, y_inner, r_inner = get_inner_circle(A, B, C)
  plt.plot(x_inner, y_inner, 'ro')
  draw_circle(x_inner, y_inner, r_inner)

  plt.show()

上面看看四个三角形的结果:

ca88会员登录中心 1

上述正是本文的全体内容,希望对我们的上学抱有援助,也期待我们多多协理帮客之家。

刚看了《最壮大脑》中国和英国对决,个中难度最大的档案的次序须求选手先脑补Tyson多边形,再找…

正文实例为大家大饱眼福了python达成画圆功用的具体代码,供大家参谋,具体内容如下

后天先来作画外接圆和内切圆,留个鸭脷洲后边来填。

ca88会员登录中心 2

外接圆圆心:三角形垂直平分线的交点。
内切圆圆心:三角形角平分线的交点。

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.patches import Polygon 
import matplotlib.patches as mpatches 

fig = plt.figure(figsize = (16,8)) 
ax = fig.gca() 
ax.set_xlim(-5,18) 
ax.set_ylim(-5,8) 
alpha = np.linspace(-np.pi,np.pi,30)  
t = np.linspace(-np.pi,np.pi,100) 
def ppp(nc , na , c , sx , sy): 
 beta = alpha[na] 
 gam = alpha[na+1] 
 x = [] ; y = [] ; 
 r = 0.5*nc 
 x.append(r*np.cos(beta)) ; y.append(r*np.sin(beta)) ; 
 tt = np.linspace(beta,gam,10) 
 for i in range(len(tt)): 
  x.append(r*np.cos(tt[i])) 
  y.append(r*np.sin(tt[i])) 
 r-=0.5 
 x.append(r*np.cos(gam)) ; y.append(r*np.sin(gam)) 
 tt = np.linspace(gam,beta,10) 
 for i in range(len(tt)): 
  x.append(r*np.cos(tt[i])) ; y.append(r*np.sin(tt[i])) 
 x = np.array(x) ; y = np.array(y) 
 verts = list(zip(x+sx,y+sy)) 
 poly = Polygon(verts , facecolor=c , edgecolor=c) 
 ax.add_patch(poly) 

#ppp(2,0,'r',0,0) 
def get_coordiate(x , y): 
 beta = (alpha[y]+alpha[y+1])/2 
 pr = 0.5*(x+x-1)/2 
 sx = pr*np.cos(beta) ; sy = pr*np.sin(beta) 
 return sx,sy 
def pp_arrow(x,y): 
 print x , y 
 sx , sy = get_coordiate(x, y) 
 for i in range(-1,2): 
  for j in range(-1,2): 
   if i == 0 and j == 0: 
    continue 
   ex , ey = get_coordiate(x+i, y+j) 
   ax.arrow(sx,sy,ex-sx,ey-sy,head_width=0.05,head_length=0.1,fc='r',ec='r') 

def pxy(cx,cy): 
 for i in range(10): 
  plt.plot(0.5*i*np.cos(t)+cx,0.5*i*np.sin(t)+cy)  
 for i in range(len(alpha)): 
  plt.plot((0.5*np.cos(alpha)+cx,4.5*np.cos(alpha)+cx),(0.5*np.sin(alpha)+cy,4.5*np.sin(alpha)+cy)) 

cx = 13 ; cy =0 
pxy(0,0) 
pxy(13,0) 

mr = np.zeros((10,30)) 
mb = np.zeros((10,30)) 

for i in range(3,9): 
 for j in range(1,27): 
  ttt = np.random.random() 
  if ttt >= 0.98 and ttt <= 1: 
   ppp(i,j,'r',0,0) 
   mr[i][j] = 1 
   continue 
  if ttt < 0.8: 
   continue 
  else: 
   ppp(i,j,'b',0,0) 
   mb[i][j] = 1 

for i in range(10): 
 for j in range(30): 
  if mr[i][j] == 0: 
   continue 
  pp_arrow(i, j) 
  ma = 0 ;posx = 1 ; posy = 1 
  for k in range(-1,2): 
   for s in range(-1,2): 
    mb[i+k][j+s]=0 
    ttt = np.random.random() 
    if ttt > ma: 
     ma = ttt 
     posx = k ; posy = s 
  ppp(i+posx , j+posy , 'r' , 13 , 0) 
  ppp(i,j,'g',13,0) 

for i in range(10): 
 for j in range(30): 
  if mb[i][j] == 0: 
   continue 
  ppp(i , j , 'b' , 13 , 0) 

arrow = mpatches.Arrow(5, 0 
      ,3,0,width = 1,color = 'r') 
ax.add_patch(arrow) 
ax.text(6.2,0.5,'after one\nunit time',ha='center',va='center',color='y') 

xx = [4,4,4] 
yy = [7.5,6.5,5.5] 
c = ['b' , 'r' , 'g'] 
s = ['debris','satellite' , 'the location of satellite one unit time ago'] 
for i in range(3): 
 print xx[i] , yy[i] 
 rect = mpatches.Rectangle((xx[i],yy[i]),0.5,0.3,ec = c[i],fc=c[i] ) 
 ax.text(xx[i]+0.7,yy[i],s[i]) 
 ax.add_patch(rect) 
plt.axis('off') 
plt.show() 

有了思路,就能够用万能的python来计量了

如上就是本文的全体内容,希望对大家的就学抱有扶助,也期望大家多多帮助脚本之家。

import matplotlib.pyplot as plt
from scipy.linalg import solve
import numpy as np
from matplotlib.patches import Circle

'''
求三角形外接圆和内切圆
'''
# 画个三角形
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 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 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)

# 内切圆
def get_inner_circle(A, B, C):
  xa, ya = A[0], A[1]
  xb, yb = B[0], B[1]
  xc, yc = C[0], C[1]

  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

  a = np.sqrt((xb - xc)**2 + (yb - yc)**2)
  b = np.sqrt((xa - xc)**2 + (ya - yc)**2)
  c = np.sqrt((xa - xb)**2 + (ya - yb)**2)

  ang_a = np.arccos((b**2 + c**2 - a**2) / (2 * b * c))
  ang_b = np.arccos((a**2 + c**2 - b**2) / (2 * a * c))

  # 两条角平分线的斜率
  k1 = np.tan(alpha + ang_a / 2)
  k2 = np.tan(beta + ang_b / 2)
  kv = np.tan(alpha + np.pi / 2)

  # 求圆心
  y, x = solve([[1.0, -k1], [1.0, -k2]], [ya - k1 * xa, yb - k2 * xb])
  ym, xm = solve([[1.0, -ka], [1.0, -kv]], [ya - ka * xa, y - kv * x])
  r1 = np.sqrt((x - xm)**2 + (y - ym)**2)

  return(x, y, r1)

if __name__ == '__main__':
  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, 'ro')
  draw_circle(x, y, r1)

  x_inner, y_inner, r_inner = get_inner_circle(A, B, C)
  plt.plot(x_inner, y_inner, 'ro')
  draw_circle(x_inner, y_inner, r_inner)

  plt.show()

您只怕感兴趣的小说:

  • Python完结的圈子绘制(画圆)示例
  • python画出三角形外接圆和内切圆的艺术
  • python完成画圆成效
  • Python达成PS图像抽象画风效果的法子
  • ca88会员登录中心 ,Python三落到实处的图案及加载图片动画效果示例
  • 少儿编制程序python入门

下边看看多个三角形的结果:

ca88会员登录中心 3

以上正是本文的全体内容,希望对大家的读书抱有扶助,也目的在于我们多多协助脚本之家。

您也许感兴趣的小说:

  • Python完毕的圈子绘制(画圆)示例
  • python达成画圆作用
  • 简短达成python画圆功效
  • Python达成PS图像抽象画风效果的办法
  • Python三贯彻的摄影及加载图片动画效果示例
  • 孩儿编程python入门

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图