用Python 画一段圆弧
原来使用 C++ 绘制的,现在需要使用 Python 进行绘制
C++ 接收 原点坐标 和 圆弧两端的坐标 绘制
Python 利用 Pillow 这个库进行绘制
安装
pip install pillow
PIL(pillow) 提供画弧的 函数
PIL.ImageDraw.Draw.arc(xy, start, end, fill=None)
在给定的边界框内,在起始角度和终止角度之间绘制弧线(圆轮廓的一部分)
参数: xy – 定义边界框的四个点[(x0, y0), (x1, y1)] 或 [x0, y0, x1, y1]的序列 outline – 用于轮廓线的颜色
那些首先需要将
原点坐标 和 圆弧两端的坐标 这两个参数转换成 xy、start、end 这三个参数
import math
from PIL import Image,ImageDraw
def (centerPt, Pt1, Pt2)
arc_color = (0, 0, 0, 255) # RGBA 的黑色
# 先算中心点到圆弧两端的长度
length1_x = abs(centerPt[0] - Pt1[0])
length1_y = abs(centerPt[1] - Pt1[1])
length2_x = abs(centerPt[0] - Pt2[0])
length2_y = abs(centerPt[1] - Pt2[1])
length1= math.sqrt((length1_x**2)+(length1_y**2)) # 算原点与第一点的线段长度
length2= math.sqrt((length2_x**2)+(length2_y**2)) # 算原点与第二点的线段长度
# 选出最长的一条
longer_len = length1 if (length1 > length2) else length2
# 圆弧的外接矩形
arc_left_top_x = centerPt[0] - longer_len
arc_left_top_y = centerPt[1] - longer_len
arc_right_bottom_x = centerPt[0] + longer_len
arc_right_bottom_y = centerPt[1] + longer_len
# 外接矩形
# draw.rectangle([arc_left_top_x,arc_left_top_y,arc_right_bottom_x,arc_right_bottom_y],fill=None,outline='blue',width=1)
# 算中心点到圆弧端点的角度
start_angle = math.atan2(Pt2[1]-centerPt[1], Pt2[0]-centerPt[0])*180/math.pi
end_angle = math.atan2(Pt1[1]-centerPt[1], Pt1[0]-centerPt[0])*180/math.pi
# print([arc_left_top_x,arc_left_top_y,arc_right_bottom_x,arc_right_bottom_y])
# print(f"开始角度 {start_angle}")
# print(f"结束角度 {end_angle}")
# 画圆弧
draw.arc([arc_left_top_x,arc_left_top_y,arc_right_bottom_x,arc_right_bottom_y],start=start_angle,end=end_angle,fill=arc_color,width=1)