如何绘制具有两个端点坐标的直线和具有两个端点和中心点坐标的圆弧
我想用Python构建一个PyQt5应用程序,使用已知的(已经计算出的)点坐标绘制直线和圆弧,即具有两个端点的直线和具有两个端点和一个中心点的圆弧。点坐标将根据已知的几何参数(例如长度、角度和圆弧半径)计算得出。我想添加水平滑块来控制几何参数并获得类似于下图中的交互式 2D 图形应用程序。使用 Pyt5 和 Python 实现此目的最快、最有效的方法是什么?哪些 2D 绘图库最合适?
I want to build a PyQt5 application with Python that draws lines and arcs using known (already calculated) point coordinates, i.e., lines with two end point and arcs with two end point and a center point. The point coordinates will be calculated from known geometric parameters such as length, angle, and arc radius. I would like to add horizontal sliders to control the geometric parameters and obtain an interactive 2D graphics application similar to the one in the following image. What is the fastest and most efficient way to achieve this with Pyt5 and Python? What 2D drawing libraries would be most suitable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QPainterPath 通常是复杂且连接的绘图路径的更合适选择:不仅它提供了一个代表路径的独特对象,但它还使用破折号图案提供了正确的绘画,而这对于多个线段来说是不可能(轻易)实现的。
Qt已经提供了基本的功能来实现你想要的。具体来说,由于中心实际上是圆的中心,因此您已经知道绘制圆弧所需的一切:
p5点;
p5
和p3
(或p4
)之间长度的两倍;p5
和p3
以及p5
和p4
之间的线的角度;根据您的回答,还请注意:
drawLine(x1, y1, x2, y2)
、drawArc(x, y, w, h 、角度、跨度等)只接受整数值,要实现精确绘制需要使用相对的Qt对象:QPointF、QLineF、QRectF;总是检查文档以查看接受的参数类型(并且不要过多依赖自动 python 转换);
请注意,对于任意连接,您可能需要检查往返弧线的方向,以便使用正确的跨度角(对于逆时针方向可能为负值)。
QPainterPath is generally a more appropriate choice for complex and connected drawing paths: not only it provides a unique object representing the path, but it also provides proper painting using dash patterns which wouldn't be (easily) possible with multiple segments.
Qt already provides basic functions to achieve what you want. Specifically, since the center is actually the center of the circle, you already know everything is needed to draw the arc:
p5
point;p5
andp3
(orp4
);p5
andp3
andp5
andp4
;Based on your answer, also note that:
drawLine(x1, y1, x2, y2)
,drawArc(x, y, w, h, angle, span)
, etc.) only accept integer values, to achieve precise painting you need to use the relative Qt objects: QPointF, QLineF, QRectF; always check the documentation to see the accepted arguments types (and don't rely too much in automatic python conversions);Note that for arbitrary connections you might need to check the direction of the lines to and from the arc in order to use the proper span angle (which might be negative for counter-clock directions).
我在musicamante的帮助下解决了这个问题。我使用了QT的QPainter类。 drawLine() 方法使用起来很简单,因为它只需要终点坐标。 drawArc() 方法使用起始角度和跨度角度,需要额外的方法来获取它们。以下是工作代码。
输出:
I solved this issue with the help from musicamante. I used QT's QPainter class. The drawLine() method is simple to use as it requires only the end point coordinates. The drawArc() method uses start angle and span angle which required additional method to obtain them. Following is the working code.
Output: