PHP drawing object structure design and application example

Source: Internet
Author: User
This article mainly describes how to design the structure of drawing objects in PHP.

This article mainly describes how to design the structure of drawing objects in PHP.

Small and Medium-sized Data structures in PHP can be implemented by simple classes, that is, they are defined by simple data and operations. However, with the complexity of the data structure, simple classes cannot meet the needs of fully reflecting the entire data structure. Therefore, the structure of classes needs to be used. in one sentence, the class members are also composed of classes. This is an extension of the object-oriented design method for data encapsulation, information hiding, and code reuse, and another method is class inheritance.
This article mainly discusses the structure of classes.

Taking plotting as an example, let's look at a simple example of Ry.
Ry consists of a series of points, lines, and surfaces. some geometric truths show that points are the most basic elements. Therefore, we use points as the basic object of design to describe the line and surface objects. It is used as an example to illustrate how the class structure is implemented.

Analysis
Assume that a point is located in a certain plane. after the coordinate system is determined (that is, the origin, coordinate axis, and their positive direction are given), we can describe the point using the coordinate position,

A straight line is determined by two points, so a line segment can be determined by the start and end points.

 

Surface-(for example) a rectangle consists of two parallel edges with an angle of 90 degrees between the adjacent sides. When drawing, it is found that the rectangle can be uniquely determined by two points on the diagonal corner.

Based on the above analysis, we began to design: Point class, line class, and surface class. Data members in the line and surface classes include vertex classes. this is the structure of the classes, and a simple class forms a complex class.
The design is as follows:

From another perspective, if the structure design method of the class is not used, a class without layers may be like this. Take Line as an example:

The idea of a simple class is: member variables represent the coordinates (x1, y1), (x2, y2) of the two points of the line segment ).

Compared with the two designs, Line_Bad results in inconvenient maintenance and change because there is no hierarchy. There are four more member variables to be maintained first. if you need to set the color of the midpoint of a line segment, you need to add the member function Line_Bad. if you need to change this line to three-dimensional space, line_Bad adds member variables and modifies all member functions. Using the Line class avoids such troubles. you only need to call the Point method to set the color. Instead, you only need to add the Point attribute (zcoordinate) to the 3D space. The Line class itself does not need to be changed (related to the PHP plot function), saving a lot of effort to maintain and modify it :).



Now that the basics of plotting are available, let's take a practical example.

In many statistics, legends are often used to display various statistical results. We use the plot class designed above to display the bar chart.

Analysis:
A bar chart consists of a coordinate axis and a column. The axis can be displayed by the Line class, and the bar can be displayed by the Rectangle class.

The axis is shown as follows:

Function DrawAxes ()
{
$ Vertspan = 200;

$ P1 = new Point (10, $ vertspan + 2.5 );
$ P2 = new Point (10, 2.5 );

$ VerticalAxis = new Line ($ p1, $ p2 );
$ VerticalAxis-> Draw ();

$ P1 = new Point (7.5, $ vertspan );
$ P2 = new Point (370, $ vertspan );

$ HorizontalAxis = new Line ($ p1, $ p2 );
$ HorizontalAxis-> Draw ();

}





The display bar is as follows:
Function Plot ($ arr_barTotal)
{
$ Vertspan = 200;
// Display ratio
$ ScaleHeight = 190;
// Determine the maximum value in the column
$ MaxTotal = Max1 (& $ arr_barTotal );
// Determine the number of bars
$ Total = count ($ arr_barTotal );

// Display the column in a loop
$ Dx = 15;
$ X = 10 + $ dx;

For ($ I = 0; $ I <$ total; $ I ++)
{
$ RectHeight = ($ arr_barTotal [$ I]/$ maxTotal) * $ scaleHeight;

$ P1 = new Point ($ x, $ vertspan-$ rectHeight );
$ P2 = new Point ($ x + $ dx, $ vertspan );

$ CurrRect = new Rectangle ($ p1, $ p2 );
$ CurrRect-> Draw ();

$ X + = 2.0 * $ dx;
}
}


Main program:
1) confirm the displayed data.
2) the axis is displayed.
3) display the column.


// Main
$ Arr_barTotal = array ('1', '3', '34', '23', '5', '25', '7'); // Display Data
DrawAxes (); // draw the coordinate axis
Plot ($ arr_barTotal); // draw a column



In this way, the main program framework is completed: D
The effect is as follows.

For further beautification, you can add the corresponding attributes and member functions in the basic display class.

Appendix: The source code is not attached due to space issues.
All the code in this article can be obtained in http: // 10.31.172.7/cgi-bin/cvsweb. cgi/kai/graph/, and has been debugged in the php4.0.2 + Gd1.62 environment.

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.