Use outputs to achieve high-quality graphic output (2)

Source: Internet
Author: User
Tags rewind

From http://www.cppprog.com/2009/0821/150.html

The last time I talked about the display principle of tracing, I gave a simple example. This article begins with every step in the workflow. In order to facilitate the comparison, you can place a chart again to display the process diagram.

 

 

In addition, the examples in the previous article are also very important, and the subsequent examples will be based on this code.

Next, we will examine every step in the pipeline display process. The best way to understand each link is to compile the experiment code. We recommend that you first create a runtime experiment environment.

Vertex Source)

<! -- Basic Attributes -->

Vertex Source)

The vertex source is an object that can generate the "vertices with commands" required by a polygon. For example, the triangle vertex source generates a vertex with the "moveTo" command, and the other two vertices with the "lineto" command and the closed "closepoly" command.

Header file
#include <agg_path_storage.h> //path_storage#include <agg_ellipse.h>  // ellipse#include <agg_arc.h> // arc#include <agg_arrowhead.h> // arrowhead#include <agg_curves.h> // curve3, curve4#include <agg_gsv_text.h> // gsv_text, gsv_text_outline#include <agg_rounded_rect.h> // rounded_rect...
Type
Custom class All classes that implement void rewind (unsigned path_id); and unsigned vertex (double * X, double * Y.
Ellipse Circle. The input is the Center Coordinate and XY axis radius. This vertex source is used in this example.
Arc Arc, the input is the Center Coordinate and XY axis radius, and the start and end angles (RAD), clockwise/counterclockwise
Curve3 The Bessert curve. The input is the starting point coordinate, the first control point coordinate, and the end point coordinate.
Curve4 The input is the starting point coordinate, the first control point coordinate, the second control point coordinate, and the end point coordinate.
Gsv_text Use the literal output (only ASCII code is supported) of the internal model, use the start_point method to specify the text position, the text method to specify the text, flip to specify whether to reverse up or down, and size to specify the text size, it is suitable for combination with conv_stroke or gsv_text_outline.
Gsv_text_outline <> Convertible text. The input is gsv_text and the transformation matrix (trans_affine by default, which will be mentioned later ). Width method to set the text width
Rounded_rect Rounded corner square. Enter the coordinates and radius of the rounded corner in the lower right corner of the upper left corner.
Path_storage Path storage. You can use the join_path method to add multiple vertex sources. In addition, path_storage supports move_to, line_to, curve, arc_to, and other online draw functions.
Arrowhead Arrow used as a marking point

The arrowhead is quite special. It is generally used as a marking point of a line segment. The specific usage is as follows:

Arrowhead Ah; Ah. head (D1, D2, D3, D4); // defines the arrow ah. tail (D1, D2, D3, D4); // defines the arrow vertexsource vs; // other vertex sources // use the vertex converter, specify the markers type as vcgen_markers_term // The vertex converter can be conv_dash, conv_stroke, or conv_marker_adaptor. For details, see "pipeline of coordinate transformation". // vcgen_markers_term: Mark the endpoint as conv_stroke <Vertex, vcgen_markers_term> csvs ();... draw_term // use conv_marker to specify ah as the mark conv_marker of the marker point of the Line Segment <vcgen_markers_term, Arrowhead> arrow (csvs. markers (), AH); Ras. add_path (csvs); Ras. add_path (arrow); // marker should be followed... render

For the meanings of the D1, D2, D3, and D4 parameters in the ah. Head () and ah. Tail () methods, see


For example, draw a simple arrow line (based on the code here)

Add the following code at the end of the on_draw () method:

  1. Direction: arrowhead Ah;
  2. Ah. Head );
  3. Ah. Tail (10, 10, 5 );
  4. // Generate a straight line using path_storage
  5. Usage: path_storage pS;
  6. PS. move_to (160,60 );
  7. PS. line_to (100,100 );
  8. // Conversion
  9. Usage: conv_stroke <usage: path_storage, usage: vcgen_markers_term> CSPs (PS );
  10. Usage: conv_marker <usage: vcgen_markers_term, usage: arrowhead>
  11. Arrow (CSPs. markers (), AH );
  12. // Draw a line
  13. RAS. add_path (CSPs );
  14. Expiration: render_scanlines_aa_solid (Ras, SL, renb, placement: rgba8 (, 0 ));
  15. // Draw arrows
  16. RAS. add_path (arrow );
  17. Principal: render_scanlines_aa_solid (Ras, SL, renb, principal: rgba8 (255, 0, 0 ));
The resulting figure is:

Note:

#include "agg_conv_marker.h"#include "agg_arrowhead.h"#include "agg_path_storage.h"#include "agg_vcgen_markers_term.h"
Test code to customize a vertex source (based on the code here)

To have a deeper understanding of the vertex source, we need to implement a vertex source by ourselves. This vertex source is just a simple triangle.

As mentioned above, as long as void rewind (unsigned path_id); and unsigned vertex (double * X, double * Y) are implemented, the method can be used as the vertex source.

The rewind method indicates that the vertex source returns to the first vertex. The vertex method extracts the current vertex and moves the current vertex down. The return value is the command carried by the current vertex. The so-called command is an Enum path_commands_e class type, which is defined as follows:

  1. EnumPath_commands_e
  2. {
  3. Path_1__stop = 0, // ---- path_1__stop
  4. Path_cmd_move_to = 1, // ---- path_cmd_move_to
  5. Path_cmd_line_to = 2, // ---- path_cmd_line_to
  6. Path_cmd_curve3 = 3, // ---- path_cmd_curve3
  7. Path_cmd_curve4 = 4, // ---- path_1__curve4
  8. Path_1__curven = 5, // ---- path_1__curven
  9. Path_cmd_catrom = 6, // ---- path_cmd_catrom
  10. Path_cmd_ubspline = 7, // ---- path_cmd_ubspline
  11. Path_cmd_end_poly = 0x0f, // ---- path_cmd_end_poly
  12. Path_cmd_mask = 0x0f // ---- path_cmd_mask
  13. };

Path_commands_e can also be combined with path_flags_e:

  1. EnumPath_flags_e
  2. {
  3. Path_flags_none = 0, // ---- path_flags_none
  4. Path_flags_ccw = 0x10, // ---- path_flags_ccw
  5. Path_flags_cw = 0x20, // ---- path_flags_cw
  6. Path_flags_close = 0x40, // ---- path_flags_close
  7. Path_flags_mask = 0xf0 // ---- path_flags_mask
  8. };

When the command returned by vertex () contains path_1__stop, it indicates the end.

  1. // Equilateral triangle
  2. ClassTriangle {
  3. Public:
  4. Triangle (double CX, double cy, double r) // center point. r is the length from the center point to the edge.
  5. {
  6. // Prepare three points directly.
  7. M_step = 0;
  8. M_pt [0]. x = Cx; m_pt [0]. Y = cy-R;
  9. M_pt [1]. x = cx + R * 0.866; m_pt [1]. Y = Cy + R * 0.5;
  10. M_pt [2]. x = Cx-R * 0.866; m_pt [2]. Y = Cy + R * 0.5;
  11. // Trim uses the direction as the basis for distinguishing the interior and exterior of a polygon. You can try m_pt [1] and m_pt [2 ].
  12. }
  13. VoidRewind (unsigned)
  14. {
  15. M_step = 0;
  16. }
  17. Unsigned vertex (double * X, double * Y)
  18. {
  19. Switch(M_step ++)
  20. {
  21. Case0:
  22. // Step 1: move_to
  23. * X = m_pt [0]. X;
  24. * Y = m_pt [0]. Y;
  25. ReturnLabels: path_cmd_move_to;
  26. Case1:
  27. Case2:
  28. // Step 2 and step 3, line_to
  29. * X = m_pt [m_step-1]. X;
  30. * Y = m_pt [m_step-1]. Y;
  31. ReturnUsage: path_cmd_line_to;
  32. Case3:
  33. // Step 4: Close the Polygon
  34. ReturnUsage: path_cmd_end_poly | usage: path_flags_close;
  35. Default:
  36. // Step 5, end
  37. ReturnUsage: path_cmd_stop;
  38. }
  39. }
  40. Private:
  41. Usage: point_d m_pt [3];
  42. Unsigned m_step;
  43. };

In the on_draw () method

Vertex: ellipse ell (100,100, 50, 50); changed to Triangle ell (100,100, 50 );
Typedef partition: conv_contour <partition: ellipse> ell_cc_type; changed to typedef partition: conv_contour <triangle> ell_cc_type;
The resulting figure is:

In addition to the text output function (gsv_text can only output ASCII text), the image richness provided by the above vertex sources has exceeded the system API. The text output function will be described in separate sections.

  • The size is 5.4 kb.
  • The size is 2.5 kb.
  • The size is 11.3 kb.
  • The size is 9.4 kb.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.