Crayon stroke drawing method and device based on track generation
By using real-time acquisition and spline curve construction combined with random texture bitmap rendering, the problem of unnatural texture distribution in crayon strokes was solved, improving the realism of digital painting and user experience.
Patent Information
- Application Number
- CN202511611673.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-05
- Publication Date
- 2026-02-03
AI Technical Summary
In existing digital painting techniques, the texture distribution of crayon strokes lacks a sense of naturalness and realism, resulting in poor visual effects.
By collecting the original path points on the user's drawn trajectory in real time, constructing spline curves and performing equidistant interpolation, and combining them with random seeds to generate texture bitmaps, the textures are drawn according to the tangent direction of the target points to create a crayon stroke effect.
It achieves the natural graininess and smooth trajectory of crayon strokes, enhancing the realism of digital drawing and the user experience.
Smart Images

Figure CN121458818A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of digital painting technology, and in particular to a crayon stroke drawing method and apparatus based on trajectory generation. Background Technology
[0002] In the field of digital painting, drawing board applications are applications that allow users to draw, doodle, and create, typically deployed on multiple platforms such as mobile devices, tablets, and personal computers. The core function of these applications is to provide drawing tools such as crayons, pencils, and watercolors. The realism and natural texture of the brushstrokes are key to enhancing the user experience and creative expression. As users draw on the application interface, the drawing board application can reproduce the brushstrokes to present the drawing effect.
[0003] In existing technologies, the following steps are generally used to reproduce the effect of handwriting: first, the drawing trajectory of the user on the canvas is determined, and then a texture bitmap is generated along the trajectory path. However, the distribution of texture points in the above methods is often fixed or regular, which cannot simulate the random distribution of texture in real brushstrokes. This results in brushstrokes lacking a natural and realistic feel. Summary of the Invention
[0004] This application provides a crayon stroke drawing method and apparatus based on trajectory generation to solve the problem that the texture distribution of strokes in traditional drawing methods lacks naturalness and realism.
[0005] In a first aspect, embodiments of this application also provide a crayon stroke drawing method based on trajectory generation. The method includes: responding to a user's drawing operation on a drawing board application interface, collecting original path points on the current stroke's trajectory in real time at a preset frequency to form an original path point set; constructing a spline curve corresponding to the stroke trajectory using the original path point set, the spline curve including at least one sub-segment, the sub-segment being constructed by a linear interpolation equation, a second-order Bézier curve equation, or a third-order Bézier curve equation; performing equidistant interpolation on multiple sampling points on the spline curve to form a target point set; wherein, the target point set includes multiple target points, the multiple target points including multiple sampling points and multiple interpolation points formed by interpolation; drawing a texture bitmap at the corresponding position of each target point according to the tangent direction of the target point to form a crayon stroke effect, the texture bitmap being dynamically generated based on a random seed.
[0006] In one possible implementation, a spline curve corresponding to the pen stroke trajectory is constructed using the original path point set, including: real-time counting of the number of points in the original path point set; the number of points in the original path point set increases as the user's pen stroke moves until the user issues a stop drawing operation; if the original path point set includes only a first number of original path points, sub-segments between the first number of original path points are determined using a linear interpolation equation; if the original path point set includes at least a second number of original path points, sub-segments between the 1st to the Mth original path points are determined using a second-order Bézier curve equation; where M equals the second number and the second number is greater than the first number; if the original path points in the original path point set are greater than the second number, sub-segments after the Mth original path point are determined using a third-order Bézier curve equation; and the sub-segments are spliced together to form a spline curve until the user issues a stop drawing operation.
[0007] In one possible implementation, the sub-segments after the Mth original path point are determined using the third-order Bézier curve equation, including: determining a four-point group, which includes four consecutive original path points; and determining the first and second control points corresponding to the four-point group based on the control point formula. The control point formulas include: C1 = Pi + (Pi + 1 - Pi - 1) / 6; C2 = Pi+1 - (Pi+2 - Pi) / 6; Wherein, C1 represents the first control point, C2 represents the second control point, Pi-1 represents the first original path point in the four-point group, Pi represents the second original path point in the four-point group, Pi+1 represents the third original path point in the four-point group, and Pi+2 represents the fourth original path point in the four-point group. The interpolation step size is calculated based on the distance between adjacent dots and empirical coefficients. The empirical coefficients are determined based on the current writing speed and / or stroke parameters. The formula for calculating the interpolation step size is as follows: step=min(0.05,K / distance(Pi,Pi+1)); Where step represents the interpolation step size, K represents the empirical coefficient, distance(Pi,Pi+1) represents the distance between adjacent points, Pi represents the second original path point in the four-point group, and Pi+1 represents the third original path point in the four-point group. Based on the coordinates of the four-point group and the equation of the third-order Bézier curve, the interpolation parameters are iteratively evaluated according to the interpolation step size to determine the coordinates of the sampling points corresponding to the four-point group; the equation of the third-order Bézier curve is as follows: B(t3)=(1-t3)³×Pi×(1-t3)²×t3×C1×(1-t3)×t3²×C2+t3³×Pi+1 (t3∈[0,1]); Where B(t3) represents the coordinates of the sampling point corresponding to the four-point group; Pi represents the second original path point in the four-point group, and Pi+1 represents the third original path point in the four-point group; C1 represents the first control point, C2 represents the second control point; t3 represents the interpolation parameter, with a value range of [0,1]. Sub-segments are generated based on sampling points.
[0008] In one possible implementation, the process of determining sub-segments after the Mth original path point using the third-order Bézier curve equation until the user issues a stop drawing operation also includes: cyclically executing the following steps S1-S2: S1: Using a sliding window, determine the (Y-1)th to (Y+2)th original path points in the original path point set; using the third-order Bézier curve equation, determine the sub-segments between the Yth and (Y+1)th original path points; and jump to step S2; where the initial value of Y is equal to M; the (Y-1)th to (Y+2)th original path points are used to form a four-point group to generate the first control point and the second control point, thereby determining the sub-segments used to generate the sub-segments. Sampling points of the line segment; the Yth and Y+1th original path points are used to determine the interpolation step size, and the Yth original path point is the starting point of the sub-line segment, and the Y+1th original path point is the ending point of the sub-line segment; S2: If the user does not issue a stop drawing operation, increment the value of Y by 1 and jump to step S1; If the user issues a stop drawing operation, determine whether Y is less than or equal to the target value. If Y is less than or equal to the target value, increment the value of Y by 1 and jump to step S1. If Y is greater than the target value, end the loop; The target value is determined by the total number of original path points in the original path set.
[0009] In one possible implementation, before splicing the sub-segments into a spline curve, the method further includes: when the user issues a stop drawing operation, using the second-order Bézier curve equation to determine the sub-segments between the (T-2)th and Tth original path points, or using the linear interpolation equation to determine the sub-segments between the (T-1)th and Tth original path points; wherein the Tth original path point is the last one in the set of original path points.
[0010] In one possible implementation, equidistant interpolation is performed on multiple sampling points on the spline curve to form a target point set, including: traversing the sampling point set and executing the following steps S3-S6; wherein, the sampling point set includes all sampling points corresponding to each sub-segment; S3: calculate the Euclidean distance Δs between the z-th pair of adjacent sampling points, and calculate the sum of the Euclidean distance Δs and the cumulative distance S to obtain the total cumulative distance. Where z is initially set to 1, and the cumulative distance S is initially set to 0; S4: If the cumulative total distance... If the distance is less than the equidistant sampling interval d, then the cumulative distance S is updated to the cumulative total distance. And, increment the value of z by 1, and jump to step S3; S5: If the total distance is accumulated If the distance between the first interpolation point and the target interpolation point is greater than or equal to the equidistant sampling interval d, determine the first interpolation point. The Euclidean distance between the first interpolation point and the target interpolation point is equal to the equidistant sampling interval d. If the first interpolation point exists, the target interpolation point is the first interpolation point before the current first interpolation point. Otherwise, the target interpolation point is the first sampling point. Reset the cumulative distance S to 0 and update the newly inserted first interpolation point to the starting point of the z-th pair of adjacent sampling points, and then jump to step S3. S6: When traversing to the last sampling point, if the cumulative distance S corresponding to it is less than the equidistant sampling interval d, end the loop.
[0011] In one possible implementation, after traversing the sampling point set and executing steps S3-S6, the method further includes: after the traversal ends, determining the first sampling point and each first interpolation point as equidistant sampling points to form an equidistant sampling point set; using the equidistant sampling point set as the target point set; or, inserting multiple second interpolation points between each pair of equidistant sampling points in the equidistant sampling point set; using the equidistant sampling points in the equidistant sampling point set and each second interpolation point inserted into the equidistant sampling point set as target points to form a target point set; the coordinates of the second interpolation points are determined based on the following formula: ; in, Indicates the coordinates of the second interpolation point. Let be the coordinates of the starting point in a pair of equally spaced sampling points. t4 is the coordinate of the endpoint of a pair of equidistant sampling points, and t4 is the interpolation coefficient. t4 is cyclically evaluated within its range with an incrementing step size, which is determined based on the Euclidean distance between the pair of equidistant sampling points, the pen stroke parameters, and / or the device performance parameters.
[0012] In one possible implementation, after the traversal ends, the sampling points in the sampling point set and each first interpolation point inserted into the sampling point set are taken as equidistant sampling points to form an equidistant sampling point set. The implementation further includes: calculating the tangent direction angle corresponding to each pair of adjacent equidistant sampling points; drawing the texture bitmap at the corresponding position of each target point, including: for each target point, determining the rectangular drawing area corresponding to the target point based on the spatial coordinates of the target point in the canvas coordinate system, where the canvas coordinate system refers to the coordinate system corresponding to the drawing operation; drawing the texture bitmap within the rectangular drawing area; wherein the center point of the texture bitmap is aligned with the target point, the texture bitmap is rotated by a first angle around its center point, and the portion of the texture bitmap exceeding the rectangular drawing area is not drawn, where the first angle is the tangent direction angle of the adjacent equidistant sampling points corresponding to the target point.
[0013] In one possible implementation, the original path point set, spline curve, and target point set grow as the user's pen strokes.
[0014] Secondly, this application also provides a crayon stroke drawing device based on trajectory generation. The device includes: a sampling module configured to: in response to a user's drawing operation on the drawing board application interface, collect the original path points on the current stroke trajectory in real time at a preset frequency to form an original path point set; a curve generation module configured to: construct a spline curve corresponding to the stroke trajectory using the original path point set, the spline curve including at least one sub-segment, the sub-segment being constructed by a linear interpolation equation, a second-order Bézier curve equation, or a third-order Bézier curve equation; an interpolation module configured to: perform equidistant interpolation on multiple sampling points on the spline curve to form a target point set; wherein, the target point set includes multiple target points, the multiple target points including multiple sampling points and multiple interpolation points formed by interpolation; and a third drawing module configured to: draw a texture bitmap at the corresponding position of each target point according to the tangent direction of the target point to form a crayon stroke effect, the texture bitmap being dynamically generated based on a random seed.
[0015] As can be seen from the above, this application provides a crayon stroke drawing method and apparatus based on trajectory generation. The method includes: responding to a user's drawing operation on a drawing board application interface, collecting original path points on the current stroke's trajectory in real time at a preset frequency to form an original path point set; constructing a spline curve corresponding to the stroke trajectory using the original path point set, the spline curve including at least one sub-segment, the sub-segment being constructed by a linear interpolation equation, a second-order Bézier curve equation, or a third-order Bézier curve equation; performing equidistant interpolation on multiple sampling points on the spline curve to form a target point set; wherein, the target point set includes multiple target points, the multiple target points including multiple sampling points and multiple interpolation points formed by interpolation; drawing a texture bitmap at the corresponding position of each target point according to the tangent direction of the target point to form a crayon stroke effect, the texture bitmap being dynamically generated based on a random seed. This application embodiment dynamically generates a texture bitmap with random particles, combines linear interpolation and multi-order Bézier curves to generate a smooth trajectory, then optimizes the distribution of target points using equidistant sampling and intermediate point interpolation, and finally performs texture fitting through direction adaptation, achieving a natural graininess and smooth trajectory of crayon strokes, as well as consistency between the distribution of texture bitmap and the direction of the stroke trajectory. This solves the problems of strong mechanical feel and stiff fitting of traditional fixed textures, and improves the realism of digital drawing and user experience. Attached Figure Description
[0016] Figure 1 A flowchart illustrating the crayon stroke drawing method based on trajectory generation provided in this application embodiment; Figure 2A schematic diagram illustrating the process of constructing spline curves provided in an embodiment of this application; Figure 3 A flowchart illustrating the interpolation process provided in an embodiment of this application; Figure 4 A schematic diagram of the sampling points and the first interpolation points provided in the embodiments of this application; Figure 5 This is a schematic diagram of sampling points and equidistant sampling points provided in the embodiments of this application; Figure 6 A schematic diagram illustrating the process of applying a texture bitmap in an embodiment of this application; Figure 7 This is a schematic diagram of the structure of the crayon stroke drawing device based on trajectory generation provided in an embodiment of this application. Detailed Implementation
[0017] To enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of this application.
[0018] To address the issue that the relatively fixed distribution of texture points during trajectory mapping makes it impossible to simulate the random distribution of textures in realistic brushstrokes, resulting in a lack of naturalness and realism in the brushstrokes, this application provides a crayon brushstroke drawing method based on trajectory generation. This method can collect original path points in real time, construct spline curves using linear interpolation, second-order or third-order Bézier curves, then interpolate the sampled points of the curves to form a target point set, and finally draw the texture bitmap dynamically generated based on random seeds onto each target point according to the direction angle to achieve a natural crayon brushstroke effect.
[0019] Figure 1 This is a flowchart illustrating the crayon stroke drawing method based on trajectory generation provided in an embodiment of this application.
[0020] like Figure 1 As shown, the crayon stroke drawing method based on trajectory generation provided in this application embodiment may include the following steps S100-S400.
[0021] S100: In response to the user's drawing operation on the drawing board application interface, it collects the original path points on the current stroke trajectory in real time according to the preset frequency, forming an original path point set.
[0022] The drawing operation is triggered by touch events, which can include touch events such as ACTION_DOWN (press), ACTION_MOVE (move), and ACTION_UP (release). As the user draws, touch events are continuously generated and captured in real time, forming an input stream synchronized with the screen refresh rate. The specific process is as follows: The ACTION_DOWN event is triggered when a finger or stylus first touches the screen. In this embodiment, a new stroke object can be created and assigned a globally unique strokeId; and the coordinates of the touch point (original path point) can be recorded. and timestamp .
[0023] After that, the buffer can be initialized to prepare for receiving subsequent events.
[0024] The ACTION_MOVE event is triggered continuously at a frequency of 60 Hz to 240 Hz during finger or stylus movement.
[0025] ACTION_UP event: Triggered the moment the finger is lifted.
[0026] Furthermore, the preset frequency is the sampling rate of the original path points, which can be determined based on the following factors: ① Device hardware capabilities: Different touch screens have different hardware sampling rates. High-end devices can have a raw path point sampling rate of 120Hz or even 240Hz, while ordinary devices can have a raw path point sampling rate of 60Hz.
[0027] ② Android system touch event mechanism: The Android system uses MotionEvent to transmit touch information. The frequency of these events is affected by both system scheduling and hardware sampling rate.
[0028] ③ Application event handling capability: Even if the hardware can provide high-frequency sampling, the preset frequency will still be limited by the application's processing capability.
[0029] In summary, the preset frequency is, for example, 30Hz, 60Hz, 120Hz or 240Hz, and the embodiments of this application do not specifically limit it.
[0030] Furthermore, step S100 collects the original path points of the user's touch trajectory at high frequency, providing basic data for subsequent flat processing. Specifically, in this embodiment, the original path points are collected in real time in response to the user's drawing operations on the canvas interface (such as finger swiping or stylus movement) by listening to ACTJON_JOWN (press) and ACTJON_MOVE (move) events. The spatial coordinates (xj, yj) and timestamp tj corresponding to each original path point are recorded as Pj=(xj, yj, tj). These points directly reflect the user's real-time drawing trajectory and are the original data for subsequent curve reconstruction. It can be understood that the spatial coordinates (xj, yj) corresponding to the original path points are located in the canvas coordinate system.
[0031] It should also be noted that as the drawing and data acquisition operations proceed, the original path point set gradually increases, that is, it grows as the user moves the pen until the user issues a stop drawing operation.
[0032] S200: Construct a spline curve corresponding to the pen stroke trajectory using the original path point set. The spline curve includes at least one sub-segment, which is constructed by a linear interpolation equation, a second-order Bézier curve equation, or a third-order Bézier curve equation.
[0033] This step can use linear interpolation and / or multi-order Bézier curves to smooth the original path point set, while spline curves are fitted curves to the pen stroke trajectory.
[0034] It is worth noting that the embodiments of this application can select different curve fitting methods based on the different numbers of original path points. For example: When the number of original path points is the first number (e.g., equal to 2), linear interpolation equations are used to construct straight line segments to ensure trajectory continuity.
[0035] When the number of original path points is the second number (e.g., equal to 3), a second-order Bézier curve is used: with the first point as the starting point, the last point as the ending point, and the intermediate point as the control point, the formula is B(t)=(1-t)²P0+2(1-t)tP1+t²P2, to achieve a smooth transition of the simple curve.
[0036] When the number of original path points is greater than or equal to the third number (e.g., equal to 4), a third-order Bézier curve is used: four consecutive points (Pj-1, Pj, Pj+1, Pj+2) are selected through a sliding window, and control points C1 and C2 are calculated based on the four consecutive points. Then, sub-segments are generated using the formula B(t)=(1-t)³Pj(1-t)²tC1(1-t)t²C2+t³Pj+1 to ensure that the tangent direction of multiple curve segments is consistent at the connection point. High-order continuity is achieved through the Catmull-Rom spline concept.
[0037] The above content is merely an exemplary description of the generation of spline curves in the embodiments of this application. The specific calculation steps will be detailed below and will not be repeated here.
[0038] It is understandable that the spline curve can gradually increase as the drawing and data acquisition operations are performed.
[0039] It should also be noted that, in order to reproduce the effect of crayon drawing, spline curves may be invisible to the user.
[0040] In some implementations, the embodiments of this application may not splice the sub-segments to obtain spline curves, but instead store the sub-segments in a discrete manner. The embodiments of this application do not specifically limit this.
[0041] S300: Perform equidistant interpolation on multiple sampling points on the spline curve to form a target point set; wherein, the target point set includes multiple target points, and the multiple target points include multiple sampling points and multiple interpolation points formed by interpolation.
[0042] It is worth noting that in a spline curve, the sub-segments are connected end to end. Furthermore, the sampling points, also known as trajectory points, can be determined using linear interpolation equations, second-order Bézier curve equations, and / or third-order Bézier curve equations. The specific sampling steps will be detailed below and will not be repeated here.
[0043] Furthermore, the step of performing equidistant interpolation on multiple sampling points can specifically be equidistant sampling interpolation (to obtain equidistant sampling points) and intermediate point interpolation (to obtain intermediate interpolation points). The specific interpolation steps will be detailed below and will not be repeated here.
[0044] It is understandable that the set of target points can gradually increase as drawing and data acquisition operations are performed.
[0045] In summary, the target point set includes equidistant sampling points and intermediate interpolation points, which ensures overall uniformity while improving local continuity through dense interpolation.
[0046] S400: The texture bitmap is drawn on the corresponding position of each target point according to the tangent direction of the target point to form a crayon stroke effect. The texture bitmap is dynamically generated based on a random seed.
[0047] Specifically, in this embodiment, the main color tone and size of the texture bitmap can be determined in response to user operations on the drawing board interface (such as selecting the crayon tool or adjusting brush stroke parameters). Then, based on a random seed, attributes such as the number, coordinates, and radius of the particle points are generated. Finally, these particle point attributes are overlaid to generate a texture bitmap with a semi-transparent crayon particle effect. This texture bitmap can be used as a temporary memory object to achieve natural and unique digital crayon strokes.
[0048] In the process of drawing the texture bitmap at the corresponding position, this embodiment of the application can rotate the texture bitmap by a first angle around its center point, and the portion of the texture bitmap that exceeds the rectangular drawing area corresponding to the target point is not drawn. The first angle is the direction angle corresponding to the target point. The direction angle corresponding to each target point can be equal to the tangent direction angle of its corresponding adjacent sampling point. In this way, direction adaptation can be achieved. The specific drawing steps will be detailed below and will not be repeated here.
[0049] As can be seen, the embodiments of this application generate a uniform target point set by equidistant sampling and intermediate point interpolation, and combine it with the tangent direction angle to make the texture bitmap rotate and fit along the trajectory tangent direction, which can ensure that the brush stroke can extend naturally on any path.
[0050] As can be seen from the above, this application provides a crayon stroke drawing method based on trajectory generation. The method includes: responding to the user's drawing operation on the drawing board application interface, collecting the original path points on the current stroke trajectory in real time according to a preset frequency to form an original path point set; using the original path point set to construct a spline curve corresponding to the stroke trajectory, the spline curve including at least one sub-segment, the sub-segment being constructed by a linear interpolation equation, a second-order Bézier curve equation, or a third-order Bézier curve equation; performing equidistant interpolation on multiple sampling points on the spline curve to form a target point set; wherein, the target point set includes multiple target points, the multiple target points including multiple sampling points and multiple interpolation points formed by interpolation; drawing a texture bitmap at the corresponding position of each target point according to the tangent direction of the target point to form a crayon stroke effect, the texture bitmap being dynamically generated based on a random seed. This application embodiment dynamically generates a texture bitmap with random particles, combines linear interpolation and multi-order Bézier curves to generate a smooth trajectory, then optimizes the distribution of target points using equidistant sampling and intermediate point interpolation, and finally performs texture fitting through direction adaptation, achieving a natural graininess and smooth trajectory of crayon strokes, as well as consistency between the distribution of texture bitmap and the direction of the stroke trajectory. This solves the problems of strong mechanical feel and stiff fitting of traditional fixed textures, and improves the realism of digital drawing and user experience.
[0051] It is worth noting that in step S200, the number of points in the original path point set P = {P0, P1, P2, ..., Pn} is first determined, and an appropriate processing strategy is selected based on the number of points. The input to this step is the original path point set P = {P0, P1, P2, ..., Pn}, and the output is the sampled point set (i.e., the smooth curve point set) Q.
[0052] Figure 2 This is a schematic diagram of the process for constructing spline curves provided in an embodiment of this application.
[0053] Furthermore, such as Figure 2As shown, step S200 includes the following steps S201-S203.
[0054] S201: Real-time statistics of the number of points in the original path point set; the number of points in the original path point set increases as the user moves the pen until the user issues a stop drawing operation.
[0055] During the user drawing process (when the ACTION_MOVE event is continuously triggered), this application embodiment can first initialize an empty result point set (sample point set) Q={}, and then determine the size of the original path point set P, that is, count the number of original path points in real time.
[0056] It is worth noting that the embodiments of this application distinguish different situations and perform different steps based on the different number of points.
[0057] Different scenarios can include: ① the original path point set includes only the first number of original path points; ② the original path point set includes at least the second number of original path points; ③ the original path point set contains more than the second number of original path points. ④ When the user lifts the pen, this application embodiment can further refine the scenario based on the number of points at the end of the pen stroke trajectory.
[0058] In some implementations, the first quantity can be equal to 2, and the second quantity can be equal to 3. The different processing steps for each case will be detailed below.
[0059] It is important to emphasize that different cases are distinguished based on the number of original path points, because there are several special scenarios in the drawing process: ① Short strokes: The user only draws a very short stroke, and fewer than four points are collected. For example, a quick click or tap on the screen may only collect 1-3 points.
[0060] ② Special characteristics of the first segment: At the beginning of drawing, only the preceding points can be obtained, and the subsequent points are not yet available. For example, when the user first starts drawing, there may only be three points: P0, P1, and P2, but not the fourth point P3.
[0061] ③ Special characteristics of the tail segment: Similarly, at the end of the drawing, there are no more subsequent points. For example, only three points, Pn-2, Pn-1, and Pn, remain.
[0062] In addition, in some low-performance devices or special circumstances, the touch sampling rate may be insufficient, resulting in fewer points being collected.
[0063] Therefore, the embodiments of this application can refine different processing strategies based on the different numbers of original path points.
[0064] In some implementations, embodiments of this application can count points at a preset frequency of 30Hz or 60Hz and assign the point count to a counter variable (such as pointCount). This variable is updated in real time as new original path points are added. This counter can serve as the core basis for subsequent steps (S202-S205) to determine the relationship between the point count and the first or second quantity, ensuring that steps S202-S205 can dynamically match the corresponding interpolation method.
[0065] In summary, the embodiments of this application can take into account the diversity of user drawing behavior (short strokes, quick clicks, etc.), the stages of the drawing process (no subsequent points in the first segment, no more points in the last segment), and the differences in device performance (insufficient sampling rate), and ensure that a continuous and smooth pen trajectory can be generated under any circumstances by dynamically adapting interpolation strategies with different number of points.
[0066] S202: When the original path point set includes only a first number of original path points, use a linear interpolation equation to determine the sub-segments between the first number of original path points.
[0067] This situation can occur when the user draws a short stroke or in the early stages of drawing. The original path point set only includes the first number of original path points, which means that when counting points in step S201, the number of points is only the first number, and has not reached the second number. Here, the first number can be equal to 2, and the second number can be equal to 3.
[0068] Furthermore, the linear interpolation equation can be: B(t1) = P0 + t1×(P1-P0)(t1∈[0,1]); Where B(t1) represents the coordinates of the sampled points on the interpolated sub-segment. P0 is the first point in the original path point set, i.e., the starting point of the linear interpolation. P1 is the second point in the original path point set, i.e., the ending point of the linear interpolation. t1 is the interpolation parameter, with a value range of [0,1]. When t1=0, B(t) equals P0; when t1=1, B(t) equals P1; when t1 varies between 0 and 1, all interpolation points between P0 and P1 can be obtained, achieving a continuous transition between the two points.
[0069] For example, when the first quantity is 2, that is, when there are only 2 points P0 and P1, this embodiment of the application uses a linear interpolation method to generate a smooth transition between the two points. The exemplary pseudocode is as follows: If |P| = 2: distance = calculate the distance between P0 and P1 step = Determine the appropriate step size (usually distance / 10). for t = 0 to 1 step step:
[0070]
[0071] Add the point (x, y) to the result set Q. return Q; As can be seen, by using linear interpolation equations, a straight line segment (sub-segment) can be generated between points P0 and P1. The purpose is to ensure the continuity of the basic trajectory when the number of points is very small, and to avoid drawing interruptions due to insufficient points. Linear interpolation ensures that there are enough intermediate points between the two points, making the drawn lines visually smooth enough.
[0072] In some implementations, when the number of points is insufficient, a valid curve cannot be formed. This embodiment of the application can directly return the original point set, which can then be used as the sampling point set to determine the target point. For example, if |P| < 2: return P, then the sampling point set Q = P. This situation typically occurs when the user lightly touches the screen but does not move; this embodiment of the application can treat this as single-point drawing.
[0073] S203: If the set of original path points includes at least the second number of original path points, use the second-order Bézier curve equation to determine the sub-segment between the 1st to the Mth original path points; where M is equal to the second number and the second number is greater than the first number.
[0074] The original path point set includes at least a second number of original path points, indicating that when counting points in step S201, the number of points has exceeded the first number and has at least reached the second number. When the second number equals 3, a sub-segment can be drawn using a second-order Bézier curve for the first 3 points. The starting point of the sub-segment is P0, the control point is P1, and the ending point is P2. In other words, this embodiment of the application can utilize a second-order Bézier curve to address the special characteristics of the first segment in the drawing process.
[0075] Furthermore, the equation of the second-order Bézier curve can be: B(t2) = (1-t2)²×P0 + 2×(1-t2)×t×P1 + t2²×P2 (t2∈[0,1]); Where B(t2) represents the coordinates of the sampling points on the curve generated by the second-order Bézier curve; P0 is the first point in the original path point set, i.e., the starting point of the second-order Bézier curve; P1 is the second point in the original path point set, serving as the control point of the curve, and its position determines the direction and degree of curvature of the curve, affecting the trajectory shape between P0 and P2; P2 is the third point in the original path point set, i.e., the ending point of the second-order Bézier curve, which, together with P0, constitutes the start and end boundaries of the curve. t2 is the interpolation parameter, with a value range of [0,1]. When t2=0, B(t2) equals P0; when t2=1, B(t2) equals P2; when t2 varies between 0 and 1, all curve points (sampling points) between P0 and P2 can be obtained, achieving a smooth transition between the three points.
[0076] It is worth noting that when the number of original path points reaches the second number (e.g., 3), the second-order Bézier curve equation can cover the scenario of the first number (2). By including the first 2 points (P0, P1) of the 3 points in the calculation, it not only realizes the smooth curve drawing from P0 to P2, but also indirectly includes the trajectory segment from P0 to P1, without having to process P0 and P1 separately through linear interpolation.
[0077] For example, when the second quantity is 3, for 3 points P0, P1, P2, this embodiment of the application can use second-order Bézier curve interpolation, where P0 is the starting point, P2 is the ending point, and P1 is the control point. Exemplary pseudocode is as follows: If |P| = 3: / / Calculate the appropriate step size distance = calculate the distance between P0 and P2 K = An empirical coefficient (1.0~3.0) determined based on line width and drawing speed. step = min(0.05, K / distance) / / Perform second-order Bezier interpolation for t = 0 to 1 step step:
[0078]
[0079] Add the point (x, y) to the result set Q. return Q; In this case, P0 is the starting point, P2 is the ending point, and P1 is the control point that determines the shape of the curve. A second-order Bézier curve can generate a smooth curve effect using only three points.
[0080] Further details can be found by referring to [link / reference]. Figure 2 Step S201 may be followed by step S204.
[0081] S204: If the number of original path points in the original path point set is greater than the second number, use the third-order Bézier curve equation to determine the sub-segments after the Mth original path point until the user issues a stop drawing operation.
[0082] As the user's pen strokes move, the number of original path points gradually increases. In order to make the handwriting transition natural, this application embodiment can use a third-order Bézier curve for trajectory fitting, which may specifically include the following steps S2041-S2045.
[0083] S2041: Determine a set of four points, which includes four consecutive original path points.
[0084] In this embodiment, four consecutive path points are selected sequentially from the original path point set using a window sliding method to form a four-point group. For example, the four-point group can be {Pi,Pi+1,Pi+2,Pi+3}, where the value of i can be in the range of [3,n-3], and n is the index of the last original path point in the original path point set.
[0085] S2042: Determine the first and second control points corresponding to the four-point group based on the control point formula; The control point formulas include: C1 = Pi + (Pi + 1 - Pi - 1) / 6; C2 = Pi+1 - (Pi+2 - Pi) / 6; Wherein, C1 represents the first control point, C2 represents the second control point, Pi-1 represents the first original path point in the four-point group, Pi represents the second original path point in the four-point group, Pi+1 represents the third original path point in the four-point group, and Pi+2 represents the fourth original path point in the four-point group.
[0086] S2043: Calculate the interpolation step size based on the distance between adjacent dots and empirical coefficients; the empirical coefficients are determined based on the current writing speed and / or stroke parameters, and the interpolation step size calculation formula is as follows: step=min(0.05,K / distance(Pi,Pi+1)); Where step represents the interpolation step size, K represents the empirical coefficient, distance(Pi,Pi+1) represents the distance between adjacent points, that is, the Euclidean distance between the original path points Pn and Pn+1, Pi represents the second original path point in the four-point group, and Pi+1 represents the third original path point in the four-point group.
[0087] In this embodiment, sampling can be performed using an adaptive interpolation step size. The interpolation step size refers to the interval between the values of the interpolation parameters when calculating sampling points for linear interpolation curves, second-order or third-order Bézier curves, and is used to control the density of sampling points on the sampling curve.
[0088] Specifically, the step size of curve sampling directly affects the density of smoothing points (sampling points) and the smoothness of the curve. The smaller the step size, the denser the values of the interpolation parameters (e.g., taking values from 0 to 1 at intervals of 0.01), the more sampling points are generated, and the smoother the curve is, but the computational cost is greater. The larger the step size, the sparser the values of the interpolation parameters (e.g., taking values from 0 to 1 at intervals of 0.05), the fewer sampling points are generated, and the computational efficiency is higher.
[0089] In this embodiment of the application, the interpolation step size can be a value between [0.01-0.05].
[0090] Furthermore, for fine brushes, slow writing, and a pursuit of high realism, K can be set to [1.0, 1.5]; for thick brushes, fast writing, and performance priority, K can be set to [2.0, 3.0]. In this way, the sampling density can be dynamically optimized according to the actual writing speed and line thickness, improving the user experience.
[0091] When determining the value of K, the current writing speed can be calculated in real time using the spatial distance between adjacent sampling points and the difference in timestamps, and combined with multiple criteria to judge the speed. For example, the formula for calculating the current writing speed can be: Current writing speed = distance(Pi,Pi-1) / (ti-ti-1); Where ti is the timestamp corresponding to the original path point Pi, and ti- is the timestamp corresponding to the original path point Pi-1. In the Android system, each MotionEvent contains timestamp information, which can be obtained directly.
[0092] In some implementations, absolute thresholds can be set for speeds below 5 cm / s as slow and above 20 cm / s as fast. Speed can also be determined by referring to the user's current average speed. For example, speeds below 30% of the average speed can be considered slow. Personalized models can also be built based on historical data.
[0093] In this embodiment of the application, the brush thickness is selected by the user directly through the UI as the line width parameter, and can be divided in units of pixels or density-independent pixels (dp) (e.g., 1-3dp is a fine brush, and 8dp and above is a thick brush).
[0094] As can be seen, by dynamically linking the step size with the real-time collected handwriting speed and the brush thickness set by the user, the embodiments of this application can intelligently optimize the sampling density, using a fine step size to preserve details when writing slowly and a large step size to improve efficiency when writing quickly, ultimately achieving a smooth and natural trajectory effect in different drawing scenarios.
[0095] It must be emphasized that the formula for calculating the interpolation step size applies not only to the value of the interpolation parameter t3 in the third-order Bézier curve equation, but also to the value of the interpolation parameter t1 in the linear curve interpolation equation, and similarly to the value of the interpolation parameter t2 in the second-order Bézier curve equation.
[0096] S2044: Based on the coordinates of the four-point group and the equation of the third-order Bézier curve, the interpolation parameters are iteratively evaluated according to the interpolation step size to determine the coordinates of the sampling points corresponding to the four-point group; the equation of the third-order Bézier curve is as follows: B(t3)=(1-t3)³×Pi×(1-t3)²×t3×C1×(1-t3)×t3²×C2+t3³×Pi+1 (t3∈[0,1]); Where B(t3) represents the coordinates of the sampling point corresponding to the four-point group; Pi represents the second original path point in the four-point group, and Pi+1 represents the third original path point in the four-point group; C1 represents the first control point, C2 represents the second control point; t3 represents the interpolation parameter, with a value range of [0,1]. S2045: Generate sub-segments based on sampling points.
[0097] Understandably, as the interpolation parameter t4 cycles through the interpolation step size, sampling points can be generated point by point. Therefore, sub-segments can extend point by point. It is evident that a sub-segment can pass through its corresponding multiple sampling points.
[0098] Furthermore, if the number of original path points in the original path point set is greater than the second number, this embodiment of the application can use a piecewise third-order Bézier curve for processing. Specifically, based on the sub-segment drawing approach of steps S2041-S2045, step S204 can also include the following step S2046.
[0099] S2046: Repeat the following steps S1-S2.
[0100] S1: Use a sliding window to determine the (Y-1)th to (Y+2)th original path points in the original path point set, use the third-order Bézier curve equation to determine the sub-segment between the Yth and (Y+1)th original path points, and jump to step S2; where the initial value of Y is equal to M; the (Y-1)th to (Y+2)th original path points are used to form a four-point group to generate the first control point and the second control point, and then determine the sampling points used to generate the sub-segment; the Yth and (Y+1)th original path points are used to determine the interpolation step size, and the Yth original path point is the starting point of the sub-segment, and the (Y+1)th original path point is the ending point of the sub-segment.
[0101] It is understandable that when the second quantity is equal to 3, M=Y=3.
[0102] Furthermore, in this embodiment, a sliding window mechanism is adopted, with a sliding window size of 4. Each time, four consecutive points (Pi-1, Pi, Pi+1, Pi+2) are selected to generate a curve segment from Pi to Pi+1.
[0103] For example, the first window takes P2, P3, P4, P5 and generates the sub-segment between P3 and P4. The next window takes P3, P4, P5, P6 and generates the sub-segment between P4 and P5, and so on.
[0104] Furthermore, based on the aforementioned third-order Bézier curve equation, When t3=0, B(t3) equals the starting point Pi; when t3=1, B(t3) equals the ending point Pi+1; when t3 cycles between 0 and 1, all curve points (sampling points) between Pi and Pi+1 can be obtained, achieving a smooth transition between two points. C1 and C2 are two control points of the curve, used to adjust the curvature of the curve. Furthermore, "3(1-t)..." 2 "tC1" represents the influence of the first control point, determining the initial direction and curvature of the curve. "3(1-t)t" 2 "C2" indicates the influence of the second control point, which determines the termination direction and curvature of the curve.
[0105] Furthermore, “(1-t3)³×Pi” represents the weight of the starting point; the smaller t3 is, the closer it is to the starting point. “t³×Pi+1” represents the weight of the ending point; the larger t3 is, the closer it is to the ending point.
[0106] As can be seen, the embodiments of this application can automatically generate control points for Bézier curves based on the Catmull-Rom spline concept. The formula for calculating C1 is: C1 = Pi + (Pi+1 - Pi-1) / 6; thus, the starting tangent can be optimized using the preceding point Pi-1. The formula for calculating C2 is: C2 = Pi+1 - (Pi+2 - Pi) / 6; thus, the ending tangent can be optimized using the following point Pi+2.
[0107] Taking i=3 as an example, based on the aforementioned control point calculation formula, we can determine that the position of C1 is determined by P1 and the vector (P4-P2). This means that the position of P2 directly affects the direction and magnitude of the vector (P4-P2). If the position of P2 is changed, C1 will shift in different directions, thus changing the initial direction of the curve starting from P3. Similarly, the position of C2 is determined by P4 and the vector (P5-P3). Changes in the position of P5 will directly affect the vector (P5-P3), thus changing the position of C2. The position of C2 determines the direction of the curve when it reaches P4.
[0108] From a geometric perspective, there is an important property regarding the direction of the tangent in a third-order Bézier curve: At the starting point P3, the tangent direction of the curve is from P3 to C1.
[0109] At the endpoint P4, the tangent direction of the curve is from C2 to P4.
[0110] Based on the control point calculation formula, C1 is located at P3 offset along the direction (P4-P2) / 6. This means that the tangent direction at P3 is affected by the relative positions of points P2, P3, and P4. In particular, the position of P2 affects the initial direction starting from P3. Similarly, C2 is located at P4 offset along the direction (P5-P3) / 6. The tangent direction at P4 is affected by the relative positions of points P3, P4, and P5. The position of P5 affects the final direction when reaching P4.
[0111] For example, if P3 and P4 are fixed, but P2 is moved: When P2 is to the left of P3, C1 will be biased to the right of P3, and the initial direction of the curve will be to the right; When P2 is above P3, C1 will be biased below P3, and the initial direction of the curve will be downward. Thus, the change in the position of P2 directly affects the direction of the curve starting from P3.
[0112] Similarly, the change in the position of P5 will affect the direction of the curve when it reaches P4.
[0113] In summary, although P2 and P5 do not directly participate in the interpolation calculation of the current segment's third-order Bézier curve, they play a crucial role in the calculation of control points C1 and C2. P2 influences C1 to determine the tangent direction of the current segment's starting point P3, and P5 influences C2 to determine the tangent direction of the current segment's ending point P4. This mechanism ensures that the tangent directions of adjacent curve segments are consistent at the connection point, avoiding inflection points and achieving smooth splicing and natural transition of multiple curve segments, greatly enhancing the realism and aesthetics of digital handwriting.
[0114] S2: If the user does not issue a stop drawing operation, increment the value of Y by 1 and jump to step S1; if the user issues a stop drawing operation, determine whether Y is less than or equal to the target value. If Y is less than or equal to the target value, increment the value of Y by 1 and jump to step S1. If Y is greater than the target value, end the loop. The target value is determined by the total number of points in the original path set.
[0115] In this embodiment, the stop drawing operation is determined by the ACTION_UP event. If the user does not lift the pen, i.e., does not issue a stop drawing operation, the ACTION_UP event will not be triggered. If the user lifts the pen, the ACTION_UP event will be triggered. If the user does not issue a stop drawing operation, the value of Y can be incremented by 1, and the sliding window can then slide forward one point in one step.
[0116] For example, the first window selects P2, P3, P4, and P5 to generate a sub-line segment between P3 and P4. Then, the sliding window can move forward one point to select P3, P4, P5, and P6, and jump back to step S1 to generate the curve from P4 to P5.
[0117] The above process continues to loop until the user lifts the pen and triggers the ACTION_UP event. At this point, it can be determined that the user has stopped drawing and the loop ends.
[0118] For example, when the second quantity is 3, that is, when the number of points is greater than 3, for the four points Pi, Pi+1, Pi+2, Pi+3, this embodiment of the application can use a pair of third-order Bézier curves, where Pi+1 is the starting point and Pi+2 is the ending point. Exemplary pseudocode is as follows: If |P| ≥ 4: / / Sliding window for i = 0 to n-3: Process the four-point group {Pi,Pi+1,Pi+2,Pi+3} to generate the curve segment from Pi+1 to Pi+2. return Q; It is worth noting that i represents the starting index of the sliding window, used to identify the starting position of the currently processed four-point group {Pi, Pi+1, Pi+2, Pi+3}. The value of i is an integer from 0 to n-3 (where n is the index of the last point in the point set P), to ensure that all consecutive four-point groups can be traversed and processed to generate the curve segment from Pi+1 to Pi+2.
[0119] Assuming the index range of the point set P is from 0 to n (a total of n+1 points), then the last window must contain point Pn (i.e. the last point). That is, the last window is {Pn-3, Pn-2, Pn-1, Pn}, with the starting index of the window being n-3 and the ending index being n.
[0120] Furthermore, the loop termination condition is that the loop needs to traverse to the last possible window. Since the starting index of the last window is n-3, the loop termination condition is i ≤ n-3, that is, for i = 0 to n-3. In other words, the target value is actually n-3.
[0121] For example, a sliding window is applied to six points P1, P2, P3, P4, and P5 (n=5), with a loop range of for i = 0 to 5-3, or for i = 0 to 2. The following window will be processed: i=0: Process {P0, P1, P2, P3} to generate the curve segment from P1 to P2; i=1: Process {P1, P2, P3, P4} to generate the curve segment from P2 to P3; i=2: Process {P2, P3, P4, P5} to generate the curve segment from P3 to P4; By sliding the window, all possible groups of four points can be processed, and each point (except the first and last points) participates in the generation of the third-order Bézier curve. In other words, the loop range for i = 0 to n-3 ensures that all possible groups of four points are processed, without attempting to access points outside the array range. The entire point set is completely transformed into a smooth sequence of Bézier curve segments. This design allows the algorithm to efficiently process point sets of arbitrary length and generate continuous and smooth curves.
[0122] Furthermore, after step S204, the following step S205 may also be included: splicing the sub-segments together to form a spline curve.
[0123] It is worth noting that the stitching step is executed dynamically during the real-time rendering process, and the specific logic is as follows: First segment generation: Use linear interpolation to generate sub-segments between P0 and P1, and / or use linear interpolation to generate sub-segments between P0 and P2.
[0124] Subsequent stitching: When the four-point group (P2, P3, P4, P5) is collected, the second sub-segment (P3 to P4) is calculated and drawn, at which point the first stitching is performed. Afterward, each slide of the sliding window triggers a new stitching operation.
[0125] Understandably, the splicing happens instantly, without waiting for the user to complete the entire stroke, ensuring the real-time generation of the curve.
[0126] Furthermore, prior to step S205, this embodiment of the application also includes the following step S206.
[0127] S206: When the user issues a stop drawing operation, the sub-segment between the (T-2)th and Tth original path points is determined using the second-order Bézier curve equation, or the sub-segment between the (T-1)th and Tth original path points is determined using the linear interpolation equation; wherein the Tth original path point is the last one in the set of original path points.
[0128] In this embodiment of the application, when the last sliding window contains four original path points (such as Pn-3, Pn-2, Pn-1, Pn), the process can be divided into two steps: The first step is to generate sub-segments from Pn-2 to Pn-1 using the third-order Bézier curve equation (control points are calculated using standard sliding window logic to ensure smooth connection with the preceding segments); the second step is to process the last two points from Pn-1 to Pn (the endpoints of the original path point set) using linear interpolation equations. This is because there are no more points after Pn available for calculating control points of higher-order curves, and linear interpolation ensures continuity at the end.
[0129] When the last sliding window contains three original path points (such as Pn-2, Pn-1, and Pn), this embodiment of the application can use a second-order Bézier curve equation to generate the sub-segment from Pn-2 to Pn. With Pn-2 as the starting point, Pn as the ending point, and Pn-1 as the control point, the curve provides a natural transition, avoiding the abruptness of a straight-line ending, and is suitable for intermediate scenarios where the number of points in the last sliding window is between 2 and 4.
[0130] As can be seen, the embodiments of this application can utilize linear interpolation, second-order Bézier curves, and third-order Bézier curves to address the special characteristics of the tail segment.
[0131] In summary, steps S201-S206 ensure that smooth, natural curves are generated regardless of the length of the strokes drawn by the user. In particular, the beginning and end of the strokes maintain the same smoothness as the middle sections, achieving seamless transitions.
[0132] In some implementations, the exemplary pseudocode corresponding to steps S201-S206 above is as follows: / / Pseudocode example fun processTouchEvent(event: MotionEvent) { when (event.action) { ACTION_DOWN -> { / / Start a new stroke, clear the dot set points.clear() / / Add the first dot points.add(Point(event.x, event.y, event.eventTime)) } ACTION_MOVE -> { / / Add new points points.add(Point(event.x, event.y, event.eventTime)) / / Select processing method based on current points when { points.size == 2 -> { / / Only two points, use linear interpolation drawLineBetween(points[0], points[1]) } points.size == 3 -> { / / Three points, using a second-order Bézier curve drawQuadraticBezier(points[0], points[1], points[2]) } points.size >= 4 -> { / / Process all complete four-point groups for (i in 0 until points.size - 3) { drawCubicBezier(points[i], points[i+1], points[i+2], points[i+3]) } } } } ACTION_UP -> { / / Add the last point points.add(Point(event.x, event.y, event.eventTime)) / / Process the last remaining points val remainingPoints = points.size % 4 if (remainingPoints > 0) { val startIdx = points.size - remainingPoints when (remainingPoints) { 2 -> drawLineBetween(points[startIdx], points[startIdx+1]) 3 -> drawQuadraticBezier(points[startIdx], points[startIdx+1], points[startIdx+2]) } } } }
[0133] The following section details the steps for sampling and interpolating spline curves.
[0134] Figure 3 This is a flowchart illustrating the interpolation process provided in an embodiment of this application.
[0135] like Figure 3 As shown, step S300 includes the following step S301.
[0136] S301: Traverse the sampling point set and execute the following steps S3-S6. The sampling point set includes all the sampling points corresponding to each of the sub-segments.
[0137] In this embodiment, the sampling points are determined through continuous sampling as the interpolation parameters are cyclically evaluated during the sub-line segment drawing process. In practical applications, this embodiment can first initialize the sampling point set, i.e., pre-create an empty sampling point set, which is used to dynamically store all generated sampling points. Furthermore, this embodiment can either uniformly write the corresponding sampling points into the sampling point set after each sub-line segment is drawn, or write each sampling point into the sampling point set immediately after it is determined; this embodiment does not specifically limit this approach.
[0138] S3: Calculate the Euclidean distance Δs between the z-th pair of adjacent sampling points, and sum the Euclidean distance Δs with the cumulative distance S to obtain the total cumulative distance. ; where the initial value of z is 1, and the initial value of the cumulative distance S is 0.
[0139] Figure 4 Schematic diagram of sampling points and first interpolation points provided by an embodiment of the present application.
[0140] As Figure 4 shown, initially, z = 1, that is, starting from the first pair of adjacent sampling points Q0 and Q1, at this time the cumulative distance S = 0, calculate the Euclidean distance Δs between the first pair of adjacent sampling points Q0 and Q1, and calculate the sum of the Euclidean distance Δs and the cumulative distance S to obtain the cumulative total distance , that is, the cumulative total distance = S + Δs.
[0141] S4: If the cumulative total distance is less than or equal to the equidistant sampling interval d, update the cumulative distance S to the cumulative total distance , and add 1 to the value of z, and jump to step S3; If S + Δs < d (the cumulative distance has not reached the equidistant sampling interval d), update S to S + Δs, and add 1 to z (process the next pair of adjacent points), and return to S3 to continue accumulating.
[0142] Exemplarily, the equidistant sampling interval d can be equal to 30px or 40px, and the embodiments of the present application do not make specific limitations thereto.
[0143] It should be supplemented and explained that Figure 4 in the exemplary case, the equidistant sampling interval d is equal to 40px.
[0144]
[0144]
[0144] It is worth noting that step S4 may specifically include the following steps S41 - S42.
[0145] S41: If the cumulative total distance is less than the equidistant sampling interval d, and in the case where the end point of the z-th pair of adjacent sampling points is not the last sampling point, update the cumulative distance S to the cumulative total distance , and add 1 to the value of z, and jump to step S3; S42: If the cumulative total distance is less than the equidistant sampling interval d, and in the case where the end point of the z-th pair of adjacent sampling points is the last sampling point, jump to step S6; S5: If the cumulative total distance is greater than or equal to the equidistant sampling interval d, determine the first interpolation point; wherein, the Euclidean distance between the first interpolation point and the target interpolation point is equal to the equidistant sampling interval d. In the case where there is a first interpolation point, the target interpolation point is the first interpolation point before the current first interpolation point, otherwise, the target interpolation point is the first sampling point; reset the cumulative distance S to 0, and update the newly inserted first interpolation point to the starting point in the z-th pair of adjacent sampling points, and then jump to step S3.
[0146] If S + Δs ≥ d, it indicates that the interval between adjacent sampling points Q is large, and a first interpolation point F should be inserted between them. The insertion position of the first interpolation point F is: the position where the Euclidean distance from the previous first interpolation point is exactly equal to the equidistant sampling interval d. Afterwards, the first interpolation point F can be added to the set of equidistant sampling points.
[0147] It is understandable that, for a sub-segment, if the remaining distance of the current segment (the segment between the previous first interpolation point and the end point of the sub-segment) is still greater than the equidistant sampling interval d, then interpolation continues within the current segment until the remaining distance is less than the equidistant sampling interval d.
[0148] It should be further explained that step S302 can initialize the sampling point set, determining the starting point of the spline curve as the first interpolation point F0. Simultaneously, this first interpolation point F0 is also the first equidistant sampling point H0; in this embodiment, all three coincide. Therefore, in the absence of a first interpolation point, the target interpolation point is the first sampling point, and the Euclidean distance between the first sampling point and the first first interpolation point is equal to the equidistant sampling interval d. Therefore, in step S5, in the absence of a first interpolation point, the target interpolation point is the first sampling point.
[0149] The formula for calculating the coordinates of the first interpolation point, i.e., the interpolation point formula, is as follows: ; in, Indicates the coordinates of the first interpolation point. For interpolation ratio, Let z be the starting point of the z-th pair of adjacent sampling points. is the endpoint in the z-th pair of adjacent sampling points.
[0150] Furthermore, , .
[0151] in, This represents the x-coordinate of the first interpolation point. Indicates the ordinate of the first interpolation point; ) represents the coordinates of the starting point among adjacent sampling points, ( () represents the coordinates of the endpoint among adjacent sampling points.
[0152] Furthermore, the interpolation ratio It can be calculated based on the following formula: ; in, Indicates the interpolation ratio. Indicates the cumulative distance. This represents the Euclidean distance between adjacent sampling points.
[0153] The interpolation ratio can represent the relative position of the first interpolation point F on the current segment (the line segment between the z-th pair of sampling points). Exemplarily, for a sub-line segment with its starting point being Q0, the z-th pair of adjacent sampling points being Q2 and Q3, assuming the Euclidean distance between Q1 and Q0 is less than d, and the Euclidean distance between Q2 and Q0 is greater than or equal to d. Then, when calculating the interpolation ratio S, the value of S is the Euclidean distance between Q1 and Q0, and the value of is the Euclidean distance between Q2 and Q3.
[0154] S6: When traversing to the last sampling point, if the corresponding cumulative distance S is less than or equal to the equidistant sampling interval d, end the loop.
[0155] It can be understood that step S6 occurs after step S4. Also, the cumulative distance S corresponding to the last sampling point is the Euclidean distance between the last sampling point and its nearest equidistant sampling point. When traversing to the last sampling point, if the cumulative distance S < d, it means that the remaining distance of the current segment is not sufficient to generate a new equidistant point, that is, the Euclidean distance between the last sampling point and the first interpolation point before it is less than the equidistant sampling interval d. At this time, end the loop to ensure that the equidistant sampling points cover the entire trajectory.
[0156] Correspondingly, if the cumulative distance S ≥ d, it means that the remaining distance of the current segment is still sufficient to insert a new first interpolation point. At this time, step S5 should be executed for interpolation instead of step S6.
[0157] Continue to refer to Figure 4 , assuming the equidistant sampling interval d is 40px, the set of sampling points Q corresponding to the sub-line segment is Q = {Q0, Q1, Q2, Q3, Q4}, where the coordinates of Q0 are (10, 10), the coordinates of Q1 are (40, 10), the coordinates of Q2 are (60, 25), the coordinates of Q3 are (70, 50), the coordinates of Q4 are (90, 80), the Euclidean distance between Q0 and Q1 is equal to 30px, the Euclidean distance between Q1 and Q2 is equal to 25px, the Euclidean distance between Q2 and Q3 is equal to 25px, and the Euclidean distance between Q3 and Q4 is equal to 35px. The starting point of this sub-line segment is Q0 and coincides with the first equidistant sampling point H0.
[0158] Furthermore, execute step S302: Traverse the set of sampling points and execute steps S3 - S6.
[0159] First, execute step S3 to calculate the Euclidean distance Δs = 30px between the first pair of adjacent sampling points Q0 and Q1. At this time, the value of the cumulative distance S is the initial value 0. Then, the cumulative total distance = 30 + 0 = 30 px.
[0160] Further, proceed to step S4 to accumulate the total distance. =30px<d, update the cumulative distance S=30px, z=2, and jump to step S3.
[0161] Further, in step S3, the Euclidean distance Δs = 25 px between the second pair of adjacent sampling points Q1 and Q2 is calculated. At this point, the cumulative distance S is 30. Therefore, the total cumulative distance is... =30+25=55 px.
[0162] Further, proceed to step S5 to accumulate the total distance. =55 px > d, so the coordinates of the first interpolation point F1 (i.e., the second equidistant sampling point H1) are determined to be (48, 16). It can be understood that the Euclidean distance between the first interpolation point F1 and the first sampling point Q0 (i.e., the target interpolation point F0) is d = 40 px.
[0163] Then, the cumulative distance S is reset to 0, the first interpolation point F1 is updated to the starting point of the second pair of adjacent sampling points, and then the process jumps to step S3.
[0164] Further, in step S3, the Euclidean distance Δs = 15 px between the second pair of adjacent sampling points F1 and Q2 is calculated. At this point, the cumulative distance S is 0. Therefore, the total cumulative distance is... =15+0=15 px.
[0165] Further, proceed to step S4 to accumulate the total distance. =15 px < d, update the cumulative distance S = 15 px, z = 3, and jump to step S3.
[0166] Further, in step S3, the Euclidean distance Δs = 25 px between the third pair of adjacent sampling points Q2 and Q3 is calculated. At this point, the cumulative distance S is 15 px. Therefore, the total cumulative distance is... =15+25=40 px.
[0167] Further, proceed to step S5 to accumulate the total distance. =40 px =d, determine the coordinates of the first interpolation point F2 (i.e. the second equidistant sampling point H2) as (70,50), which coincides with the sampling point Q3.
[0168] Then, the cumulative distance S is reset to 0, the first interpolation point F2 is updated to the starting point of the third pair of adjacent sampling points, and then the process jumps to step S3.
[0169] Further, in step S3, the Euclidean distance Δs ≈ 36px between the third pair of adjacent sampling points F2 and Q3 is calculated. At this point, the cumulative distance S is 0. Therefore, the total cumulative distance is... =36+0=36px<d.
[0170] Further, perform steps S4 and / or S6 to end the loop.
[0171] It should also be noted that, please continue to refer to [link / reference needed]. Figure 4 When jumping from step S5 to step S3, since the distance between the first interpolation point F1 and the starting point Q0 of the current sub-segment is known to be (ds), the remaining distance of the current segment (the segment between Q1 and Q2) (the distance between the first interpolation point F1 and Q2) is equal to Δs - (dS) = Δs - d + S, where Δs is the Euclidean distance between Q1 and Q2, and S is the Euclidean distance between Q0 and Q1. Furthermore, this remaining distance can be directly used as the new cumulative distance S, meaning there is no need to explicitly calculate the new Euclidean distance between the starting point (first interpolation point F1) and the ending point Q2. Instead, we can directly use "cumulative distance S = remaining distance = Δs - (dS) = Δs - α·Δs = (1 - α)·Δs", where α is the interpolation ratio calculated when generating the first interpolation point F1.
[0172] Step S302 ensures that the algorithm can process every stroke of the user's drawing and generate equidistant sampling points suitable for texture matching.
[0173] Furthermore, after step S302, the following step S303 is also included.
[0174] S303: After the traversal ends, the first sampling point and each first interpolation point are determined as equidistant sampling points to form an equidistant sampling point set.
[0175] Based on the above, it can be seen that the embodiments of this application can traverse the smooth trajectory points (sampling points), interpolate to generate new sampling points, and after completing the interpolation generation of one equidistant sampling point, the loop does not stop, but instead continues to recursively execute the "distance accumulation - interval judgment - interpolation sampling" process on the current segment (the line segment between the new first interpolation point and the next sampling point) and subsequent segments (the line segment between the next sampling point and its subsequent sampling points) until the entire smooth path (sub-segment) is traversed. In other words, this step specifically includes: Update starting point: Take the newly generated sampling point as the new starting point of the current segment (the line segment between the new first interpolation point and the next sampling point), and take the remaining sampling points as the points to be processed later.
[0176] Cumulative Remaining Distance: Calculate the distance from the new starting point to the next trajectory point and continue accumulating. If the cumulative distance reaches or exceeds the set equidistant sampling interval d again, perform interpolation again to generate the next equidistant sampling point.
[0177] Multiple interpolation processing: If the current segment (the line segment between the new first interpolation point and the next sampling point) is long, the sampling interval may be reached multiple times within the same segment. In this case, multiple interpolations need to be performed within the same segment until the remaining distance is less than d.
[0178] Loop recursion: Repeat the above process of "distance accumulation - interval judgment - interpolation sampling" to process all sampling points in turn until the end of the traversal path is reached.
[0179] Termination condition: When all sampling points have been traversed and the cumulative distance is insufficient for the sampling interval, the loop terminates and the final equidistant sampling point set H={H0,H1,……Hk} is output.
[0180] Figure 5 This is a schematic diagram of the sampling points and equidistant sampling points provided in the embodiments of this application.
[0181] With an equidistant sampling interval d of 40px. Figure 5 The relationship between the original smooth trajectory point (sampling point) Q and the equidistant sampling points H is clearly shown.
[0182] It should also be noted that, after step S303, the embodiments of this application may further include the following step S304.
[0183] S304: Calculate the tangent direction angle corresponding to each pair of adjacent equidistant sampling points.
[0184] Understandably, this step is used to calculate H for each pair of adjacent equidistant sampling points. k-1 H k Tangent direction angle between The formula for calculating the tangent direction angle is: ; in,( , )and( , ) represent the coordinates of adjacent equally spaced sampling points.
[0185] Furthermore, after step S303 or step S304, the following step S305 may also be included.
[0186] S305: Use the equidistant sampling point set as the target point set.
[0187] At this point, the equidistant sampling points coincide with the target point.
[0188] In some implementations, step S305 may be followed by the following steps S306-S307.
[0189] S306: Insert multiple second interpolation points between each pair of equidistant sampling points in the equidistant sampling point set.
[0190] The coordinates of the second interpolation point are determined based on the following formula: ; in, Indicates the coordinates of the second interpolation point. Let be the coordinates of the starting point in a pair of equally spaced sampling points. t4 is the coordinate of the endpoint of a pair of equally spaced sampling points, and t4 is the interpolation coefficient, which is cyclically taken within its range with an increasing step size.
[0191] Furthermore, the increment step size is determined based on the Euclidean distance between a pair of equidistant sampling points, stroke parameters, and / or device performance parameters. The increment step size determines the number of interpolation points between each pair of sampling points. The smaller the step size, the more interpolation points, and the smoother the line.
[0192] It is understandable that by substituting the x-coordinates and y-coordinates of the equidistant sampling points into the coordinate calculation formula for the second interpolation point, the x-coordinates and y-coordinates of the second interpolation point can be determined.
[0193] It should also be noted that the increment step size of the interpolation coefficient t4 is... It can adaptively adjust based on parameters such as line segment length and brush density. Typical values are: ; in, This indicates the increment step size of the interpolation coefficient t4. The distance between two equally spaced sampling points is the Euclidean distance. This represents the brush density parameter.
[0194] Furthermore, the brush density parameter controls the density of interpolation points, thus affecting the fullness and graininess of crayon strokes. A smaller brush density parameter results in more interpolation points, leading to fuller, more continuous strokes, suitable for highly realistic and detailed drawing needs; a larger brush density parameter results in fewer interpolation points, higher drawing performance, suitable for fast writing or low-performance devices. In practical applications, the brush density parameter can be dynamically adjusted based on brush type (e.g., thickness, hardness), user settings, and device performance. For example, the brush density parameter can be automatically increased with thicker brushes or at higher resolutions, and appropriately decreased with thinner brushes or at lower resolutions; it can also adaptively adjust based on the user's writing speed, appropriately decreasing the brush density parameter at faster speeds to ensure smoothness.
[0195] For example, the brush density parameter can be 8, 12, 16, etc. The specific value can be set according to actual needs and experimental results. This application embodiment does not make specific limitations on this.
[0196] S307: Take the equidistant sampling points in the equidistant sampling point set and each second interpolation point inserted into the equidistant sampling point set as target points to form a target point set.
[0197] It's worth noting that steps S305-S306 can perform intermediate point interpolation based on equidistant sampling points. This is an optimization and supplement based on the characteristics of equidistant sampling and the need for refined high-fidelity crayon strokes. Specifically, equidistant sampling, as a basic processing method, has already achieved trajectory sampling through uniformly distributed sampling points. The setting of its equidistant sampling interval d comprehensively considers factors such as performance, brush width, and display resolution, and has significant advantages in balancing computational efficiency and trajectory continuity. In high-fidelity crayon stroke rendering scenarios, since the size of crayon texture blocks is usually small, and there are complex situations such as high-speed writing and curve transitions in actual drawing, it may be necessary to further improve the detail representation. For example, when the texture block size is smaller than the equidistant sampling interval d, supplementing the area between adjacent equidistant sampling points with intermediate points can achieve more refined coverage, thereby achieving a "continuous coloring" effect; at locations with drastic trajectory changes, supplementing intermediate points can make the texture fit more tightly, reduce visual transition gaps, and make the strokes appear fuller and more natural.
[0198] As can be seen, the embodiments of this application can further introduce an intermediate point interpolation algorithm based on equidistant sampling. This algorithm generates several intermediate points through adaptive interpolation between sampling points, ensuring seamless coverage and natural transition of texture blocks on the trajectory, greatly improving the visual realism of brushstrokes and the system's rendering performance, and is practical.
[0199] Figure 6 This is a schematic diagram illustrating the process of applying a texture bitmap in an embodiment of this application.
[0200] like Figure 6 As shown, step S400 includes the following steps S401-S402.
[0201] S401: For each target point, determine the rectangular drawing area corresponding to the target point based on the spatial coordinates of the target point in the canvas coordinate system. The canvas coordinate system refers to the coordinate system corresponding to the drawing operation.
[0202] This step calculates the boundary of the texture drawing based on the spatial coordinates of the target point, providing a basis for local refresh and precise fitting. Furthermore, the boundary of the rectangular region is determined by the size of the texture bitmap and the coordinates of the target point, using the following formula: left = target point x-coordinate - texture width / 2; top = target point y coordinate - texture height / 2; right = x-coordinate of target point + texture width / 2; bottom = target point y coordinate + texture height / 2; Where left represents the coordinates of the left boundary of the rectangular drawing area (horizontal starting point), top represents the coordinates of the upper boundary of the rectangular drawing area (vertical starting point), right represents the coordinates of the right boundary of the rectangular drawing area (horizontal ending point), and bottom represents the coordinates of the lower boundary of the rectangular drawing area (vertical ending point).
[0203] S402: Draw a texture bitmap in a rectangular drawing area; wherein the center point of the texture bitmap is aligned with the target point, the texture bitmap is rotated by a first angle around its center point, and the part of the texture bitmap that exceeds the rectangular drawing area is not drawn, the first angle being the tangent direction angle of the adjacent equidistant sampling points corresponding to the target point.
[0204] It is understandable that the step of drawing the texture bitmap at the corresponding position of each target point can also be called the step of "fitting the texture bitmap".
[0205] It should be emphasized that when using the set of equidistant sampling points as the target point set, the direction angle corresponding to the target point during step S400 is the tangent direction angle between its corresponding equidistant sampling points. In other words, the direction angle corresponding to the target point is the tangent direction angle between the current target point and the previous target point. When using the equidistant sampling points in the equidistant sampling point set and each second interpolation point inserted into the equidistant sampling point set as target points to form the target point set, the direction angle corresponding to the target point during step S400 is determined according to the following steps: When the target point is an equidistant sampling point, its direction angle is the tangent direction angle between that point and the previous equidistant sampling point (i.e., the line segment direction calculated based on the original equidistant sampling point). When the target point is the second interpolation point, its direction angle follows the overall direction characteristics of the line segment it belongs to, that is, it is consistent with the tangent direction angle between the target point and a pair of equidistant sampling points (the start and end points of the line segment where the second interpolation point is located).
[0206] In this embodiment, the pen stroke trajectory is the original path drawn by the user. When the user performs a drawing operation on the drawing board interface with their finger or stylus, this embodiment can collect the original path points in real time at a preset sampling frequency (such as 60Hz, 120Hz) as the pen stroke trajectory extends, thus obtaining a set of original path points, denoted as . Subsequently, in this embodiment of the application, line fitting can be performed on the original set of path points to obtain a spline curve. The equidistant sampling points are the result of uniform resampling of the spline curve. The target point is an equidistant sampling point, and / or, the target point is the final drawing reference point obtained after refining and interpolating adjacent equidistant sampling points.
[0207] For example, for each pair of adjacent equidistant sampling points H k-1 H k Calculate the tangent direction angle Direction angle calculation formula: ; in,( , )and( , The coordinates of adjacent equidistant sampling points are shown below. This angle parameter is directly used to rotate the texture bitmap, ensuring that each texture bitmap is strictly aligned with the tangent direction of the current trajectory.
[0208] It should also be noted that when drawing texture bitmaps, portions exceeding the rectangular area (left, top, right, bottom) are automatically cropped, retaining only the pixels within the area. This mechanism ensures a natural transition of textures between adjacent target points in overlapping areas, preventing redundant drawing beyond the boundaries from affecting other trajectories.
[0209] In some implementations, embodiments of this application can also achieve texture orientation adaptation based on matrix transformation. Specifically, for each target point and its corresponding orientation angle, a two-dimensional rotation matrix is used to rotate the texture bitmap so that the principal axis of the texture bitmap is strictly aligned with the current trajectory direction. The rotation matrix formula is as follows: ; in,( ) represents the original coordinates of a pixel on the texture bitmap, ( ) represents the coordinates of the point on the texture canvas after rotation. The coordinates of the target point coincide with the center coordinates of the texture bitmap. This indicates the tangent direction angle corresponding to the current target point.
[0210] Furthermore, to ensure that each rotated texture bitmap accurately covers the pen stroke trajectory, this embodiment of the application can align the center of the texture bitmap with the target point. The specific coordinates (left, top) of the alignment position are calculated as follows: left= -w / 2; top= -h / 2; Where w is the width of the texture bitmap and h is the height of the texture bitmap. These are the coordinates of the target point.
[0211] Based on this, the top left corner of the texture bitmap can be aligned with the (left, top) position to ensure that the center of the texture bitmap is strictly aligned with the target point.
[0212] In summary, the crayon stroke drawing method provided in this application accurately simulates the natural tilting characteristic of a real crayon along the direction of the stroke, making the fitting effect of digital textures closer to the real painting experience.
[0213] This allows for the reproduction of crayon strokes.
[0214] Figure 7 This is a schematic diagram of the structure of the crayon stroke drawing device based on trajectory generation provided in an embodiment of this application.
[0215] like Figure 7 As shown in the figure, this application embodiment also provides a crayon stroke drawing device based on trajectory generation, the device comprising: The sampling module 2001 is configured to: respond to the user's drawing operation on the drawing board application interface, collect the original path points on the current stroke trajectory in real time according to a preset frequency, and form an original path point set; The curve generation module 2002 is configured to: construct a spline curve corresponding to the pen stroke trajectory using the original path point set. The spline curve includes at least one sub-segment, which is constructed by a linear interpolation equation, a second-order Bézier curve equation, or a third-order Bézier curve equation. Interpolation module 2003 is configured to: perform equidistant interpolation on multiple sampling points on a spline curve to form a target point set; wherein, the target point set includes multiple target points, and the multiple target points include multiple sampling points and multiple interpolation points formed by interpolation; The third drawing module 2004 is configured to draw the texture bitmap at the corresponding position of each target point according to the tangent direction of the target point to form a crayon stroke effect. The texture bitmap is dynamically generated based on a random seed.
[0216] In one possible implementation, the curve generation module 2002 is specifically configured to: count the number of points in the original path point set in real time; the number of points in the original path point set increases as the user moves the pen until the user issues a stop drawing operation; if the original path point set includes only a first number of original path points, determine the sub-segments between the first number of original path points using a linear interpolation equation; if the original path point set includes at least a second number of original path points, determine the sub-segments between the 1st to the Mth original path points using a second-order Bézier curve equation; where M equals the second number and the second number is greater than the first number; if the original path points in the original path point set are greater than the second number, determine the sub-segments after the Mth original path point using a third-order Bézier curve equation until the user issues a stop drawing operation; and splice the sub-segments into a spline curve.
[0217] In one possible implementation, the curve generation module 2002 is further configured to: determine a four-point group, which includes four consecutive original path points; and determine the first control point and the second control point corresponding to the four-point group based on the control point formula. The control point formulas include: C1 = Pi + (Pi + 1 - Pi - 1) / 6; C2 = Pi+1 - (Pi+2 - Pi) / 6; Wherein, C1 represents the first control point, C2 represents the second control point, Pi-1 represents the first original path point in the four-point group, Pi represents the second original path point in the four-point group, Pi+1 represents the third original path point in the four-point group, and Pi+2 represents the fourth original path point in the four-point group. The interpolation step size is calculated based on the distance between adjacent dots and empirical coefficients. The empirical coefficients are determined based on the current writing speed and / or stroke parameters. The formula for calculating the interpolation step size is as follows: step=min(0.05,K / distance(Pi,Pi+1)); Where step represents the interpolation step size, K represents the empirical coefficient, distance(Pi,Pi+1) represents the distance between adjacent points, Pi represents the second original path point in the four-point group, and Pi+1 represents the third original path point in the four-point group. Based on the coordinates of the four-point group and the equation of the third-order Bézier curve, the interpolation parameters are iteratively evaluated according to the interpolation step size to determine the coordinates of the sampling points corresponding to the four-point group; the equation of the third-order Bézier curve is as follows: B(t3)=(1-t3)³×Pi×(1-t3)²×t3×C1×(1-t3)×t3²×C2+t3³×Pi+1 (t3∈[0,1]); Where B(t3) represents the coordinates of the sampling point corresponding to the four-point group; Pi represents the second original path point in the four-point group, and Pi+1 represents the third original path point in the four-point group; C1 represents the first control point, C2 represents the second control point; t3 represents the interpolation parameter, with a value range of [0,1]. Sub-segments are generated based on sampling points.
[0218] In one possible implementation, the curve generation module 2002 is further configured to: cyclically execute the following steps S1-S2: S1: determine the (Y-1)th to (Y+2)th original path points in the original path point set using a sliding window, determine the sub-segment between the Yth to (Y+1)th original path points using the third-order Bézier curve equation, and jump to step S2; wherein, the initial value of Y is equal to M; the (Y-1)th to (Y+2)th original path points are used to form a four-point group to generate the first control point and the second control point, thereby determining the sampling points used to generate the sub-segment; the Yth and (Y+1)th original path points are used to determine the interpolation step size, and the Yth original path point is the starting point of the sub-segment, and the (Y+1)th original path point is the ending point of the sub-segment.
[0219] In one possible implementation, the curve generation module 2002 is further configured to: determine the sub-segment between the (T-2)th and Tth original path points using the second-order Bézier curve equation, or determine the sub-segment between the (T-1)th and Tth original path points using a linear interpolation equation, wherein the Tth original path point is the last one in the set of original path points.
[0220] In one possible implementation, the interpolation module 2003 is specifically configured to: traverse the sampling point set and execute the following steps S3-S6; wherein, the sampling point set includes all sampling points corresponding to each sub-segment; S3: calculate the Euclidean distance Δs between the z-th pair of adjacent sampling points, and calculate the sum of the Euclidean distance Δs and the cumulative distance S to obtain the total cumulative distance. Where z is initially set to 1, and the cumulative distance S is initially set to 0; S4: If the cumulative total distance... If the distance is less than the equidistant sampling interval d, then the cumulative distance S is updated to the cumulative total distance. And, increment the value of z by 1, and jump to step S3; S5: If the total distance is accumulated If the distance between the first interpolation point and the target interpolation point is greater than or equal to the equidistant sampling interval d, determine the first interpolation point. The Euclidean distance between the first interpolation point and the target interpolation point is equal to the equidistant sampling interval d. If the first interpolation point exists, the target interpolation point is the first interpolation point before the current first interpolation point. Otherwise, the target interpolation point is the first sampling point. Reset the cumulative distance S to 0 and update the newly inserted first interpolation point to the starting point of the z-th pair of adjacent sampling points, and then jump to step S3. S6: When traversing to the last sampling point, if the cumulative distance S corresponding to it is less than the equidistant sampling interval d, end the loop.
[0221] In one possible implementation, the interpolation module 2003 is further configured to: after the traversal ends, determine the first sampling point and each of the first interpolation points as equidistant sampling points to form an equidistant sampling point set; use the equidistant sampling point set as the target point set; or, insert multiple second interpolation points between each pair of equidistant sampling points in the equidistant sampling point set; use the equidistant sampling points in the equidistant sampling point set and each of the second interpolation points inserted into the equidistant sampling point set as target points to form a target point set. The coordinates of the second interpolation point are determined based on the following formula: ; in, Indicates the coordinates of the second interpolation point. Let be the coordinates of the starting point in a pair of equally spaced sampling points. t4 is the coordinate of the endpoint of a pair of equidistant sampling points, and t4 is the interpolation coefficient. t4 is cyclically evaluated within its range with an incrementing step size, which is determined based on the Euclidean distance between the pair of equidistant sampling points, the pen stroke parameters, and / or the device performance parameters.
[0222] In one possible implementation, the interpolation module 2003 is also configured to calculate the tangent direction angle corresponding to each pair of adjacent equidistant sampling points.
[0223] In one possible implementation, the third drawing module 2004 is further configured to: for each target point, determine a rectangular drawing area corresponding to the target point based on the spatial coordinates of the target point in the canvas coordinate system, wherein the canvas coordinate system refers to the coordinate system corresponding to the drawing operation; draw a texture bitmap in the rectangular drawing area; wherein the center point of the texture bitmap is aligned with the target point, the texture bitmap is rotated by a first angle with respect to its center point, and the part of the texture bitmap that exceeds the rectangular drawing area is not drawn, the first angle being the tangent direction angle of the adjacent equidistant sampling points corresponding to the target point.
[0224] In one possible implementation, the original path point set, spline curve, and target point set grow as the user's pen strokes.
[0225] As can be seen from the above, the embodiments of this application provide a crayon stroke drawing method and apparatus based on trajectory generation. The method achieves highly realistic crayon strokes by dynamically generating textures, smoothing paths, isometric sampling, interpolation optimization, and adaptive directional fitting.
[0226] In summary, this application provides a crayon stroke drawing method based on trajectory generation, aiming to solve the problems of repetitive fixed textures and strong mechanical feel in traditional digital drawing. Specifically, this application can achieve trajectory smoothing by real-time acquisition of the original path points of the user's pen stroke trajectory and constructing spline curves using linear interpolation, second-order or third-order Bézier curves; it can also sample and interpolate the trajectory points on the curve at equal intervals to form a target point set, ensuring uniform distribution; it can dynamically generate a texture bitmap with graininess, color perturbation, and transparency changes based on random seeds, and then fit it in a center-aligned manner after rotating it according to the tangent direction angle of the target point; at the same time, it stores the handwriting data containing parameters such as target point coordinates, direction angle, and random seeds, supports high-fidelity history redrawing, and meets the needs of undo and collaborative synchronization, thus achieving high simulation of digital crayon strokes while balancing naturalness and drawing performance.
[0227] In a specific implementation, the present invention also provides a computer storage medium, wherein the computer storage medium may store a program, and when the program is executed, it may include some or all of the steps of the various embodiments of the crayon stroke drawing method based on trajectory generation provided by the present invention. The storage medium may be a magnetic disk, optical disk, read-only memory (ROM) or random access memory (RAM), etc.
[0228] It is readily understood that, based on the several embodiments provided in this application, those skilled in the art can combine, split, or reorganize the embodiments of this application to obtain other embodiments, none of which exceed the protection scope of this application.
[0229] The above detailed embodiments further illustrate the purpose, technical solution, and beneficial effects of the embodiments of this application. It should be understood that the above are merely specific embodiments of the embodiments of this application and are not intended to limit the protection scope of the embodiments of this application. Any modifications, equivalent substitutions, improvements, etc., made on the basis of the technical solutions of the embodiments of this application should be included within the protection scope of the embodiments of this application.
Claims
1. A crayon stroke drawing method based on trajectory generation, characterized in that, The method includes: In response to the user's drawing operation on the drawing board application interface, the original path points on the current stroke trajectory are collected in real time according to the preset frequency to form an original path point set. The original path point set is used to construct the spline curve corresponding to the pen stroke trajectory. The spline curve includes at least one sub-segment, which is constructed by a linear interpolation equation, a second-order Bézier curve equation, or a third-order Bézier curve equation. Equidistant interpolation is performed on multiple sampling points on the spline curve to form a target point set; wherein, the target point set includes multiple target points, and the multiple target points include the multiple sampling points and multiple interpolation points formed by interpolation; The texture bitmap is drawn on the corresponding position of each target point according to the tangent direction of the target point to form a crayon stroke effect. The texture bitmap is dynamically generated based on a random seed.
2. The crayon stroke drawing method based on trajectory generation according to claim 1, characterized in that, The step of constructing the spline curve corresponding to the pen stroke trajectory using the original path point set includes: The number of points in the original path point set is counted in real time; the number of points in the original path point set increases as the user moves the pen until the user issues a stop drawing operation. When the set of original path points includes only a first number of original path points, the sub-segments between the first number of original path points are determined using the linear interpolation equation. If the set of original path points includes at least a second number of the original path points, the sub-segment between the 1st to the Mth original path points is determined using the second-order Bézier curve equation; wherein M is equal to the second number, and the second number is greater than the first number; If the number of original path points in the set of original path points is greater than the second number, the sub-segments after the Mth original path point are determined using the third-order Bézier curve equation until the user issues a stop drawing operation. The sub-segments are spliced together to form the spline curve.
3. The crayon stroke drawing method based on trajectory generation according to claim 2, characterized in that, The process of determining the sub-segments after the Mth original path point using the third-order Bézier curve equation includes: Determine a group of four points, which includes four consecutive original path points; The first and second control points corresponding to the four-point group are determined based on the control point formula. The control point formula includes: C1 = Pi + (Pi + 1 - Pi - 1) / 6; C2 = Pi+1 - (Pi+2 - Pi) / 6; Wherein, C1 represents the first control point, C2 represents the second control point, Pi-1 represents the first original path point in the four-point group, Pi represents the second original path point in the four-point group, Pi+1 represents the third original path point in the four-point group, and Pi+2 represents the fourth original path point in the four-point group. The interpolation step size is calculated based on the distance between adjacent dots and empirical coefficients; the empirical coefficients are determined based on the current writing speed and / or stroke parameters, and the interpolation step size is calculated using the following formula: step=min(0.05,K / distance(Pi,Pi+1)); Where step represents the interpolation step size, K represents the empirical coefficient, distance(Pi,Pi+1) represents the distance between adjacent points, Pi represents the second original path point in the four-point group, and Pi+1 represents the third original path point in the four-point group. Based on the coordinates of the four-point group and the equation of the third-order Bézier curve, the interpolation parameters are iteratively evaluated according to the interpolation step size to determine the coordinates of the sampling points corresponding to the four-point group; the equation of the third-order Bézier curve is as follows: ; Wherein, B(t3) represents the coordinates of the sampling point corresponding to the four-point group; Pi represents the second original path point in the four-point group, and Pi+1 represents the third original path point in the four-point group; C1 represents the first control point, C2 represents the second control point; t3 represents the interpolation parameter, with a value range of [0,1]. The sub-segment is generated based on the sampling points.
4. The crayon stroke drawing method based on trajectory generation according to claim 3, characterized in that, The process of determining the sub-segments after the Mth original path point using the third-order Bézier curve equation until the user issues a stop drawing operation also includes: Repeat the following steps S1-S2: S1: Determine the (Y-1)th to (Y+2)th original path points in the original path point set using a sliding window, determine the sub-segment between the Yth and (Y+1)th original path points using the third-order Bézier curve equation, and proceed to step S2; wherein, the initial value of Y is equal to M; the (Y-1)th to (Y+2)th original path points are used to form the four-point group to generate the first control point and the second control point, thereby determining the sampling point used to generate the sub-segment; the Yth and (Y+1)th original path points are used to determine the interpolation step size, and the Yth original path point is the starting point of the sub-segment, and the (Y+1)th original path point is the ending point of the sub-segment; S2: If the user does not issue the stop drawing operation, increment the value of Y by 1 and jump to step S1; if the user issues the stop drawing operation, determine whether Y is less than or equal to the target value. If Y is less than or equal to the target value, increment the value of Y by 1 and jump to step S1. If Y is greater than the target value, end the loop. The target value is determined by the total number of points in the original path set.
5. The crayon stroke drawing method based on trajectory generation according to claim 2, characterized in that, Before splicing the sub-segments into the spline curve, the method further includes: When the user issues the stop drawing operation, the sub-segment between the (T-2)th and Tth original path points is determined using the second-order Bézier curve equation, or the sub-segment between the (T-1)th and Tth original path points is determined using the linear interpolation equation; wherein the Tth original path point is the last one in the set of original path points.
6. The crayon stroke drawing method based on trajectory generation according to claim 1, characterized in that, The step of performing equidistant interpolation on multiple sampling points on the spline curve to form a target point set includes: Traverse the sampling point set and execute the following steps S3-S6; wherein, the sampling point set includes all the sampling points corresponding to each of the sub-line segments; S3: Calculate the Euclidean distance between the z-th pair of adjacent sampling points. And calculate the Euclidean distance. The sum of the cumulative distance S and the cumulative distance S gives the total cumulative distance. Where z has an initial value of 1 and the cumulative distance S has an initial value of 0; S4: If the cumulative total distance If the distance is less than the equidistant sampling interval d, then the cumulative distance S is updated to the cumulative total distance. And, increment the value of z by 1, and jump to step S3; S5: If the cumulative total distance If the distance between the first interpolation point and the target interpolation point is greater than or equal to the equidistant sampling interval d, a first interpolation point is determined; wherein, the Euclidean distance between the first interpolation point and the target interpolation point is equal to the equidistant sampling interval d, and if a first interpolation point exists, the target interpolation point is the first interpolation point before the current first interpolation point; otherwise, the target interpolation point is the first sampling point; the cumulative distance S is reset to 0, and the newly inserted first interpolation point is updated to the starting point of the z-th pair of adjacent sampling points, and then the process jumps to step S3; S6: When traversing to the last sampling point, if the cumulative distance S corresponding to it is less than the equidistant sampling interval d, the loop ends.
7. The crayon stroke drawing method based on trajectory generation according to claim 6, characterized in that, After traversing the sampling point set and executing steps S3-S6, the method further includes: After the traversal is completed, the first sampling point and each first interpolation point are determined as equidistant sampling points, forming an equidistant sampling point set; The set of equidistant sampling points is used as the target point set; Alternatively, multiple second interpolation points may be inserted between each pair of equidistant sampling points in the set of equidistant sampling points; The target point set is formed by taking the equidistant sampling points in the equidistant sampling point set and each of the second interpolation points inserted into the equidistant sampling point set as the target points; The coordinates of the second interpolation point are determined based on the following formula: ; in, Indicates the coordinates of the second interpolation point. Let be the coordinates of the starting point in a pair of equally spaced sampling points. t4 is the coordinate of the endpoint of a pair of equidistant sampling points, and t4 is the interpolation coefficient. t4 is cyclically evaluated within its range with an incrementing step size, which is determined based on the Euclidean distance between the pair of equidistant sampling points, the pen stroke parameters, and / or the device performance parameters.
8. The crayon stroke drawing method based on trajectory generation according to claim 7, characterized in that, After the traversal ends, the sampling points in the sampling point set and each first interpolation point inserted into the sampling point set are taken as equidistant sampling points to form an equidistant sampling point set, which further includes: Calculate the tangent direction angle corresponding to each pair of adjacent equidistant sampling points; The step of drawing the texture bitmap at the corresponding position of each of the target points includes: For each target point, a rectangular drawing area corresponding to the target point is determined based on the spatial coordinates of the target point in the canvas coordinate system, wherein the canvas coordinate system refers to the coordinate system corresponding to the execution of the drawing operation; In the rectangular drawing area, the texture bitmap is drawn; wherein, the center point of the texture bitmap is aligned with the target point, the texture bitmap is rotated by a first angle with respect to its center point, and the portion of the texture bitmap that extends beyond the rectangular drawing area is not drawn, the first angle being the tangent direction angle of the adjacent equidistant sampling points corresponding to the target point.
9. The crayon stroke drawing method based on trajectory generation according to claim 1, characterized in that, The original path point set, the spline curve, and the target point set grow as the user's pen strokes.
10. A crayon stroke drawing device based on trajectory generation, characterized in that, The device includes: The sampling module is configured to: respond to the user's drawing operation on the drawing board application interface, collect the original path points on the current stroke trajectory in real time according to a preset frequency, and form an original path point set; The curve generation module is configured to: construct a spline curve corresponding to the pen stroke trajectory using the original path point set, wherein the spline curve includes at least one sub-segment, and the sub-segment is constructed by a linear interpolation equation, a second-order Bézier curve equation, or a third-order Bézier curve equation; The interpolation module is configured to: perform equidistant interpolation on multiple sampling points on the spline curve to form a target point set; wherein, the target point set includes multiple target points, and the multiple target points include the multiple sampling points and multiple interpolation points formed by interpolation; The third drawing module is configured to draw a texture bitmap at the corresponding position of each target point according to the tangent direction of the target point to form a crayon stroke effect. The texture bitmap is dynamically generated based on a random seed.