-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.py
34 lines (27 loc) · 1.35 KB
/
draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import sys, math
from PyQt5 import QtCore, QtGui, QtWidgets
class MyWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.pen = QtGui.QPen(QtGui.QColor(0,0,0)) # set lineColor
self.pen.setWidth(3) # set lineWidth
self.brush = QtGui.QBrush(QtGui.QColor(255,0,255,255)) # set fillColor
self.polygon = self.createPoly(4,100,0) # polygon with n points, radius, angle of the first point
def createPoly(self, n, r, s):
polygon = QtGui.QPolygonF()
w = 360/n # angle per step
for i in range(n): # add the points of polygon
t = w*i + s
x = r*math.cos(math.radians(t))
y = r*math.sin(math.radians(t))
polygon.append(QtCore.QPointF(self.width()/2 +x, self.height()/2 + y))
return polygon
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(self.pen)
painter.setBrush(self.brush)
painter.drawPolygon(self.polygon)
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())