PyQt5 and trigonometric functions: the control is distributed along the circle track (the circle parameter equation finds the coordinates of the point on the circle)

0x01 circle track

The definition of a trajectory in mathematics is a collection of points that meet certain conditions, called the trajectory of the point. The so-called circle track is essentially all points on a circle. The standard equation is (xa) 2 +(yb) 2 = r 2 , and the following equation can be obtained after derivation:


Circle parameter equation: {x = a + r ∗ cos θ y = b + r ∗ sin θ \left\{\begin {aligned}x&=a+r * cos θ\\y&=b+r*sinθ \end{aligned} \right.{ xand=a+rcosθ=b+rsinθ, (Θ is the parameter, that is, the angle), (a, b) are the coordinates of the center of the circle.


The calculation of the equation can use math or numpy. The parameters passed in when calculating the trigonometric function value should be arc length, and the angle should be converted to arc length when using. Get the following code:

import numpy as np

def get_coordinates(a, b, r, angle):
    x = a + r * np.cos(angle*np.pi/180)
    y = b + r * np.sin(angle*np.pi/180)
    return x, y

Then the coordinates of a point with a center coordinate of (0, 0), a radius of 50, and an angle of 10 are as follows:

>>> get_coordinates(0, 0, 50, 10)
(49.2403876506104, 8.682408883346517)

0x02 PyQt5 coordinate arrangement

PyQt5 takes the upper left corner vertex as the origin (0, 0), and the coordinate system x, y extends to the lower right corner. Therefore, it is not recommended to take the dots too close to (0, 0), which may make the circle display abnormal.

There is a circle with a radius of 150 and a center coordinate of (250, 250). There are 20 controls (or buttons) waiting to be laid out on the circle track. Prepare a UI. The old rule is that if you don’t write code, you don’t write it. It is generated by Designer, so no code is posted.
Insert picture description here


Evenly distributed to the circle, each coordinate interval angle is 360°/20=18°
class CircleCtl(QWidget, Ui_circle):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setStyleSheet("QPushButton{border-radius: 15px;background:#ffb2e8}")    # 加点样式好看点
        self.move_pushButtons(250, 250, 150, 18)

    def get_coordinates(self, a, b, r, angle):
        x = a + r * np.cos(angle*np.pi/180)
        y = b + r * np.sin(angle*np.pi/180)
        return x, y

    def move_pushButtons(self, a, b, r, angle):
        interval = angle
        for i in range(1, 21):
            self.__dict__["pushButton_{}".format(i)].move(*(self.get_coordinates(a, b, r, angle)))
            angle += interval

The effect is shown in the figure. Using this method is not necessarily evenly divided. After determining the angle, you can also intercept a circular arc to distribute.

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39177678/article/details/107995405
Recommended