from math import sqrt, cos, sin, radians
def distance(point1, point2):
x1, y1 = point1[0], point1[1]
x2, y2 = point2[0], point2[1]
return sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) # 两点距离
def get_circumference_of_ellipse(a, b, point, angle=0, kama=0.25):
"""
:param a: 长轴
:param b: 短轴
:param point: 起始点
:param angle: 弧度
:param kama: 偏移量
:return: 周长
"""
ellipse = 0
x0, y0 = point[0], point[1]
while (angle <= 360):
x = a * cos(radians(angle)) # 弧度360/2π
y = b * sin(radians(angle))
ellipse += distance((x0, y0), (x, y))
x0, y0 = x, y
angle += kama
return ellipse
ellipse = get_circumference_of_ellipse(5, 5, (5, 0))
print("Circumference of ellipse = %f" % ellipse)
def get_position(ellipse, divide, a, b, point, angle=0, angle0=0.25, kama=0.25):
"""
:param ellipse: 周长
:param divide: 等分
:param a: 长轴
:param b: 短轴
:param point: 起始点
:param angle: 弧度
:param angle0: 游动弧度
:param kama: 偏移量
:return:
"""
list_xy = []
list_x = []
list_y = []
x0, y0 = point[0], point[1]
onetenth = ellipse / divide
for i in range(int(divide)):
dist = 0
while (dist < onetenth):
x = a * cos(radians(angle))
y = b * sin(radians(angle))
dist += distance((x0, y0), (x, y))
x0 = x
y0 = y
angle += kama
print("%d : angle = %.2f\t angle0 = %.2f\tdifference = %.2f" % (i + 1, angle - kama, angle0, angle - angle0))
angle0 = angle
print((float(x0), float(y0)))
list_xy.append((float(x0), float(y0)))
list_x.append(float(x0))
list_y.append(float(y0))
return list_xy, list_y, list_x
# 使用matplotlib将坐标可视化
import numpy as np
import matplotlib.pyplot as mpl
# 使用matplotlib将坐标可视化
list_xy, list_y, list_x = get_position(ellipse, 100, 5, 3, (5, 0))
x = np.array(list_x)
y = np.array(list_y)
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'
for i in range(len(list_x) - 1):
dx = list_x[i + 1] - list_x[i]
dy = list_y[i + 1] - list_y[i]
mpl.quiver(list_x[i], list_y[i], dx, dy, angles='xy', scale=1.003, scale_units='xy', width=0.002)
mpl.show()
python实现椭圆等分
于 2022-01-06 11:13:48 首次发布