Carrying forward the Olympic spirit, we draw the five Olympic rings in line 49

Posted by sabbagh on Fri, 31 Dec 2021 23:15:34 +0100

Although the Olympic Games encountered many obstacles and was delayed due to the impact of the epidemic, the public opposition was postponed, and then there were xq cases after the rehearsal, and there were dark scenes in the alley. Let's ignore all these ~

As of the publication of this article, let's take a look at the war situation:

Carry forward the Olympic spirit, today we write songs with 49 lines, the five Olympic rings ~

Preview of this article:

Olympic symbol

Olympics The logo (Olympic logo / symbol Olympique / Olympic rings) is created by Pierre de Coubertin It was conceived and designed by Mr. in 1913< Olympic Charter >Determined, also known as the Olympic Games Five rings It is the most widely recognized symbol in the world Olympic Games Sign. It consists of five Olympic ring It is composed of socket connection and has five colors: blue, yellow, black, green and red. The rings are connected with each other from left to right, with blue, black and red rings above and yellow and green rings below. The whole shape is a small rule at the bottom trapezoid.

initialization

We use PyQt5

Set up a frame first:

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

# Define Olympic class
class Olympic(QWidget):
  def __init__(self):
    # Parent class initialization
    super().__init__()
    # Set width and height
    self.setGeometry(300, 300, 550, 600)
    self.setWindowTitle('Olympic rings')
    # initialize variable
    self.isBlue = False
    self.isYellow = False
    self.isBlack = False
    self.isGreen = False
    self.isRed = False
    self.brushWidth = 10
    self.show()    
    
  # Define painting events
  def paintEvent(self, event):
    qp = QPainter()
    qp.begin(self)
    
if __name__ == '__main__':  
  app = QApplication(sys.argv)
  ex = Olympic()
  app.exec_() 

start

Paint the first blue circle first

 # Define painting events
  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() 

Draw circles of other colors in turn

 # Define painting events
  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() 

It's like this after painting ~

Optimization linked

You think it's finished?

nono~

When the Olympic rings were like this ~

The Lun family is linked

So we have to deal with it. First draw a picture:

 # Draw an arc to cover it
    qp.setPen(bluePen)
    qp.drawArc(99, 150, 100, 100, 345*16, 30*16) 

Look at the effect

Yellow and blue are right. Let's deal with them in the same way

 # Draw an arc to cover 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() 

Look at the effect

In this way, our five bad Olympic Games will be finished ~

Full code:

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

# Define Olympic class
class Olympic(QWidget):
  def __init__(self):
    # Parent class initialization
    super().__init__()
    # Set width and height
    self.setGeometry(300, 300, 550, 600)
    self.setWindowTitle('Olympic rings')
    # initialize variable
    self.isBlue = False
    self.isYellow = False
    self.isBlack = False
    self.isGreen = False
    self.isRed = False
    self.brushWidth = 10
    self.show()    
    
  # Define painting events
  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 to cover 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_() 

Remove comments and blank lines, line 49

However, this seems too simple. We add a mouse to click on the circle to get the color. If the circles of two colors cross, we need to take out two colors, get ready, and start ~

Take color

Olympic class adds and defines mouse click events

 # Define mouse click events
  def mousePressEvent(self, event):
    self.x = event.x()
    self.y = event.y()
    
    # Calculate whether to click in 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 lower view
    self.update() 

paintEvent add

 if((self.isBlue)):
      if(self.isBlue):
        qp.fillRect(100, 450, 350, 100, Qt.blue) 

Look at the effect

Tonifying all color effects

mousePressEvent method

 # Define mouse click events
  def mousePressEvent(self, event):
    self.x = event.x()
    self.y = event.y()
    
    # Calculate whether to click in 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 lower view
    self.update() 

For the paintEvent method, it should be noted that if two circles are combined, 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) 

Fix bug

 # Repair command+w close window
  sys.exit(app.exec_()) 

Final effect

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 Olympic class
class Olympic(QWidget):
  def __init__(self):
    # Parent class initialization
    super().__init__()
    # Set width and height
    self.setGeometry(300, 300, 550, 600)
    self.setWindowTitle('Olympic rings')
    # initialize variable
    self.isBlue = False
    self.isYellow = False
    self.isBlack = False
    self.isGreen = False
    self.isRed = False
    self.brushWidth = 10
    self.show()   

  # Define mouse click events
  def mousePressEvent(self, event):
    self.x = event.x()
    self.y = event.y()
    
    # Calculate whether to click in 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 lower view
    self.update()

  # Define painting events
  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 to cover 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()
  # Repair command+w close window
  sys.exit(app.exec_()) 

task

Using what we have learned today, we can realize that there is a little white edge at the intersection of each circle:

Topics: Python Programmer