import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QBrush, QPen
from PyQt5.QtCore import Qt
class KochCurveWidget(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 1108, 900)
self.setWindowTitle('Koch Curve')
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
pen = QPen(Qt.black)
pen.setWidth(2)
painter.setPen(pen)
# 抽象步骤,如果是0阶,只需前行;如果是一阶,需要前行,转向,前行,转向,前行,转向,前行,
# 共有的是前行,阶数需要控制转向的次数,所以边界是0阶,只需前行
def koch(length, n):
if n == 0:
painter.drawLine(0, 0, length, 0)
painter.translate(length, 0)
else:
#
for angle in [0, -60, 120, -60]:
painter.rotate(angle)
koch(length / 3, n - 1)
painter.translate(250, 250)
for i in range(3):
koch(600, 2)
painter.rotate(120)
app = QApplication(sys.argv)
koch_curve_widget = KochCurveWidget()
koch_curve_widget.show()
sys.exit(app.exec_())
三阶科赫雪花
支持大佬,给大佬点赞