This article is participating in Python Theme Month. See the link to the event for more details

XDJMM, I’m still that unwomanly cat.

Although the Olympic Games encountered many obstacles, the impact of the epidemic and postponed, postponed to hold the public opposition, and then after the rehearsal also broke XQ case, more out of the trail black curtain. Let’s just ignore all of that

As of publication, here’s a look at the battle:

Carry forward the Olympic spirit, today we use 49 lines to write a song about the Olympic Rings ~

Preview of this article:

Olympic symbol

The Olympic Logo /Symbole Olympique/Olympic Rings was conceived and designed by Pierre de Coubertin in 1913. It is determined by the Olympic Charter and is also known as the Olympic Rings. It is the most widely recognized symbol of the Olympic Games in the world. It consists of five interlocking Olympic rings in five colors: blue, yellow, black, green and red. The rings are interconnected from left to right, with blue, black and red rings on top and yellow and green rings on the bottom. The whole shape is a regular trapezoid with a small base.

Initialize the

We use PyQt5

Let’s build a framework:

import sys
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt

# Define the Olympic class
class Olympic(QWidget) :
  def __init__(self) :
    # Superclass initialization
    super().__init__()
    Set width and height
    self.setGeometry(300.300.550.600)
    self.setWindowTitle('Olympic Rings')
    # Initialize variables
    self.isBlue = False
    self.isYellow = False
    self.isBlack = False
    self.isGreen = False
    self.isRed = False
    self.brushWidth = 10
    self.show()    
    
  # Define the paint event
  def paintEvent(self, event) :
    qp = QPainter()
    qp.begin(self)
    
if __name__ == '__main__':  
  app = QApplication(sys.argv)
  ex = Olympic()
  app.exec_()
Copy the code

start

Let’s paint the first blue circle

  # Define the paint event
  def paintEvent(self, event) :
    qp = QPainter()
    qp.begin(self)
    bluePen = QPen(QBrush(Qt.blue), self.brushWidth)
    qp.setPen(bluePen)
    qp.drawEllipse(99.150.100.100)
    qp.end()
Copy the code

Draw circles of other colors in turn

  # Define the paint event
  def paintEvent(self, event) :
    qp = QPainter()
    qp.begin(self)
    
    bluePen = QPen(QBrush(Qt.blue), self.brushWidth)
    yellowPen = QPen(QBrush(Qt.yellow), self.brushWidth)
    blackPen = QPen(QBrush(Qt.black), self.brushWidth)
    greenPen = QPen(QBrush(Qt.green), self.brushWidth)
    redPen = QPen(QBrush(Qt.red), self.brushWidth)
    
    qp.setPen(bluePen)
    qp.drawEllipse(99.150.100.100)

    qp.setPen(yellowPen)
    qp.drawEllipse(161.5.200.100.100)
    
    qp.setPen(blackPen)
    qp.drawEllipse(224.150.100.100)
    
    qp.setPen(greenPen)
    qp.drawEllipse(286.5.200.100.100)
    
    qp.setPen(redPen)
    qp.drawEllipse(349.150.100.100)
    
    qp.end()
Copy the code

Painting is so great aunt ~

Optimization goes hand in hand

You think you’re done?

Nono ~

The Olympic rings are so great ~

The family is interlinked

So let’s do it. Let’s draw it:

    # Draw an arc over it
    qp.setPen(bluePen)
    qp.drawArc(99.150.100.100.345*16.30*16)
Copy the code

Look at the effect

Yellow and blue are right. Let’s do the same thing again

    # Draw an arc over it
    qp.setPen(bluePen)
    qp.drawArc(99.150.100.100.345*16.30*16)
    qp.setPen(yellowPen)
    qp.drawArc(161.5.200.100.100.70*16.15*16)
    qp.setPen(greenPen)
    qp.drawArc(286.5.200.100.100.160*16.30*16)
    qp.setPen(blackPen)
    qp.drawArc(224.150.100.100.345*16.30*16)
    qp.setPen(greenPen)
    qp.drawArc(286.5.200.100.100.70*16.15*16)
    qp.end()
Copy the code

Look at the effect

So our Olympic five bad painting finished ~

Complete code:

import sys
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt

# Define the Olympic class
class Olympic(QWidget) :
  def __init__(self) :
    # Superclass initialization
    super().__init__()
    Set width and height
    self.setGeometry(300.300.550.600)
    self.setWindowTitle('Olympic Rings')
    # Initialize variables
    self.isBlue = False
    self.isYellow = False
    self.isBlack = False
    self.isGreen = False
    self.isRed = False
    self.brushWidth = 10
    self.show()    
    
  # Define the paint event
  def paintEvent(self, event) :
    qp = QPainter()
    qp.begin(self)
    
    bluePen = QPen(QBrush(Qt.blue), self.brushWidth)
    yellowPen = QPen(QBrush(Qt.yellow), self.brushWidth)
    blackPen = QPen(QBrush(Qt.black), self.brushWidth)
    greenPen = QPen(QBrush(Qt.green), self.brushWidth)
    redPen = QPen(QBrush(Qt.red), self.brushWidth)
    
    qp.setPen(bluePen)
    qp.drawEllipse(99.150.100.100)

    qp.setPen(yellowPen)
    qp.drawEllipse(161.5.200.100.100)
    
    qp.setPen(blackPen)
    qp.drawEllipse(224.150.100.100)
    
    qp.setPen(greenPen)
    qp.drawEllipse(286.5.200.100.100)
    
    qp.setPen(redPen)
    qp.drawEllipse(349.150.100.100)
    
    # Draw an arc over it
    qp.setPen(bluePen)
    qp.drawArc(99.150.100.100.345*16.30*16)
    qp.setPen(yellowPen)
    qp.drawArc(161.5.200.100.100.70*16.15*16)
    qp.setPen(greenPen)
    qp.drawArc(286.5.200.100.100.160*16.30*16)
    qp.setPen(blackPen)
    qp.drawArc(224.150.100.100.345*16.30*16)
    qp.setPen(greenPen)
    qp.drawArc(286.5.200.100.100.70*16.15*16)
    qp.end()
    
if __name__ == '__main__':  
  app = QApplication(sys.argv)
  ex = Olympic()
  app.exec_()
Copy the code

Strip out comments and blank lines, 49 lines

But that seems too easy, let’s add a mouse and click around to pick the colors, if the two colors cross we want to pick the two colors, ready, go

The color

Olympic class is added to define the mouse click event

  # Define mouse click event
  def mousePressEvent(self, event) :
    self.x = event.x()
    self.y = event.y()
    
    # Calculate whether the click is within the circle according to the mouse position
    def point_is_in_the_circle(origin_x, origin_y, radius, x, y) :
      d = ((x - origin_x)**2 + (y - origin_y)**2* * ()1/2)
      if (d < radius):  
        return True
      return False 
      
    self.isBlue = False  
    if(point_is_in_the_circle(149.200.50, self.x, self.y)): 
      self.isBlue = True

    Update the lower view
    self.update()
Copy the code

PaintEvent add

    if((self.isBlue)):
      if(self.isBlue):
        qp.fillRect(100.450.350.100, Qt.blue)
Copy the code

Look at the effect

Fill gas all take color effect

MousePressEvent method

  # Define mouse click event
  def mousePressEvent(self, event) :
    self.x = event.x()
    self.y = event.y()
    
    # Calculate whether the click is within the circle according to the mouse position
    def point_is_in_the_circle(origin_x, origin_y, radius, x, y) :
      d = ((x - origin_x)**2 + (y - origin_y)**2* * ()1/2)
      if (d < radius):  
        return True
      return False 
      
    self.isBlue = False
    self.isYellow = False
    self.isBlack = False
    self.isGreen = False
    self.isRed = False    
    if(point_is_in_the_circle(149.200.50, self.x, self.y)): 
      self.isBlue = True
    if(point_is_in_the_circle(211.5.250.50, self.x, self.y)):
      self.isYellow = True
    if(point_is_in_the_circle(274.200.50, self.x, self.y)):
      self.isBlack = True
    if(point_is_in_the_circle(336.5.250.50, self.x, self.y)):
      self.isGreen = True
    if(point_is_in_the_circle(399.200.50, self.x, self.y)):
      self.isRed = True
    Update the lower view
    self.update()
Copy the code

In the paintEvent method, it is important to note that the two circles are union to take out both colors, as follows:

    if((self.isBlue) or (self.isYellow) or (self.isBlack) or (self.isGreen) or (self.isRed)):
      if(self.isBlue):
        qp.fillRect(100.450.350.100, Qt.blue)
      if(self.isYellow):
        qp.fillRect(100.450.350.100, Qt.yellow)
      if((self.isBlue) and (self.isYellow)):
        qp.fillRect(100.450.175.100, Qt.blue)
        qp.fillRect(275.450.175.100, Qt.yellow)
      if(self.isBlack):
        qp.fillRect(100.450.350.100, Qt.black)
      if((self.isBlack) and (self.isYellow)):
        qp.fillRect(100.450.175.100, Qt.yellow)
        qp.fillRect(275.450.175.100, Qt.black)
      if(self.isGreen):
        qp.fillRect(100.450.350.100, Qt.green)
      if((self.isBlack) and (self.isGreen)):
        qp.fillRect(100.450.175.100, Qt.black)
        qp.fillRect(275.450.175.100, Qt.green)
      if(self.isRed):
        qp.fillRect(100.450.350.100, Qt.red)
      if((self.isGreen) and (self.isRed)):
        qp.fillRect(100.450.175.100, Qt.green)
        qp.fillRect(275.450.175.100, Qt.red)
Copy the code

Fix the bug

  # Fix command+w to close the window
  sys.exit(app.exec_())
Copy the code

The final result

The complete code is as follows:

import sys
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt

# Define the Olympic class
class Olympic(QWidget) :
  def __init__(self) :
    # Superclass initialization
    super().__init__()
    Set width and height
    self.setGeometry(300.300.550.600)
    self.setWindowTitle('Olympic Rings')
    # Initialize variables
    self.isBlue = False
    self.isYellow = False
    self.isBlack = False
    self.isGreen = False
    self.isRed = False
    self.brushWidth = 10
    self.show()   

  # Define mouse click event
  def mousePressEvent(self, event) :
    self.x = event.x()
    self.y = event.y()
    
    # Calculate whether the click is within the circle according to the mouse position
    def point_is_in_the_circle(origin_x, origin_y, radius, x, y) :
      d = ((x - origin_x)**2 + (y - origin_y)**2* * ()1/2)
      if (d < radius):  
        return True
      return False 
      
    self.isBlue = False
    self.isYellow = False
    self.isBlack = False
    self.isGreen = False
    self.isRed = False    
    if(point_is_in_the_circle(149.200.50, self.x, self.y)): 
      self.isBlue = True
    if(point_is_in_the_circle(211.5.250.50, self.x, self.y)):
      self.isYellow = True
    if(point_is_in_the_circle(274.200.50, self.x, self.y)):
      self.isBlack = True
    if(point_is_in_the_circle(336.5.250.50, self.x, self.y)):
      self.isGreen = True
    if(point_is_in_the_circle(399.200.50, self.x, self.y)):
      self.isRed = True
    Update the lower view
    self.update()

  # Define the paint event
  def paintEvent(self, event) :
    qp = QPainter()
    qp.begin(self)
    
    bluePen = QPen(QBrush(Qt.blue), self.brushWidth)
    yellowPen = QPen(QBrush(Qt.yellow), self.brushWidth)
    blackPen = QPen(QBrush(Qt.black), self.brushWidth)
    greenPen = QPen(QBrush(Qt.green), self.brushWidth)
    redPen = QPen(QBrush(Qt.red), self.brushWidth)
    
    qp.setPen(bluePen)
    qp.drawEllipse(99.150.100.100)

    qp.setPen(yellowPen)
    qp.drawEllipse(161.5.200.100.100)
    
    qp.setPen(blackPen)
    qp.drawEllipse(224.150.100.100)
    
    qp.setPen(greenPen)
    qp.drawEllipse(286.5.200.100.100)
    
    qp.setPen(redPen)
    qp.drawEllipse(349.150.100.100)
    
    # Draw an arc over it
    qp.setPen(bluePen)
    qp.drawArc(99.150.100.100.345*16.30*16)
    qp.setPen(yellowPen)
    qp.drawArc(161.5.200.100.100.70*16.15*16)
    qp.setPen(greenPen)
    qp.drawArc(286.5.200.100.100.160*16.30*16)
    qp.setPen(blackPen)
    qp.drawArc(224.150.100.100.345*16.30*16)
    qp.setPen(greenPen)
    qp.drawArc(286.5.200.100.100.70*16.15*16)
    
    if((self.isBlue) or (self.isYellow) or (self.isBlack) or (self.isGreen) or (self.isRed)):
      if(self.isBlue):
        qp.fillRect(100.450.350.100, Qt.blue)
      if(self.isYellow):
        qp.fillRect(100.450.350.100, Qt.yellow)
      if((self.isBlue) and (self.isYellow)):
        qp.fillRect(100.450.175.100, Qt.blue)
        qp.fillRect(275.450.175.100, Qt.yellow)
      if(self.isBlack):
        qp.fillRect(100.450.350.100, Qt.black)
      if((self.isBlack) and (self.isYellow)):
        qp.fillRect(100.450.175.100, Qt.yellow)
        qp.fillRect(275.450.175.100, Qt.black)
      if(self.isGreen):
        qp.fillRect(100.450.350.100, Qt.green)
      if((self.isBlack) and (self.isGreen)):
        qp.fillRect(100.450.175.100, Qt.black)
        qp.fillRect(275.450.175.100, Qt.green)
      if(self.isRed):
        qp.fillRect(100.450.350.100, Qt.red)
      if((self.isGreen) and (self.isRed)):
        qp.fillRect(100.450.175.100, Qt.green)
        qp.fillRect(275.450.175.100, Qt.red)

    qp.end()

if __name__ == '__main__':  
  app = QApplication(sys.argv)
  ex = Olympic()
  # Fix command+w to close the window
  sys.exit(app.exec_())
Copy the code

homework

Using what we learned today, implement the following: each circle intersects with a little white edge: