Method for drawing rounded rectangle at any angle on Qt platform

By establishing the corresponding calculation process on the Qt platform and calling the DrawArc and LineTo methods, the problem of complex calculation of drawing arbitrary angle rounded rectangles is solved, and efficient and accurate drawing effect is achieved.

CN119672171APending Publication Date: 2025-03-21SICHUAN CHANGHONG EDUCATION TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411737428.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-29
Publication Date
2025-03-21

AI Technical Summary

Technical Problem

The prior art requires complex calculations and tedious operations when drawing arbitrary rounded rectangles on the Qt platform, making it difficult to achieve efficient and accurate drawing.

Method used

By establishing the arc-connected rectangle calculation process, the arc starting point and end point position calculation process, and the arc starting angle calculation process in the Qt platform, input the three vertex coordinates and rounded corner radius of the rounded rectangle, automatically calculate and output the arc center, starting point and end point position and starting angle of the arc, and call the DrawArc and LineTo methods to draw the rounded rectangle.

Benefits of technology

It realizes the rapid and accurate drawing of rounded rectangles at any angle on the Qt platform, improving work efficiency and drawing experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119672171A_ABST
    Figure CN119672171A_ABST
Patent Text Reader

Abstract

The invention mainly relates to the technical field of Qt platform graph drawing. The invention provides a method for drawing a rounded rectangle at any angle on a Qt platform. The method comprises the following steps: automatically outputting the center of a circular arc, the length, the width and the left upper corner coordinates of a circumscribed rectangle in a circular arc circumscribed rectangle calculation process; the arc start-stop position calculation process is used for outputting the start position and the end position of the arc according to the circle center position and the fillet radius of the arc, the arc start angle calculation process is used for calculating the arc start angle according to the determined arc start position, and the start angle is the included angle between the arc start point and the horizontal direction; the Qt platform calls a DrawArc method to respectively draw four rounded corners of a rounded rectangle based on the calculated length and width of the external rectangle of the arc, the coordinates of the upper left corner, the starting point position, the ending point position and the starting angle of the arc, and then calls a LineTo method to connect the four drawn rounded corners to obtain the rounded rectangle; and the drawing efficiency and the application range of the rounded rectangle in the Qt platform can be effectively improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention mainly relates to the technical field of Qt platform graphics drawing, and in particular to a method for drawing a rounded rectangle with any angle on the Qt platform. Background Art

[0002] In the field of graphics drawing, rounded rectangle is a common graphic element. The existing drawing method requires first drawing an unrotated rounded rectangle, and then inputting the rotation angle to achieve the effect of drawing a rotated rounded rectangle. Therefore, it is necessary to calculate the rotation angle, which often requires complex calculations and tedious operations, making it difficult to achieve efficient and accurate drawing. In order to meet the needs of users in different scenarios, it is particularly important to provide a convenient method to draw rounded rectangles of any angle. Summary of the invention

[0003] Technical Problems to be Solved by the Invention

[0004] Provides a method for drawing rounded rectangles of arbitrary angles on the Qt platform, which is used to improve the convenience and efficiency of drawing rounded rectangles on the Qt platform.

[0005] The technical solution adopted by the present invention to solve the above technical problems

[0006] A method for drawing a rounded rectangle with arbitrary angles on a Qt platform, which is applicable to drawing a rounded rectangle with three vertices and a rounded radius of a known circumscribed rectangle, including:

[0007] In the Qt platform, the calculation process of the arc circumscribed rectangle, the calculation process of the arc start and end positions, and the calculation process of the arc starting angle are established respectively;

[0008] The three vertex coordinates and the corner radius of the circumscribed rectangle of the rounded rectangle are input into the established arc circumscribed rectangle calculation process, and the arc circumscribed rectangle calculation process automatically outputs the center of the arc, the length, width and upper left corner coordinates of the circumscribed rectangle of the arc; the arc starting and ending position calculation process is used to output the starting point coordinates and the end point coordinates of the arc according to the center position and the corner radius of the arc, and the position and length of the side of the circumscribed rectangle of the rounded rectangle; the arc starting angle calculation process is used to calculate the starting angle of the arc according to the determined starting point position of the arc, and the starting angle is the angle between the starting point of the arc and the horizontal direction;

[0009] The Qt platform calls the DrawArc method based on the calculated length, width, and upper left corner coordinates of the arc circumscribed rectangle, as well as the arc starting point position coordinates, end point position coordinates, and arc starting angle to draw the four corners of the rounded rectangle, and then calls the LineTo method to connect the four drawn rounded corners to obtain a rounded rectangle.

[0010] Furthermore, the arc circumscribed rectangle calculation process includes: respectively obtaining vectors from a vertex P to an adjacent vertex of the circumscribed rectangle of the rounded rectangle, recorded as vector n and vector m; performing vector addition on vector n and vector m to obtain vector L, normalizing vector L and multiplying it by The starting point of vector D is point P, and the end point of vector D is the coordinate of the center of the arc. After the coordinate of the center of the arc is moved to the left by the length of the corner radius, and then moved upward by the length of the corner radius, the position is the upper left corner position of the circumscribed rectangle of the arc. The width and height of the circumscribed rectangle of the arc are twice the corner radius.

[0011] Furthermore, the process of calculating the starting and ending points of the arc includes: taking the coordinates of the center of the arc as the starting point, obtaining the intersection points with the circumscribed rectangle of the rounded rectangle after moving a circle radius in a direction parallel to and opposite to vector n and vector m, and taking the point first swept by a horizontal straight line rotating counterclockwise to the right as the starting point of the arc, and the point swept later as the end point of the arc.

[0012] Furthermore, the arc starting angle calculation process includes: calculating the angle between the side of the circumscribed rectangle of the arc rectangle where the arc rectangle starting point is located and the plumb line passing through point P, the size of the angle being the arc starting angle.

[0013] Furthermore, the method for calculating the angle is: calculate the horizontal difference dx and the vertical difference dy from point P to the other vertex of the side of the circumscribed rectangle of the arc rectangle where the starting point of the arc rectangle is located, calculate the radian value of the angle based on dx and dy, and convert the radian value into an angle value.

[0014] Beneficial effects of the present invention

[0015] The method for drawing a rounded rectangle with any angle on a Qt platform described in the present invention respectively establishes a circular arc circumscribed rectangle calculation process, a circular arc starting point and end point position calculation process, and a circular arc starting angle calculation process in the Qt platform. The Qt platform can quickly and accurately draw a rounded rectangle that meets the requirements when only the three vertices of the circular arc circumscribed rectangle and the rounded corner radius are known, thereby improving work efficiency and drawing experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] Figure 1 A schematic diagram of three vertices of a rectangle circumscribing a rounded rectangle in an embodiment;

[0017] Figure 2 Schematic diagram of the arc-shaped circumscribed rectangle of the embodiment;

[0018] Figure 3 Schematic diagram of the arc starting angle in the embodiment. DETAILED DESCRIPTION

[0019] A method for drawing a rounded rectangle with arbitrary angles on a Qt platform, which is applicable to drawing a rounded rectangle with three vertices of the circumscribed rectangle and the rounded radius of the rounded rectangle known, including:

[0020] In the Qt platform, the calculation process of the arc circumscribed rectangle, the calculation process of the arc starting and end positions, and the calculation process of the arc starting angle are established respectively;

[0021] The three vertex coordinates and the fillet radius of the circumscribed rectangle of the rounded rectangle are input in the established arc circumscribed rectangle calculation process, and the arc circumscribed rectangle calculation process automatically outputs the center of the arc, and the length, width and upper left corner coordinates of the circumscribed rectangle; the arc start and end position calculation process is used to output the starting position and end position of the arc according to the center position of the arc and the fillet radius, and the arc starting angle calculation process is used to calculate the arc starting angle according to the determined arc starting position, and the starting angle is the angle between the arc starting point and the horizontal direction;

[0022] The Qt platform calls the Arc method based on the calculated length and width of the arc circumscribed rectangle, the coordinates of the upper left corner, and the arc starting point, end point and starting angle to draw the four corners of the rounded rectangle respectively, and then calls the LineTo method to connect the four drawn rounded corners to obtain the rounded rectangle.

[0023] 1. Determine the fourth vertex based on the three known vertices of the circumscribed rectangle of the rounded rectangle: Figure 1 As shown, in this embodiment, it is assumed that the three vertices of the circumscribed rectangle of the rounded rectangle are respectively p1, p2 and p3, wherein p1 and p3 are points on the same diagonal line of the circumscribed rectangle of the rounded rectangle, and the fourth vertex p4 of the circumscribed rectangle of the rounded rectangle is determined as follows:

[0024] (1) Using the coordinates of p1 and p3, find the coordinates of the midpoint of the diagonal where p1 and p3 are located, recorded as midPoint;

[0025] (2) p4 and p2 are also symmetric about midPoint, so the coordinates of p4 are 2*midPoint-p2.

[0026] 2. The determination of the rounded corner radius needs to take into account the width and height of the circumscribed rectangle of the rounded rectangle. The rounded corner radius cannot exceed half of the smaller value of the width and height of the circumscribed rectangle of the rounded rectangle, otherwise the rounded corner will exceed the rectangular range.

[0027] 3. Algorithms for various parameters of the arc part

[0028] First, you need to draw a rounded corner completely, and then determine the coordinate positions of the start and end points of the rounded corners of the other three vertices through mirror symmetry and central symmetry. Here, we take p1 as the starting point to describe the drawing of the rounded corner at position p1. To use Qt's DrawArc method to draw rounded corners, you need to know the circumscribed rectangle of the circle of the rounded corner, the starting angle of the arc, and the rotation angle of the arc.

[0029] 3.1 The circumscribed rectangle of the arc

[0030] To determine the circumscribed rectangle of an arc, you need to determine the coordinates of the upper left corner of the circumscribed rectangle, as well as the width and height of the circumscribed rectangle. To achieve the above calculation, you first need to determine the coordinate position of the center of the arc. Figure 1 As shown, starting from p1, through vector subtraction, we get the vector from p1 to p2, recorded as p1Top2Vector. Similarly, we get the vector from p1 to p3, recorded as p1Top3Vector. Add the vectors p1Top2Vector and p1Top3Vector, and record the result as p1TowVector. p1TowVector must pass through the center of the arc. Normalize p1ToVector and multiply it by the fillet radius Get the coordinates of the center of the fillet r1, take r1 as the starting point, move upward and left by the length of the fillet radius to get the coordinates of the upper left corner of the circumscribed rectangle of the arc part, that is Figure 2 At point C in the figure, the width and height of the circumscribed rectangle of the arc part are twice the radius of the fillet.

[0031] 3.2 Starting angle of the arc part

[0032] like Figure 3 As shown in the figure, the starting angle of the arc part is the angle between the line from the starting point of the arc to the center of the circle and the positive semi-axis direction of the x-axis (with the horizontal right direction as the positive direction of the x-axis), that is, Figure 3 The value of angle A is equal to the angle between p1Top2Vector and the positive direction of the Y axis (the vertical downward direction of the plumb line), that is, Figure 3 Angle B in the circle. The specific proof is as follows: draw a perpendicular line from the center of the arc to p1Top2Vector and p1Top3Vector, record the intersection points of the perpendicular line with p1Top2Vector and p1Top3Vector as m and n respectively, record the lines connecting the center of the circle with m and n as mr1 and nr1 respectively, and determine the intersection point of the center of the circle with p1Top2Vector in the positive direction of the y-axis, record it as point s, and record the line connecting the center of the circle with point s as sr1. Because sr1 / / p1y, mr1 / / p1n, angle A and angle B are equal.

[0033] Angle B is calculated as follows:

[0034] (1) Calculate the coordinate difference: First, calculate the horizontal difference dx and the vertical difference dy from point P to the other vertex of the side of the circumscribed rectangle of the arc rectangle where the starting point of the arc rectangle is located;

[0035] (2) Calculate the angle (radian): Use the qAtan2 inverse tangent function qAtan2(dx, dy) to calculate and return a radian value, which represents the angle from the positive direction of the Y axis to this line in a counterclockwise direction.

[0036] (3) Convert to degrees: Since the angle parameter of DrawArc is in units of 1 / 16 degrees, converting radians to degrees is more convenient for calculation. Use the qRadiansToDegrees function to convert radians to degrees.

[0037] Step 3.2, draw a line from the center of the circle to the tangent point. Because sr1 / / p1y, mr1 / / p1n, angle A and angle B are equal. Therefore, the starting angle A of the fillet arc is converted to the angle between the vector p1Top2Vector and the Y axis, that is, angle B. That is, find the angle between p1Top2Vector and the Y axis.

[0038] (4) Adjust the angle range: Make sure the calculated angle is between 0 and 360 degrees. If the angle is a negative number, add 360 degrees to adjust it.

[0039] 3.3 Angle of the arc sweep

[0040] Normally, the sweep angle of the arc is 90 degrees.

[0041] 3.4 Arc start and end coordinates

[0042] The coordinates of the starting point of the arc can be calculated by r1 and p1Top3Vector. The calculation method is: starting from the position of r1, move the distance of the fillet radius in the opposite direction of the p1Top3Vector vector, and the result is the coordinates of the starting point of the arc. Similarly, the coordinates of the end point of the arc can be calculated. Starting from the position of r1, move the distance of the fillet radius in the opposite direction of the p1Top2Vector vector, and the result is the coordinates of the end point of the arc. Figure 3 As shown, to obtain the coordinates of the tangent point m between the arc and the arc rectangle, we only need to start from r1 and move the radius length in the opposite direction of p1Top3Vector, that is, move the vector r1m from point r1. The vector r1m and the vector p3p1 are in the same direction. The method for calculating the coordinates of the other tangent point n is similar.

[0043] The Qt platform calls the DrawArc method to draw the rounded corner at p1 based on the calculated length, width, and upper left corner coordinates of the arc's circumscribed rectangle, as well as the coordinates of the arc's starting position m, the coordinates of the arc's end position n, and the arc's starting angle A.

[0044] 4. Algorithms for the parameters at the remaining three rectangle vertices

[0045] After obtaining the arc circumscribed rectangle parameters and the arc start and end coordinates at p1, the corresponding parameters at p3 can be obtained by centrosymmetry. The corresponding parameters at p2 can be obtained by simply adding the coordinates of the point at p1 to the vector p1To p2Vector. The corresponding parameters at p4 can be obtained by centrosymmetry by symmetrically combining the parameters at p2 with respect to midPoint.

[0046] The starting angles of the arcs at the other three vertices can be increased by 90 degrees from the previous vertex. That is, the starting angle of the rounded corner at p2 is the starting angle of the arc at p1 + 90 degrees, the starting angle of the rounded corner at p3 is the starting angle of the arc at p2 + 90 degrees, and the starting angle of the rounded corner at p4 is the starting angle of the arc at p3 + 90 degrees. Therefore, all the parameters of the four corners of the rounded rectangle can be obtained. Then call the LineTo method in the Qt platform to connect the rounded corners of the four corners to draw a rounded rectangle.

Claims

1. A method for drawing a rounded rectangle with arbitrary angles on a Qt platform, which is applicable to drawing a rounded rectangle with three vertices and a rounded radius of a known circumscribed rectangle, and is characterized in that: include: In the Qt platform, the calculation process of the arc circumscribed rectangle, the calculation process of the arc start and end positions, and the calculation process of the arc starting angle are established respectively; The three vertex coordinates and the corner radius of the circumscribed rectangle of the rounded rectangle are input into the established arc circumscribed rectangle calculation process, and the arc circumscribed rectangle calculation process automatically outputs the center of the arc, the length, width and upper left corner coordinates of the circumscribed rectangle of the arc; the arc starting and ending position calculation process is used to output the starting point coordinates and the end point coordinates of the arc according to the center position and the corner radius of the arc, and the position and length of the side of the circumscribed rectangle of the rounded rectangle; the arc starting angle calculation process is used to calculate the starting angle of the arc according to the determined starting point position of the arc, and the starting angle is the angle between the starting point of the arc and the horizontal direction; The Qt platform calls the DrawArc method based on the calculated length, width, and upper left corner coordinates of the arc circumscribed rectangle, as well as the arc starting point position coordinates, end point position coordinates, and arc starting angle to draw the four corners of the rounded rectangle, and then calls the LineTo method to connect the four drawn rounded corners to obtain a rounded rectangle.

2. A method for drawing a rounded rectangle with arbitrary angles on a Qt platform according to claim 1, characterized in that: The arc circumscribed rectangle calculation process includes: respectively obtaining vectors from a vertex P to an adjacent vertex of the circumscribed rectangle of the rounded rectangle, recorded as vector E and vector F; performing vector addition on vector E and vector F to obtain vector L, normalizing vector L and multiplying it by The starting point of vector D is point P, and the end point of vector D is the coordinate of the center of the arc. After the coordinate of the center of the arc is moved to the left by the length of the corner radius, and then moved upward by the length of the corner radius, the position is the upper left corner position of the circumscribed rectangle of the arc. The width and height of the circumscribed rectangle of the arc are twice the corner radius.

3. A method for drawing a rounded rectangle with arbitrary angles on a Qt platform according to claim 1, characterized in that: The process of calculating the starting and ending points of the arc includes: taking the coordinates of the center of the arc as the starting point, obtaining the intersection points with the circumscribed rectangle of the rounded rectangle after moving a circle radius in a direction parallel to and opposite to vector n and vector m, and taking the point first swept by a horizontal rightward straight line in a counterclockwise rotation as the starting point of the arc, and the point swept later as the end point of the arc.

4. A method for drawing a rounded rectangle with arbitrary angles on a Qt platform according to claim 1, characterized in that: The arc starting angle calculation process includes: calculating the angle between the side of the circumscribed rectangle of the arc rectangle where the arc rectangle starting point is located and the plumb line passing through point P, and the size of the angle is the arc starting angle.

5. A method for drawing a rounded rectangle with arbitrary angles on a Qt platform according to claim 4, characterized in that: The angle is calculated by calculating the horizontal difference dx and the vertical difference dy from point P to the other vertex of the side of the circumscribed rectangle of the arc rectangle where the starting point of the arc rectangle is located, calculating the radian value of the angle based on dx and dy, and converting the radian value into an angle value.