System design and real world! Realization of point, circle, ball

zhaozj2021-02-11  164

Recently, the C forum is discussing, point, round, and ball OOP implementation. The specific discussion is shown below URLHTTP: //www.9cbs.net/expert/topic/772/772272.xml? Temp = .4635889. I have an opinion on the views of JINFENG_WANG and other discussion. In the post of jinfeng_wang, he gave IS-A, HAS-A between the circle and the ball. And some friends give some solutions. But I think the relationship between the first, points, circles, and the ball is really IS-A, HAS-A relationship? Does these programs really reflect the relationship between the reality world, round, and the ball? If not, what is their relationship? ? What should their solutions? Let me first look at a relatively complete program Interface2d {Virtual int getX () const = 0; Virtual int get () const = 0;}; interface point3d {Virtual int getX () const = 0; Virtual Int gety () const = 0; Virtual int GETZ () const = 0;}; interface circle2d {Virtual int getX () const = 0; Virtual int get () const = 0; Virtual Int getr () const = 0;}; interface globe {Virtual INT getX () const = 0; Virtual int getz () const = 0; Virtual int GETZ () const = 0; Virtual Int getr () const = 0;

Class Globe2Circle: INT getX ();} int get} int gety ();} int get} int gety ();} int game ();} int GETR (); CONST GLOBE & G;

Point2D, Point3d class design should have no problem, it can reflect three-dimensional and two-dimensional coordinates. But let's take a look at Circle2D, what information is it given? What is the three int variables, what is their information? Perhaps the natural idea is X, Y represents, coordinates on the circle, and R represents a radius. Ok, if I give the following implementation you can say that he is a circle? Class aspect: public circle2d {public: Virtual int getX () const {return 100;} Virtual int getY () const {return -100;} Virtual Int getr () {return 1;}} If he is a circle, then this circle is strange. One point one line is a circle? (X, y) What is it? Is it a little on a circle? If this is the origin of this round? (X, y) Is the origin? If so, how do you say that on the circle? Another can I say that aspect is a semi-circular or sector? If the aspect is a half circle (100,100) as a half circle R as a radius, can it be a circular equivalent of the radius and a circular point R as a circle R? That is, do we easily add the operator = () operation to Circle2D? Maybe there are other remedies. It is best to remedy. But no matter how to remedy, if you say your class is a circle, then at least you must pass the following verification: give any point (x, y), your class can not be judged (x, y) in your class definition on. We will look at the relationship between the circles and the ball. Class Globe2Circle: INT getX ();} int get} int gety ();} int get} int gety ();} int game ();} int GETR (); CONST GLOBE & G;

What is this relationship tells us? If we don't like the definition of Circle2D, it is no problem. Then he only tells us that globe2circle is a projection that the ball is cross-sectional on it through its dot (x, y, z) and z is 0. If this is the relationship between circles and balls, it is too narrow. If it is a projection to the cross section of (0, Y, Z)? If it is a population of the projection of the crown in (x, y, 0) means? It can be said that it is a circle or it is a ball. This is the illusion of common sense to us. If we are deep in the characteristics, there is no area without the size, how can it say that it is a circle and a ball? In parsing the geometric mid point is a Cartesian, the so-called Cartesi is a tuple (x1, x2 ... xn-1), a flute of the first element of the first element is ordered. Carli is ((x1, x2 ", x). What is a circle, a collection of two-dimensional points, all the points of the point to a certain point is fixed, we call this collection For a circle, the fixed point is called the center, and the length is called a radius. We record (X-X0) ^ 2) (Y-Y0) ^ 2 = r ^ 2. What is the ball, the collection of three-dimensional points, all the distances to a certain point of order are fixed lengths. We call this point for a ball called the center, and the length is called a radius. We record (X-X0) ^ 2) (Y-Y0) ^ 2 (Z-Z0) ^ 2 = R ^ 2. In fact, the ball (circle) is essentially a series of points that meet a function. Therefore, it is necessary to describe a circle, or a ball, there must be three necessary conditions, 1. Circle (ball) equation defined domain 2. Circle (Ball) Equation 3. The value of round (ball) equation is then Discuss the ball, circle, what is the relationship? We know a ball equation (X-X0) ^ 2) (Y-Y0) ^ 2 (Z-Z0) ^ 2 = R ^ 2. Only give a variable is unable to find out that the other two variables are just an equation. It is a circular equation. The relationship between the ball and circle is that any of the values ​​m in a certain (x, y, z) can give this ball on the plane N = m (N is X, Y, Z any variable). projection. Then, it is given a circle, which is given a defined domain (X-X1) ^ 2 (Y-Y2) ^ 2 = R2 ^ 2, which can be obtained on this defined domain. point.

Popular speaking, the point is the projection of the line in the 0-dimensional coordinates, the line is the projection of the circle in a one-dimensional coordinate, the circle is the projection of the ball on two-dimensional coordinates. If there is four-dimensional, then the ball is the four-dimensional object in 3D. Projection. That is, the point is the definition domain of the line, and the line is a circular definition domain, and the circle is the definition domain of the ball. Regardless of point, line, circle, and the ball is a point collection, to indicate that this point is round, point, and ball, you should meet an equation. So, line, circles, balls are not object but algorithm data. Of course, you can pack the data into classes, then use abstract ways to access these data, and separate algorithms give these data to constrain. Here are the implementation of my point, line, circle, and ball. #include "stdafx.h" #include #include #include #include using namespace std; //// Template point // template / / Point Dimension Class Point {Double MeaningLESS; / / Unwanted Value-1 1/2 Double Double __Point [DEM]; // Dist-Coordinate Public: Point () {MEMSET (__ Point, 0, DEM); / / Initialization point works as the origin 0 meaningness = POW (-1, 0.5); // Initialization meaningless point} Point (double p [DEM]) // Initial point work is the coordinate {Memcpy in P [DEM] {MEMCPY __point, p, dem); meaningless = POW (-1, 0.5);}

Bool Operator <(const point & P) // Compare the size of two points {for (int i = 0; i & P) // Decision two points equal to {for (INT i = 0; i & Operator = (const point & P) // pay value COPY {for (int i = 0; i

} Return (* this);} Bool ismeaningless () // is a meaningless value (such as negative number on the flat surface) {for (int i = 0; i DEM) {Double K = POW (-1, 0.5); Return K;} else return __point [index];}}; template Bool Operator <(Const Point & P1, Const Point & P2) {for INT i = 0; I & P1 = (Point &) P1; Point & P2 = (Point &) P2; IF (P1 [i] Class Point <0> // That of the 0-dimensional coordinate system {Double MeaningLess; double __point [1]; public: Point () {__point [0] = 0.0; MeaningLESS = POW (-1, 0.5);} Point (double p) {__point [0] = p; meaningless = POW (-1, 0.5);} Bool Operator <(Const Point & P) {

IF (__ point [0]

& P) {

IF (__ point [0] == p .__ point [0]) Return True; Else Return False;} Point <0> & operator = (const point <0> & p) {

__point [0] = p .__ point [0];

Return (* this);} bool ismeaningness () {

Double MeaningLESS = POW (-1, 0.5); if (__ point [0] == MeaningLess) Return True; Else Return False;} Double & Operator [] (const INDEX) {if (INDEX> 0) {Double K = POW (-1, 0.5); return k;} else return__point [index];

Bool Operator <(Const Point <0> & P1, Const Point <0> & P2) {INT i = 0; Point <0> & P1 = (Point <0> &) P1; Point <0> & P2 = (Point < 0> &) P2; IF (P1 [i] Class Reference_frame // Coordinate system {Int Demsize; // Dimension Point __ORIINPOINT; // Coordinate original public: reference_frame () {if (DEM <1) // If the dimension is less than 1 error {Exception E; throw E;} DemsionSize = DEM;}};

Template Class Aspect: Public Reference_Frame // Map between points in a certain coordinate system (currently full shift) {Vector > __Dpointset; // Defining Domain Multimap , Point > __vpointSet; // Valid SHIFT * __SHIFT; // Transform Function Struct Iteradaptor {MultiMap , Point >: : Iterator & Iterator; Friend Class Iteradaptor; PUBLIC: ITERADAPTOR (MultiMap , Point > :: Iterator & It): Iterator (IT) {

} ITerAdaptor (iTeraDaptor & Ita): iterator (ita.iterator) {

} Iteradaptor & Operator () {Return ITERADAPTOR ( iTerator);} {Return Iteradaptor (- Iterator);} {RETURN ITERADAPTOR (- Iterator);} {RETURAPTOR & OPERATOR = (MultiMap , Point > :: itemaptaptor (it);} iTeradaptor & Operator = (iTeradaptor & It) {this-> item = it; return (* this);} Point & Operator * () {Return ( * itrator). Second;} Bool Operator == (iTeradaptor & It) {Return Iterator == it.Item;} Bool Operator! = (iTeradaptor & It) {Return (! (* this) == IT));}} ; public: typedef vector > :: iterator __Defiterator; typedef IterAdaptor iterator; template Aspect (_A & PointSet, ParamType & Param) // constructor and passing domain, mapping requires Parameters {__shift = new shift (para :: itrator it = pointset.begin (); for (; it! = PointSet.end (); it ) {Point P = (* it); __dpointset .push_back (p);} buildvalue ();

} Template aspect (_A & pointset) {__shift = new shift; _a :: itrator it = pointset.begin (); for (; it! = Point set.end (); it ) {Point p = (* IT); __dpointset.push_back (p);} buildvalue ();

} Aspect () {__shift = new shift;} ~ aspect () {delete __shift;} setdefinition (Vector > & PointSet) {__dpointset = PointSet; buildvalue ();} __defiterator dbegin () // Get definition The upper limit of the domain {RETURN __DPOINTSET.BEGIN ();} __defiterator dend () // Waiting to define the lower limit of the domain {Return __dpointset.end ();} {Return ITERADAPTOR (__ vpoint set.begin) ()). ); For (; it! = Dend (); it ) {Point TEMP; TEMP = (* __ shift) ((* IT)); __vpointset.insert (MultiMap , Point > :: value_type ((* it), temp));}}}; class Line // Y = 0 mapping class {public:

Line () {

Point <1> & operator () (Point <0> & point) {Point <1> Temp; Temp [0] = Point [0]; Return Temp;

}

Template class circleparam // ball or circle parameters (DEM = 2 is circle, DEM = 3 is the ball) {protected: int __radius; // radius Point __circle_centre; // round (sphere) public : CircleParam (int R, Point O) {IF (DEM> 3 || DEM <2) {Exception E; th;} __radius = r; __circle_centre = O;} circleparam () {__RADIUS = 0;} CircleParam & Operator = (CircleParam Center) {__RADIUS = center .__ radius; __circle_centre = center .__ circle_centre; return (* this);}};

Class Circle: Public CircleParam <2> // Circular {Public: Circle (CircleParam <2> & param) {(CircleParam <2>) (* this) = param;} Point <2> Operator () (Point <1> Point) {double r_sqr = __ radius * __ radius; double x_sqr = (Point [0] -__ circle_centre [0]) * (Point [0] -__ circle_centre [0]); Double Y = POW (r_sqr-x_sqr, 0.5); POINT <2> TMP; TMP [0] = POINT [0]; TMP [1] = Y; Return TMP;}}; Class Ball: Public CircleParam <3> // Ball {Public: Ball (CircleParam <3> & PARAM ) {(* This) = param;} Point <3> Operator () (Point <2> Point) {double r_sqr = __ radius * __ radius; double x_sqr = (Point [0] -__ circle_centre [0 ]) * (Point [0] -__ circle_centre [0]); Double Y_SQR = (Point [1] -__ circle_centre [1]) * (Point [1] -__ circle_centre [1]); double z = POW (r_sqr-x_sqr -y_sqr, 0.5); Point <3> TMP; TMP [0] = Point [0]; TMP [1] = Point [1]; TMP [0] = z; returnit;}}};

INT main (int Argc, char * argv []) {double z = POW (-4, 0.5); typedef point <0> p0; typedef point <1> p1; typedef point <2> p2; typedef point <3> P3; Vector > _ldef; _ldef.push_back (p0 (0)); _LDEF.PUSH_BACK (P0 (1)); _LDEF.PUSH_BACK (P0 (2)); _LDEF.PUSH_BACK (p0 (3)) _LDEF.PUSH_BACK (P0 (4)); _LDEF.PUSH_BACK (P0 (5));

Double param; aspect <1, line> line (_ldef); // Line (discrete line) Double P2 [2] = {0.0, 0.0}; CircleParam <2> CP2 (100, P2 (P2)); Double P3 [3] = {0.0, 0.0, 0.0}; CircleParam <3> CP3 (100, P3 (P2));

Aspect <2, Circle> Circle (Line, CP2); // Taking line as a circle (discrete circle) aspect <3, ball> ball (CP3) in the domain; // with Circle as a domain of the domain ( Discrete balls) RETURN 0;}

转载请注明原文地址:https://www.9cbs.com/read-5266.html

New Post(0)