Graphical element processing method and apparatus, electronic device, and storage medium

By expanding line segments into polygons and dividing them into regions, and calculating row lengths and row start coordinates, the problem of high bandwidth consumption in existing line segment drawing is solved, improving drawing efficiency and CPU utilization.

CN114741188BActive Publication Date: 2025-11-25EEASY TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210324274.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-29
Publication Date
2025-11-25
Estimated Expiration
2042-03-29

AI Technical Summary

Technical Problem

Existing line segment drawing techniques frequently call the DMA controller, resulting in high bandwidth consumption, low drawing efficiency, and high costs.

Method used

The target line segment is expanded into a polygon and divided into several regions according to the vertical and horizontal distances. The row length and row starting coordinates of the target point coordinates are calculated, converted into target format data and output to memory space, and drawn in rows.

Benefits of technology

This reduces data transfer volume, avoids additional bandwidth loss, improves line segment drawing efficiency, and frees up CPU resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114741188B_ABST
    Figure CN114741188B_ABST
Patent Text Reader

Abstract

The technical scheme of the present application provides a kind of graphic element processing method, device, electronic equipment and storage medium, it is suitable for line segment drawing, method includes: response line segment drawing instruction, obtain the first endpoint coordinate, second endpoint coordinate and line segment width of target line segment;Calculate the horizontal distance and vertical distance between the first endpoint coordinate and the second endpoint coordinate;Based on line segment width expansion target line segment forms polygon;If vertical distance is less than horizontal distance, divide polygon into several areas;Determine target point coordinate in polygon, calculate the row length of the row where target point coordinate is located;Calculate the row start point coordinate of the row where target point coordinate is located;Convert row length and row start point coordinate into target format data;Output target format data to memory space.This scheme can reduce bandwidth consumption and improve the efficiency of line segment drawing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer graphics processing technology, and in particular to a method, apparatus, electronic device and storage medium for processing graphic elements, applicable to line segment drawing. Background Technology

[0002] Line drawing technology falls under the category of computer graphics. With the diversification of drawing applications on terminals, users have higher expectations for the accuracy and speed of drawing graphics. In the process of drawing graphics, data transfer needs to be completed through the Direct Memory Access (DMA) controller. The DMA controller completes the task of receiving graphics parameters and transferring data to memory space by responding to the transfer action initialized by the central processing unit (CPU).

[0003] There are three existing methods for drawing line segments: CPU drawing, polygon drawing devices, and Blend overlay drawing. However, CPU drawing generates a large number of fragmented command calls, resulting in very low data transfer efficiency and additional bandwidth consumption. Polygon drawing devices expand the line segment into a quadrilateral, divide the quadrilateral into triangles, and then draw the triangles. This method requires an additional buffer, which has a high cost. Blend overlay drawing performs well when drawing horizontal and vertical lines and frees up the CPU, but when drawing diagonal lines, it transmits the entire content of the rectangle whose diagonal is the diagonal, resulting in significant bandwidth loss.

[0004] In summary, existing line segment drawing techniques require frequent calls to the DMA controller and consume significant bandwidth, resulting in reduced drawing efficiency and increased drawing costs. Summary of the Invention

[0005] The following is an overview of the subject matter described in detail herein. This overview is not intended to limit the scope of the claims.

[0006] This invention provides a method, apparatus, electronic device, and storage medium for processing graphic elements, which reduces bandwidth consumption and improves graphic rendering efficiency.

[0007] In a first aspect, embodiments of the present invention provide a method for processing graphic elements, applicable to line segment drawing, including:

[0008] In response to the line segment drawing command, obtain the coordinates of the first endpoint, the coordinates of the second endpoint, and the width of the target line segment;

[0009] Calculate the horizontal and vertical distances between the coordinates of the first endpoint and the coordinates of the second endpoint;

[0010] The target line segment is expanded to form a polygon based on the width of the line segment;

[0011] If the vertical distance is less than the horizontal distance, the polygon is divided into several regions;

[0012] Determine the coordinates of the target point within the polygon, and calculate the row length of the row containing the target point coordinates;

[0013] Calculate the coordinates of the starting point of the row containing the target point;

[0014] Convert the row length and the row start coordinates to the target format data;

[0015] Output the target format data to memory space.

[0016] Secondly, embodiments of the present invention provide a graphic element processing apparatus suitable for line segment drawing, comprising:

[0017] The parameter input module is used to respond to line segment drawing commands and obtain the coordinates of the first endpoint, the second endpoint, and the width of the target line segment.

[0018] The parameter calculation module is used to calculate the horizontal and vertical distances between the coordinates of the first endpoint and the coordinates of the second endpoint, expand the target line segment to form a polygon based on the line segment width, divide the polygon into several regions if the vertical distance is less than the horizontal distance, determine the coordinates of the target point within the polygon, calculate the row length of the row where the target point coordinates are located, and calculate the row start coordinates of the row where the target point coordinates are located.

[0019] The format conversion module is used to convert the line length and the line start coordinates into target format data;

[0020] A memory access module is used to output the target format data to memory space.

[0021] Thirdly, embodiments of the present invention provide an electronic device, the electronic device including a processor, a memory, and one or more programs, the one or more programs being stored in the memory and configured to be executed by the processor, the programs being executed by the processor to implement the graphical element processing method of the first aspect described above.

[0022] Fourthly, embodiments of the present invention provide a computer-readable storage medium, characterized in that it stores processor-executable program instructions, the program instructions being used to execute the graphical element processing method of the first aspect described above.

[0023] The embodiments of the present invention include at least the following beneficial effects: by expanding the width of the target line segment into a polygon, then dividing the polygon into "blocks", and then dividing the "blocks" into "rows", data in units of "rows" is finally generated. These "row" data can be input into the memory space through the DMA controller, so that the central processing unit (CPU) can directly call the "row" data in memory and draw in units of "rows". This can reduce the amount of data that needs to be transmitted, thereby avoiding additional bandwidth loss and improving the efficiency of line segment drawing. In addition, peripheral electronic devices can replace the CPU in completing data transmission tasks based on the DMA mechanism, freeing the CPU to reschedule and process other tasks, thereby further improving the efficiency of line segment drawing. Attached Figure Description

[0024] Figure 1 This is a flowchart illustrating a graphical element processing method provided in an embodiment of the present invention;

[0025] Figure 2 This is a schematic diagram of a line segment expansion effect provided by an embodiment of the present invention;

[0026] Figure 3 This is a schematic diagram of another line segment expansion effect provided by an embodiment of the present invention;

[0027] Figure 4 This is a schematic diagram of a line segmentation result provided in an embodiment of the present invention;

[0028] Figure 5 This is a schematic diagram of an arbitrary row within a polygon provided by an embodiment of the present invention;

[0029] Figure 6 This is a flowchart of a specific embodiment of a graphic element processing method provided by the present invention;

[0030] Figure 7 This is a schematic diagram of a line segment drawing result provided by an embodiment of the present invention;

[0031] Figure 8 This is a structural diagram of a graphic element processing device provided in an embodiment of the present invention;

[0032] Figure 9 This is a system structure diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation

[0033] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0034] It should be understood that if the description of orientation, such as up, down, front, back, left, right, etc., is used in the specification, claims and above drawings, the orientation or positional relationship indicated is based on the orientation or positional relationship shown in the drawings, it is only for the purpose of facilitating the description of the present invention and simplifying the description, and does not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation, and therefore should not be construed as a limitation of the present invention.

[0035] It should be noted that "at least one" means one or more, "more than one" means two or more, "greater than," "less than," and "exceeding" are understood to exclude the number itself, while "above," "below," and "within" are understood to include the number itself. If it is described that "first" and "second" are only used to distinguish technical features, and should not be construed as indicating or implying relative importance, or implicitly indicating the number of technical features indicated, or implicitly indicating the order of the technical features indicated.

[0036] Reference Figure 1 This invention provides a method for processing graphic elements, applicable to line segment drawing, including but not limited to the following steps:

[0037] Step S101: Respond to the line segment drawing command and obtain the coordinates of the first endpoint, the second endpoint, and the line segment width of the target line segment.

[0038] After receiving the instruction to draw the target line segment, the CPU calls the peripheral electronic device. The electronic device responds to the drawing instruction and receives parameters sent by the CPU. These parameters may include the coordinates of the two endpoints of the target line segment, the line segment width, and the line segment color, as specified by the user. These two endpoints can be divided into a first endpoint and a second endpoint. Since the line segment is based on the two-dimensional XY plane, the coordinates of the first endpoint and the second endpoint are positive integers relative to the X and Y axes, respectively. In addition, it is necessary to ensure that the ordinate of the second endpoint is greater than or equal to the ordinate of the first endpoint. In some embodiments, if the coordinates of the first endpoint and the second endpoint entered by the user do not meet the conditions, the client's drawing software can report an error and instruct the user to re-enter them.

[0039] As an example, the coordinates of the first endpoint A are (x0, y0), the coordinates of the second endpoint B are (x1, y1), and are guaranteed to be positive integers and y1≥y0, and the line segment width is w.

[0040] In some embodiments, since different color spaces have odd or even number restrictions on data, for example, in the YUV color space, the width and height of a graphic must be even, but in the RGB color space, the width and height of a graphic can be odd, the line segment width can be preprocessed to meet the requirements of the color space for data; in other embodiments, the converted target format data can also be input into the electronic device as a parameter when the user inputs the line segment color.

[0041] Step S102: Calculate the horizontal and vertical distances between the coordinates of the first endpoint and the coordinates of the second endpoint.

[0042] As an example, based on the example in step S101 above, the horizontal distance dx = |x1-x0| and the vertical distance dy = |y1-y0| between the first endpoint A and the second endpoint B can be determined, where dx is the horizontal distance between the first endpoint A and the second endpoint B, and dy is the vertical distance between the first endpoint A and the second endpoint B.

[0043] It is important to note that since line segments are drawn on digital images using pixels as the drawing unit, both the first and second endpoint coordinates provided by the user must be positive integers. Furthermore, to avoid negative numbers during calculation, it is necessary to ensure that the y-value of the second endpoint is greater than or equal to the y-value of the first endpoint. In some embodiments, if the parameters received by the electronic device do not meet the above conditions, the drawing software can handle the error, providing feedback and reminding the user to re-upload the parameters.

[0044] Step S103: Expand the target line segment to form a polygon based on the line segment width.

[0045] The target line segment to be drawn has width, so it is necessary to expand it based on the line segment width to form a polygonal outline of the target line segment. Different expansion methods can be used depending on the relationship between the vertical and horizontal distances. The expansion width is determined by the user-specified line segment width, and the expansion endpoint is a vertex of the polygon.

[0046] In some embodiments, if the vertical distance is greater than or equal to the horizontal distance, the target line segment is extended parallel to the X-axis with the coordinates of the first endpoint and the second endpoint as centers to form a polygon. The polygon includes a first vertex, a second vertex, a third vertex, and a fourth vertex, including but not limited to the following steps:

[0047] Step S201: Subtract half the line segment width from the x-value of the first endpoint coordinate, and use the difference as the x-value of the first vertex coordinate. Add half the line segment width to the x-value of the first endpoint coordinate, and use the sum as the x-value of the second vertex coordinate. Subtract half the line segment width from the x-value of the second endpoint coordinate, and use the difference as the x-value of the third vertex coordinate. Add half the line segment width to the x-value of the second endpoint coordinate, and use the sum as the x-value of the fourth vertex coordinate. Use the y-value of the first endpoint coordinate as the y-value of the first vertex coordinate and the second vertex coordinate. Use the y-value of the second endpoint coordinate as the y-value of the third vertex coordinate and the fourth vertex coordinate.

[0048] Step S202: Connect the first vertex, the second vertex, the third vertex, and the fourth vertex.

[0049] As an example, such as Figure 2 The diagram illustrates a line segment expansion effect according to an embodiment of the present invention. Specifically, expanding the line segment width by half to the left and right with the first endpoint A as the center yields the first vertex A' and the second vertex A''. Expanding the line segment width by half to the left and right with the second endpoint B as the center yields the third vertex B' and the fourth vertex B'''. Based on the example in step S101 above, the coordinates of the expanded first vertex, second vertex, third vertex, and fourth vertex are A'(x0-w / 2,y0), A'''(x0+w / 2,y0), B'(x1-w / 2,y1), and B'''(x1+w / 2,y1), respectively. Connecting these four vertices yields the polygon A'A'B'B formed by the left and right expansion of the target line segment.

[0050] In other embodiments, if the vertical distance is less than the horizontal distance, the target line segment is extended parallel to the Y-axis with the coordinates of the first endpoint and the second endpoint as centers to form a polygon. The polygon includes a fifth vertex, a sixth vertex, a seventh vertex, and an eighth vertex, including but not limited to the following steps:

[0051] Step S211: Subtract half the line segment width from the y-value of the first endpoint coordinate, and use the difference as the y-value of the fifth vertex coordinate. Add half the line segment width to the y-value of the first endpoint coordinate, and use the sum as the y-value of the sixth vertex coordinate. Subtract half the line segment width from the y-value of the second endpoint coordinate, and use the difference as the y-value of the seventh vertex coordinate. Add half the line segment width to the y-value of the second endpoint coordinate, and use the sum as the y-value of the eighth vertex coordinate. Use the x-value of the first endpoint coordinate as the x-value of the fifth and sixth vertex coordinates, and use the x-value of the second endpoint coordinate as the x-value of the seventh and eighth vertex coordinates.

[0052] Step S212: Connect the fifth, sixth, seventh and eighth vertices.

[0053] As an example, such as Figure 3 The above is a schematic diagram of another line segment expansion effect provided by an embodiment of the present invention. Specifically, expanding the line segment width vertically by half with the first endpoint A as the center yields the fifth vertex A' and the sixth vertex A'''. Expanding the line segment width vertically by half with the second endpoint B as the center yields the seventh vertex B' and the eighth vertex B'''''. Based on the example in step S101 above, the coordinates of the expanded fifth, sixth, seventh, and eighth vertices are A'(x0, y0-w / 2), A'''(x0, y0+w / 2), B'(x1, y1-w / 2), and B'(x1, y1+w / 2), respectively. Connecting these four vertices yields the polygon A'A''B'B formed by the vertical expansion of the target line segment.

[0054] In the above embodiments, the target line segment can be expanded into a quadrilateral, i.e., a regular polygon. In other embodiments, the target line segment can also be expanded into an irregular polygon, with additional quadrilateral protrusions added on the basis of the quadrilateral.

[0055] Step S104: If the vertical distance is less than the horizontal distance, divide the polygon into several regions.

[0056] In some embodiments, if the vertical distance is less than the horizontal distance, dividing the polygon into several regions includes: dividing the polygon into several regions based on the vertical distance and the line segment width, including a first triangular region, a second triangular region, and a quadrilateral region.

[0057] According to the embodiment of step S103 above, the expanded polygon can be divided into two types. The first type is a polygon formed by expanding the target line segment horizontally parallel to the X-axis when the vertical distance is greater than or equal to the horizontal distance. The second type is a polygon formed by expanding the target line segment vertically parallel to the Y-axis when the vertical distance is less than the horizontal distance. If the currently expanded polygon belongs to the second type, it can be divided into several sub-regions: the polygon can be divided by drawing perpendicular lines through the four vertices of the polygon to the Y-axis. Since the relationship between the vertical distance and the line segment width is different for different polygons, the number and shape of the sub-regions contained in the polygon are also different. For example, when the vertical distance is equal to the line segment width, the polygon can be divided into a first triangular region and a second triangular region. However, when the vertical distance is not equal to the line segment width, the polygon can be divided into a first triangular region, a second triangular region, and a quadrilateral region.

[0058] As an example, such as Figure 4As described above, it is a schematic diagram of the line segment segmentation result provided by an embodiment of the present invention. The dotted line is the segmentation line perpendicular to the Y-axis. Specifically, based on the example in the above step S102, when dy < w, the polygon can be divided into three parts, namely the first triangle region 401, the quadrilateral region 402, and the second triangle region 403. Among them, the quadrilateral region 402 is a rectangle; when dy = w, the polygon can be divided into two parts, namely the first triangle region 401 and the second triangle region 403; when dy > w, the polygon can be divided into three parts, namely the first triangle region 401, the quadrilateral region 402, and the second triangle region 403. Among them, the quadrilateral region 402 is a parallelogram.

[0059] Step S105: Determine the coordinates of the target point within the polygon and calculate the row length of the row where the target point coordinates are located.

[0060] Among them, the target point is a certain pixel point on the edge of the polygon; the selection of the target point can be determined according to the actual situation. In the embodiment of the present invention, the target point is the end point or the start point of the row where the target point is located. The coordinates of the target point can be selected on the edge of one side of the polygon, and then according to the horizontal distance, the vertical distance, and the vertex coordinates of the polygon, the row length of the row where the target point is located is calculated.

[0061] Since the smallest unit of a digital image is a pixel point, when drawing a graph, the outline of the graph and the content of the graph are both accumulated by pixel points connected by edges or corners, and the color of the graph is represented by assigning values that conform to the color space format to each pixel point.

[0062] In this embodiment, the row length of the polygon refers to the number of pixel points that make up a certain row within the polygon. By calculating the row lengths of each row within the polygon, it can be stored in the memory space in units of row length. When the central processing unit CPU draws, it can also assign values to each pixel point in units of rows by calling the row length parameter, thereby reducing the data that needs to be transmitted and reducing the bandwidth loss.

[0063] Furthermore, based on the polygons obtained by different expansion methods, the calculation methods of the row lengths within each of them are also different.

[0064] In some embodiments, if the vertical distance is greater than or equal to the horizontal distance, it is determined that the row length of the row where the target point coordinates are located is equal to the line segment width.

[0065] As an example, based on the example in the above step S102, when the vertical distance is greater than or equal to the horizontal distance, that is, dy ≥ dx, then L y = w. Here, L y is the row length of any target point within the polygon, and w is the line segment width.

[0066] In other embodiments, if the vertical distance is less than the horizontal distance, the target point coordinates are determined within the polygon, and the row length of the row containing the target point coordinates is calculated, including but not limited to the following steps:

[0067] Step S301: Select the y-value of the target point coordinates.

[0068] Specifically, the y-value of the target point coordinates can be determined by selecting any integer within a range, with the boundary of the range being the ordinates of the fifth and eighth vertices of the polygon; after determining the y-value of the target point coordinates, the x-value of the target point coordinates can be further determined.

[0069] Step S302: If the y-value of the target point coordinates is within the first triangle region, then calculate the x-value of the target point coordinates based on the y-value of the target point coordinates and the first function, and subtract the x-value of the first endpoint coordinates from the x-value of the target point coordinates to obtain the row length of the row where the target point coordinates are located. Here, the first triangle region is a part of a polygon, and the first function corresponds to the first hypotenuse within the first triangle region.

[0070] The y-value of the target point coordinates is within the first triangular region, which means that the y-value of the target point coordinates is within the range of values ​​in the first triangular region. Since the shapes of the sub-regions segmented based on step S104 are different, the range of values ​​in the first triangular region is also different for different segmentation results. In this embodiment, the target point coordinates are the coordinates of the end point of the row where the target point is located.

[0071] Specifically, firstly, based on the coordinates of the fifth and seventh vertices, a first function can be calculated. The independent variable of the first function is the x-value of any point on the straight line containing the first hypotenuse, and the value of the first function is the y-value of that point. Next, the y-value of the target point can be used as the independent variable to input the inverse function of the first function, and its output function value is the x-value of the target point. After determining the x-value of the target point, the row length of the row containing the target point can be calculated. Since the x-coordinate of the starting point of the row within the first triangular region is fixed and equal to the x-value of the first endpoint, the row length of the row containing the target point can be obtained by subtracting the x-coordinate of the target point from the x-coordinate of the starting point. In some embodiments, the row length of each row within the first triangular region can be obtained by determining the coordinates of each target point individually.

[0072] As an example, such as Figure 5 The diagram shown is a schematic diagram of an arbitrary row within a polygon provided by an embodiment of the present invention, wherein the dashed lines with arrows represent rows, and the dashed lines without arrows represent dividing lines of sub-regions within the polygon. Figure 5In the polygons A'A”B'B”, the vertical distance is less than the horizontal distance. Specifically, the target point p(x) can be selected on the first hypotenuse A'B’ of the first triangular region 501. A'B' ,y A'B' ), where y A'B' The value range of is [y0-w / 2, y1-w / 2]. Since the first hypotenuse A'B' coincides with one side of the polygon A'A”B'B”, and the endpoints of the first hypotenuse A'B' are the fifth vertex A' and the seventh vertex B' of the polygon, and the coordinates of the fifth vertex A' and the seventh vertex B' are known, respectively, A'(x0, y0-w / 2) and B'(x1, y1-w / 2), we can calculate the first function y' = k'x' + b' of the line containing the first hypotenuse A'B', where k' is the slope and b' is the intercept. Based on the expression of the first function, we can calculate its inverse function expression f. (-1) (y')=(y'-b') / k'; Therefore, the target point p(x) A'B' ,y A'B' y in ) A'B' Input the inverse function expression f (-1) (y A'B' ), and we can obtain the corresponding x. A'B' Then, calculate the target point p(x). A'B' ,y A'B' The row length of a given row can be determined using the formula L. y =x A'B' -x0 is implemented, where L y For the target point p(x) A'B' ,y A'B' The line length of the line containing ) x o Let A be the x-coordinate of the first endpoint A.

[0073] In some embodiments, the linear function expression of the line containing the first hypotenuse can be transformed based on the Bresenham algorithm, a line-drawing algorithm that determines an approximation of a straight line between two selected grid points. Since the calculated coordinates are integers, this is generally used to draw line primitives on a bitmap. Using the two endpoints of the line segment as parameters, the calculation process ensures that the ordinate increases positively, while the abscissa may increase positively or negatively. In the embodiments provided by this invention, to ensure that the abscissa always increases positively, when the abscissa of the first endpoint is greater than that of the second endpoint, the abscissas of the two endpoints of the target line segment are swapped before calculation.

[0074] Step S303: If the y-value of the target point coordinates is within the quadrilateral region, if the vertical distance is less than the line segment width, then the row length of the row containing the target point coordinates is equal to the vertical distance; if the vertical distance is greater than the line segment width, then the row length of the row containing the target point coordinates is equal to the line segment width. Here, the quadrilateral region is part of a polygon.

[0075] The statement that the y-value of the target point is within the quadrilateral region means that the y-value of the target point is within the range of values ​​of the quadrilateral region. Since the shapes of the sub-regions segmented based on step S104 are different, the range of values ​​of the quadrilateral region is also different for different segmentation results.

[0076] As an example, refer to Figure 5 You can select any row within the quadrilateral region 502 as the row containing the target point. Specifically, you can select the target point m(x1, y1) on edge BB'. BB' ), where y BB' The range of values ​​for is [y1-w / 2, y1], and y BB' The distance is an integer; if the perpendicular distance of the polygon is less than the width of the line segment, such as... Figure 4 As shown in the left-middle figure, it can be determined that the row length of each target point within the quadrilateral region 402 is equal to the perpendicular distance. If the perpendicular distance of the polygon is greater than the width of the line segment, such as... Figure 4 As shown in the middle right figure, it can be determined that the row length of each target point within the quadrilateral region 402 is equal to the line segment width.

[0077] Step S304: If the y-value of the target point coordinates is within the second triangle region, then determine the x-value of the target point coordinates based on the y-value of the target point coordinates and the second function, and subtract the x-value of the target point coordinates from the x-value of the second endpoint coordinates to obtain the row length of the row where the target point coordinates are located. Here, the second triangle region is part of a polygon, and the second function corresponds to the second hypotenuse within the second triangle region.

[0078] The y-value of the target point coordinates is within the second triangular region, which means that the y-value of the target point coordinates is within the range of values ​​in the second triangular region. Since the shapes of the sub-regions segmented based on step S104 are different, the range of values ​​in the second triangular region is also different for different segmentation results. In this embodiment, the target point coordinates are the coordinates of the starting point of the row where the target point is located.

[0079] Specifically, firstly, based on the coordinates of the sixth and eighth vertices of the polygon, a second function can be calculated. The independent variable of the second function is the x-value of any point on the line containing the second hypotenuse, and the value of the second function is the y-value of that point. Next, the y-value of the target point can be used as the independent variable to input the inverse function of the second function, and its output function value is the x-value of the target point. After determining the x-value of the target point, the row length of the row containing the target point can be calculated. Since the x-coordinate of the row endpoint within the second triangular region is fixed and equal to the x-value of the second endpoint, the row length of the row containing the target point can be obtained by subtracting the x-coordinate of the row endpoint from the x-coordinate of the target point. In some embodiments, the row length of each row within the second triangular region can be obtained by determining the coordinates of each target point individually.

[0080] As an example, refer to Figure 5 Specifically, the target point q(x) can be selected on the second hypotenuse A”B” within the second triangular region 503. A”B” ,y A”B” ), where y A”B” The value range of is [y0+w / 2, y1+w / 2]. Since the second hypotenuse A”B” coincides with one side of polygon A'A”B'B”, and the endpoints of the second hypotenuse A”B” are the sixth vertex A” and the eighth vertex B” of polygon A'A”B'B”, and the coordinates of the sixth vertex A” and the eighth vertex B” are known, namely A”(x0, y0+w / 2) and B”(x1, y1+w / 2), respectively, the second function y”=k”x”+b” of the line containing the second hypotenuse A”B” can be calculated from the coordinates of the sixth vertex A” and the eighth vertex B”, where k” is the slope of the second function and b” is the intercept of the second function. Based on the expression of the second function, its inverse function expression f can be calculated. (-1) (y”)=(y”-b”) / k”; Therefore, the target point q(x) A”B” ,y A”B” y in ) A”B” Input the inverse function expression f (-1) (y A”B” ), and we can obtain the corresponding x. A”B” Then, calculate the target point q(x). A”B” ,y A”B” The row length of a given row can be determined using the formula L. y =x1-x A”B” Implementation, where L y For the target point q(x) A”B” ,y A”B” x1 is the x-coordinate of the second endpoint B, where x is the row length of the row containing x.

[0081] Step S106: Calculate the coordinates of the starting point of the row containing the target point.

[0082] After obtaining the row length of the row containing the target point coordinates within the polygon according to step S105 above, it is also necessary to determine the row starting point coordinates corresponding to the row length. For different types of polygons, the coordinates of the row starting point can be calculated based on different parameters.

[0083] In some embodiments, if the vertical distance is greater than or equal to the horizontal distance, the coordinates of the starting point of the row containing the target point are calculated, including but not limited to the following steps:

[0084] Step S401: Determine the y-value of the starting point coordinate of the row.

[0085] As an example, refer to Figure 2 The y-value of the row starting coordinate can be any integer within the range [y0, y1]. Preferably, the y-value of the row starting coordinate can be initialized to y0 and then incremented to y1. For each row starting coordinate y-value, the corresponding x-value can be calculated according to step S402.

[0086] Step S402: Calculate the x value of the row starting point coordinate based on the y values ​​of the vertex coordinates and the row starting point coordinates of the polygon, wherein the vertices of the polygon include the first vertex, the second vertex, the third vertex, and the fourth vertex.

[0087] Reference Figure 2 Specifically, based on the example in step S103 above, the third function corresponding to the line connecting the first vertex A'(x0-w / 2,y0) and the third vertex B'(x1-w / 2,y1) can be obtained. Then, the x-value of the starting point coordinate of the row can be obtained by inputting the y-value into the inverse function of the third function; preferably, the x-value of the starting point coordinate of the current row can be obtained by incrementally incrementing the y-value of the starting point coordinate of the row and then calculating the x-value of the starting point coordinate of the current row based on the inverse function of the third function, thereby obtaining the starting point coordinates of each row within the polygonal region.

[0088] In some embodiments, if the vertical distance is less than the horizontal distance, the coordinates of the starting point of the row containing the target point are calculated, including but not limited to the following steps:

[0089] Step S411: Determine that the y-value of the starting point coordinate is equal to the y-value of the target point coordinate.

[0090] In step S105 above, the y-value of the target point coordinates has been selected. Since the row starting point in step S106 is the row starting point of the row where the target point is located, the y-value of the target point coordinates can be used as the y-value of the row starting point coordinates.

[0091] Step S412: If the y-value of the row starting point coordinate is less than or equal to the y-value of the sixth vertex coordinate, then determine that the x-value of the row starting point coordinate is equal to the x-value of the first endpoint coordinate. If the y-value of the row starting point coordinate is greater than the y-value of the sixth vertex coordinate, then determine the x-value of the row starting point coordinate based on the y-value of the row starting point coordinate and the vertex coordinates of the polygon. The vertices of the polygon include the fifth vertex, the sixth vertex, the seventh vertex, and the eighth vertex.

[0092] As an example, refer to Figure 5 Specifically, the x-coordinate of the starting point of a row within polygon A'A”B'B” can be calculated in two segments. The first segment is when the starting point falls on edge A'A”, the x-coordinate of the starting point does not change with the y-coordinate, so the x-value of the starting point can always be equal to the x-coordinate x0 of the first endpoint A. The second segment is when the starting point falls on edge A”B”, the x-coordinate of the starting point begins to change with the y-coordinate. The relationship between the x and y coordinates is determined by the second function corresponding to the line A”B” connecting the sixth vertex A” and the eighth vertex B”. The calculation process of the expression of the second function has been explained in step S304 above. By inputting the y-value of the starting point coordinate to the inverse function of the second function, the x-value of the starting point coordinate can be obtained. Thus, corresponding to the row length obtained in step S105, the starting point coordinate of the row where the target point is located can be obtained.

[0093] In some embodiments, after responding to a line segment drawing instruction and obtaining the coordinates of the first endpoint, the second endpoint, and the line segment width of the target line segment, the process includes:

[0094] If the x-value of the first endpoint is greater than the x-value of the second endpoint, then swap the x-values ​​of the first endpoint and the second endpoint and record the swap action.

[0095] If a swap action is detected, then after calculating the starting coordinates of the row where the target point is located, the x-value of the starting coordinates of the row is mapped.

[0096] As an example, based on the example in step S101 above, if x0>x1, then x0 and x1 are swapped, the coordinates of the first endpoint A(x1,y0) and the coordinates of the second endpoint B(x0,y1) are updated, and the swap action is recorded.

[0097] If a swap action is detected, a mapping operation needs to be performed on the row start coordinates to obtain the row start coordinates within the polygons corresponding to the original first endpoint and the original second endpoint.

[0098] As an example, this can be achieved by formulating x' = x A +x B -x y -L yCalculate the starting coordinate of the line when x1 < x0, where: x' is the final horizontal coordinate of the starting point of the line; x A is the horizontal coordinate of the first endpoint; x B is the horizontal coordinate of the second endpoint; x y is the horizontal coordinate of the current starting point of the line; L y is the length of the current line; Through this mapping method, the Bresenham algorithm can also be optimized to ensure that the ordinate increases positively during the drawing process.

[0099] In some embodiments, when the target line segment is extended into an irregular polygon, the implementation method of calculating the line length and the starting coordinate of the line for the regular polygon in the above embodiments can also be used to obtain the line length and the starting coordinate of the line of the polygon on the same horizontal line.

[0100] Step S107: Convert the line length and the starting coordinate of the line into target format data.

[0101] In some embodiments, converting the line length and the starting coordinate of the line into target format data includes:

[0102] Obtain the color space format requirements of the target line segment, and convert the line length and the starting coordinate of the line into target format data according to the color space format requirements.

[0103] Step S108: Output the target format data to the memory space.

[0104] Output the line length and the starting coordinate of the line that meet the format requirements to the memory space and store them as drawing units, so that when the CPU draws a line segment, it can directly extract the target format data in the memory space and draw the line segment in units of the line length and the starting coordinate of the line, improving the line segment drawing efficiency and reducing the bandwidth consumption during the data transmission process.

[0105] As Figure 6 shown, it is a flowchart of a specific embodiment of a graphic element processing method provided by an embodiment of the present invention, which is applicable to the case where the vertical distance is less than the horizontal distance. Among them, starting from the fifth vertex of the polygon, each pixel point on the first hypotenuse is sequentially selected as the target point, and then according to the target point coordinates, the line length of the row where the target point is located is calculated one by one, and then the starting coordinate of the current row is calculated, so as to obtain the line length and the starting coordinate of each row in the polygon. Specifically, it may include but is not limited to the following steps:

[0106] Step S601: Analyze the line segment parameters, including obtaining the coordinates of the first endpoint, the coordinates of the second endpoint, and the line segment width of the target line segment, and determining whether the obtained parameters meet the requirements.

[0107] Step S602: Expand the line segment width, including expanding the target line segment into a polygon according to the line segment width.

[0108] Step S603: Line segment division, which includes dividing the polygon into several sub-regions.

[0109] Step S604: Calculate the line segment row length, which includes initializing the y-value of the target point coordinates and calculating the row length of the row containing the target point coordinates.

[0110] Step S605: Calculate the row starting point coordinates, which includes calculating the row starting point coordinates of the row where the target point is located in step S604.

[0111] Step S606: Determine whether the row containing the target point in step S605 is the last row within the polygon. If yes, proceed to step S607. If not, increment the y-value of the target point coordinates by one and return to step S604 to perform the step of calculating the row length of the row containing the target point.

[0112] Step S607: Target format conversion, which includes converting the line length and line start coordinates into target format data according to the format requirements of the color space where the target line segment is located.

[0113] Step S608: Output target format data.

[0114] Specifically, in step S604, the y-value of the target point coordinates is initialized, and the y-value of the target point coordinates is equal to the y-value of the fifth vertex coordinates. In step S606, it is determined whether the row where the target point is located is the last row inside the polygon. This can be achieved by determining whether the y-value of the target point coordinates is equal to the y-value of the eighth vertex coordinates. In step S608, the length of each row that conforms to the target format and the corresponding row starting coordinates are transmitted and stored in the memory space.

[0115] like Figure 7 The diagram illustrates a line segment drawing result according to an embodiment of the present invention. The left diagram shows the case where the x-coordinate of the first endpoint is less than the x-coordinate of the second endpoint, and the right diagram shows the case where the x-coordinate of the first endpoint is greater than the x-coordinate of the second endpoint. Dashed lines represent rows within a polygon, and the arrows of the dashed lines represent the endpoints of those rows. The CPU can directly access data from memory to complete the line segment drawing task. For example, it can draw a line segment based on the starting coordinates of the row and the row length. Figure 7 The row indicated by the dashed line with an arrowhead.

[0116] The embodiments of the present invention include at least the following beneficial effects: After initializing the line segment drawing task, different polygons can be formed by expanding the target line segment in different directions according to the horizontal and vertical distances between its two endpoints. For different polygons, corresponding segmentation strategies, row length calculation steps, and row start coordinate calculation steps can be adopted. Finally, the row start coordinates and corresponding row lengths within the polygons are output to the memory space after format conversion as "row" data. Thus, the central processing unit (CPU) can directly call the "row" data in memory and draw the target line segment in units of "rows", thereby improving the line segment drawing efficiency and avoiding additional bandwidth loss.

[0117] and Figure 1 Corresponding to the method implementation examples, Figure 8 This is a structural diagram of a graphic element processing device provided in an embodiment of the present invention. The device may include:

[0118] The parameter input module 801 is used to respond to line segment drawing instructions and obtain the coordinates of the first endpoint, the second endpoint, and the line segment width of the target line segment.

[0119] The first endpoint and the second endpoint are the two endpoints of the target line segment to be drawn. The central processing unit (CPU) sends drawing instructions and drawing parameters, including the first endpoint, the second endpoint, and the line segment width input module. The parameter input module can receive and confirm the instructions and parameters for subsequent processing.

[0120] The parameter calculation module 802 is used to calculate the horizontal and vertical distances between the coordinates of the first endpoint and the coordinates of the second endpoint. It expands the target line segment based on the line segment width to form a polygon. If the vertical distance is less than the horizontal distance, the polygon is divided into several regions. The coordinates of the target point are determined within the polygon. The row length of the row containing the target point coordinates is calculated. The starting coordinates of the row containing the target point coordinates are calculated.

[0121] The format conversion module 803 is used to convert line length and line start coordinates to target format data.

[0122] The memory access module 804 is used to output target format data to memory space.

[0123] The memory access module 804 can use Direct Memory Access (DMA) technology to input target format data into the memory space, so that the central processing unit (CPU) can directly call the target format data in the memory space to draw line segments.

[0124] Reference Figure 9This invention provides a system architecture diagram of an electronic device, including a memory 901, a central processing unit 902, an electronic device 903, and a bus 904. The memory 901, the central processing unit 902, and the electronic device 903 communicate with each other through the bus 904. The electronic device 903 includes a memory 9031 and a processor 9032. The memory 9031 is used to store computer programs. When the processor 9032 executes the program stored in the memory, it implements the graphics element processing method provided in this invention.

[0125] This invention provides a computer-readable storage medium storing instructions that, when executed on a computer, cause the computer to perform the graphical element processing method provided in this invention.

[0126] The processors mentioned above can be general-purpose processors, including CPUs (Central Processing Units), NPs (Network Processors), etc.; they can also be DSPs (Digital Signal Processors), ASICs (Application Specific Integrated Circuits), FPGAs (Field-Programmable Gate Arrays), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.

[0127] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0128] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on its differences from other embodiments. In particular, the device embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments. The above descriptions are merely preferred embodiments of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.

Claims

1. A method for processing graphic elements, applicable to line segment drawing, characterized in that, include: In response to the line segment drawing command, obtain the coordinates of the first endpoint, the coordinates of the second endpoint, and the width of the target line segment; Calculate the horizontal and vertical distances between the coordinates of the first endpoint and the coordinates of the second endpoint; The target line segment is expanded to form a polygon based on the width of the line segment; If the vertical distance is less than the horizontal distance, the polygon is divided into several regions; Determine the coordinates of a target point within the polygon, and calculate the row length of the row containing the target point; wherein, the target point is a point on the edge of the polygon; Calculate the coordinates of the starting point of the row containing the target point; Convert the row length and the row start coordinates to the target format data; Output the target format data to memory space; Wherein, if the vertical distance is less than the horizontal distance, the polygon is divided into several regions, including: when the vertical distance is not equal to the line segment width, the polygon is divided into a first triangular region, a second triangular region, and a quadrilateral region; The step of determining the coordinates of the target point within the polygon and calculating the row length of the row containing the target point coordinates includes: If the vertical distance is greater than or equal to the horizontal distance, then the row length of the row containing the target point coordinates is determined to be equal to the line segment width; If the vertical distance is less than the horizontal distance, then determining the target point coordinates within the polygon and calculating the row length of the row containing the target point coordinates specifically includes: Select the y-value of the target point coordinates. If the y-value of the target point coordinates is within the first triangle region, then based on the y-value of the target point coordinates and the first function, the x-value of the target point coordinates is calculated, and the x-value of the first endpoint coordinates is subtracted from the x-value of the target point coordinates to obtain the row length of the row containing the target point coordinates. Here, the first triangle region is a part of the polygon, and the first function corresponds to the first hypotenuse within the first triangle region. If the y-value of the target point's coordinates lies within the quadrilateral region, and if the vertical distance is less than the line segment width, then the row length of the row containing the target point's coordinates is determined to be equal to the horizontal distance; if the vertical distance is greater than the line segment width, then the row length of the row containing the target point's coordinates is determined to be equal to the line segment width. The quadrilateral region is a portion of the polygon. If the y-value of the target point coordinates is within the second triangle region, then the x-value of the target point coordinates is calculated based on the y-value of the target point coordinates and the second function, and the x-value of the target point coordinates is subtracted from the x-value of the second endpoint coordinates to obtain the row length of the row where the target point coordinates are located, wherein the second triangle region is a part of the polygon, and the second function corresponds to the second hypotenuse within the second triangle region.

2. The graphic element processing method according to claim 1, characterized in that, The step of expanding the target line segment based on the line segment width to form a polygon includes: If the vertical distance is greater than or equal to the horizontal distance, the target line segment is extended parallel to the X-axis with the first endpoint coordinates and the second endpoint coordinates as centers to form the polygon. The polygon includes a first vertex, a second vertex, a third vertex, and a fourth vertex, specifically comprising: The difference between the x-value of the first endpoint coordinate and half the line segment width is used as the x-value of the first vertex coordinate. The sum of the x-value of the first endpoint coordinate and half the line segment width is used as the x-value of the second vertex coordinate. The difference between the x-value of the second endpoint coordinate and half the line segment width is used as the x-value of the third vertex coordinate. The sum of the x-value of the second endpoint coordinate and half the line segment width is used as the x-value of the fourth vertex coordinate. The y-value of the first endpoint coordinate is used as the y-value of the first vertex coordinate and the second vertex coordinate. The y-value of the second endpoint coordinate is used as the y-value of the third vertex coordinate and the fourth vertex coordinate. Connect the first vertex, the second vertex, the third vertex, and the fourth vertex. If the vertical distance is less than the horizontal distance, the target line segment is extended parallel to the Y-axis with the first endpoint coordinates and the second endpoint coordinates as centers to form the polygon. The polygon includes a fifth vertex, a sixth vertex, a seventh vertex, and an eighth vertex, specifically comprising: The difference between the y-value of the first endpoint coordinate and half the line segment width is used as the y-value of the fifth vertex. The sum of the y-value of the first endpoint coordinate and half the line segment width is used as the y-value of the sixth vertex. The difference between the y-value of the second endpoint coordinate and half the line segment width is used as the y-value of the seventh vertex. The sum of the y-value of the second endpoint coordinate and half the line segment width is used as the y-value of the eighth vertex. The x-value of the first endpoint coordinate is used as the x-value of the fifth and sixth vertex coordinates, and the x-value of the second endpoint coordinate is used as the x-value of the seventh and eighth vertex coordinates. The fifth vertex, the sixth vertex, the seventh vertex, and the eighth vertex are then connected.

3. The graphic element processing method according to claim 2, characterized in that, The calculation of the row starting coordinates of the row containing the target point includes: If the vertical distance is greater than or equal to the horizontal distance, then calculating the row starting coordinates of the row containing the target point specifically includes: Determine the y-value of the starting coordinate of the row. Calculate the x-value of the row starting point coordinate based on the vertex coordinates of the polygon and the y-value of the row starting point coordinate. If the vertical distance is less than the horizontal distance, then calculating the row starting coordinates of the row containing the target point specifically includes: The y-value of the starting point coordinate of the row is determined to be equal to the y-value of the target point coordinate. If the y-value of the row starting point coordinate is greater than the y-value of the fifth vertex coordinate, then the x-value of the row starting point coordinate is determined to be equal to the x-value of the first endpoint coordinate. If the y-value of the row starting point coordinate is less than or equal to the y-value of the fifth vertex coordinate, then the x-value of the row starting point coordinate is determined based on the y-value of the row starting point coordinate and the vertex coordinates of the polygon.

4. The graphic element processing method according to claim 1, characterized in that, After obtaining the coordinates of the first endpoint, the second endpoint, and the width of the target line segment in response to the line segment drawing instruction, the process includes: If the x-value of the first endpoint coordinate is greater than the x-value of the second endpoint coordinate, then the x-values ​​of the first endpoint coordinate and the second endpoint coordinate are swapped, and the swap action is recorded. If the exchange action is detected, and if so, the x-value of the row starting point coordinate is mapped after the row starting point coordinate of the target point coordinate is calculated.

5. The graphic element processing method according to claim 1, characterized in that, The conversion of the line length and the line start coordinates to the target format data includes: Obtain the color space format requirements of the target line segment, and convert the line length and the coordinates of the line start point into the target format data according to the color space format requirements.

6. A graphic element processing device, suitable for line segment drawing, characterized in that, include: The parameter input module is used to respond to line segment drawing commands and obtain the coordinates of the first endpoint, the second endpoint, and the width of the target line segment. The parameter calculation module is used to calculate the horizontal and vertical distances between the coordinates of the first endpoint and the coordinates of the second endpoint, expand the target line segment based on the line segment width to form a polygon, divide the polygon into several regions if the vertical distance is less than the horizontal distance, determine the coordinates of the target point within the polygon, calculate the row length of the row containing the target point coordinates, and calculate the starting coordinates of the row containing the target point coordinates; wherein, the target point is a point on the edge of the polygon; The format conversion module is used to convert the line length and the line start coordinates into target format data; The memory access module is used to output the target format data to the memory space; Wherein, if the vertical distance is less than the horizontal distance, the polygon is divided into several regions, including: when the vertical distance is not equal to the line segment width, the polygon is divided into a first triangular region, a second triangular region, and a quadrilateral region; The step of determining the coordinates of the target point within the polygon and calculating the row length of the row containing the target point coordinates includes: If the vertical distance is greater than or equal to the horizontal distance, then the row length of the row containing the target point coordinates is determined to be equal to the line segment width; If the vertical distance is less than the horizontal distance, then determining the target point coordinates within the polygon and calculating the row length of the row containing the target point coordinates specifically includes: Select the y-value of the target point coordinates. If the y-value of the target point coordinates is within the first triangle region, then based on the y-value of the target point coordinates and the first function, the x-value of the target point coordinates is calculated, and the x-value of the first endpoint coordinates is subtracted from the x-value of the target point coordinates to obtain the row length of the row containing the target point coordinates. Here, the first triangle region is a part of the polygon, and the first function corresponds to the first hypotenuse within the first triangle region. If the y-value of the target point's coordinates lies within the quadrilateral region, and if the vertical distance is less than the line segment width, then the row length of the row containing the target point's coordinates is determined to be equal to the horizontal distance; if the vertical distance is greater than the line segment width, then the row length of the row containing the target point's coordinates is determined to be equal to the line segment width. The quadrilateral region is a portion of the polygon. If the y-value of the target point coordinates is within the second triangle region, then the x-value of the target point coordinates is calculated based on the y-value of the target point coordinates and the second function, and the x-value of the target point coordinates is subtracted from the x-value of the second endpoint coordinates to obtain the row length of the row where the target point coordinates are located, wherein the second triangle region is a part of the polygon, and the second function corresponds to the second hypotenuse within the second triangle region.

7. An electronic device comprising a processor, a memory, and one or more programs stored in the memory and configured to be executed by the processor, the programs including methods for performing a graphics element processing method as described in any one of claims 1-5.

8. A computer-readable storage medium, characterized in that, The system stores processor-executable program instructions for performing the graphics element processing method as described in any one of claims 1-5.

Citation Information

Patent Citations

  • Device and method for realizing line segment to triangle drawing in GPU (Graphic Processing Unit)

    CN108510565A

  • Method for realizing any-angle rotation of 2D graph and medium

    CN113284053A