-->
Your statement implies that you have already seen a rectangle class. And you are now asking for detailed info on how to do it?
Any way your statement is not really correct. Obviously a circle has a centre point & radius while a rectangle has width & height, and a circle has lots of different ways of being created, but the organisation of the code is the same.
You have constructors to set member variables when the object is created, and functions to change the value of the member variables (set functions), functions to retrieve values of member variables (get functions), and output functions to, say, print out values.
Professor wrote:
Try to avoid writing a get / set function for each member variable - only write them if you need them. Remember class functions have direct access to all the member variables, so you can take advantage of this in your Output function. As well as that you can have functions that set several variables at once. IMO, one only needs get functions if the data is required by another class. Having get / set functions for every member variable is effectively the same as making them all public.
If you explain why you didn't have get functions & only 1 set function in this case, you professor would be a bit unfair to mark you down IMO.
Also, having get in a function name is a bit misleading - it implies the retrieval of a member variable. I would just name the functions Area & Perimeter.
There is a bit of a convention to name member variables with a leading m_ as in m_Width. This can make it easier to name function parameters.
There you go, some stuff to think about.
C program all the c language program. Sunday, 11 November 2012. 2D Reflection #include #include #include #include struct s. 2D Reflection; 2D Polygon Rotation; Scaling a 2D Polygon; Translate a 2D polygon; Mid Point Algorithm (circle Drawing ). 2D Rotation Program Using C Programming. 2D Translation Rectangle Program Using C Programming. 3D Animation Computer Graphics Programs. 3D Translation Program Using C Programming. 3D Scaling Program Using C Programming. 3D Rotation Program Using C Programming. Midpoint Circle Algorithm Using C Programming.
In Drawing with Direct2D, we saw that the ID2D1RenderTarget::FillEllipse method draws an ellipse that is aligned to the x- and y-axes. But suppose that you want to draw an ellipse tilted at an angle?
By using transforms, you can alter a shape in the following ways.
- Rotation around a point.
- Scaling.
- Translation (displacement in the X or Y direction).
- Skew (also known as shear).
A transform is a mathematical operation that maps a set of points to a new set of points. For example, the following diagram shows a triangle rotated around the point P3. After the rotation is applied, the point P1 is mapped to P1', the point P2 is mapped to P2', and the point P3 maps to itself.
Transforms are implemented by using matrices. However, you do not have to understand the mathematics of matrices in order to use them. If you want to learn more about the math, see Appendix: Matrix Transforms.
To apply a transform in Direct2D, call the ID2D1RenderTarget::SetTransform method. This method takes a D2D1_MATRIX_3X2_F structure that defines the transformation. You can initialize this structure by calling methods on the D2D1::Matrix3x2F class. This class contains static methods that return a matrix for each kind of transform:
For example, the following code applies a 20-degree rotation around the point (100, 100).
The transform is applied to all later drawing operations until you call SetTransform again. To remove the current transform, call SetTransform with the identity matrix. To create the identity matrix, call the Matrix3x2F::Identity function.
Drawing Clock Hands
Let's put transforms to use by converting our Circle program into an analog clock. We can do this by adding lines for the hands.
Instead of calculating the coordinates for the lines, we can calculate the angle and then apply a rotation transform. The following code shows a function that draws one clock hand. The fAngle parameter gives the angle of the hand, in degrees.
This code draws a vertical line, starting from the center of the clock face and ending at the point endPoint. The line is rotated around the center of the ellipse by applying a rotation transform. The center point for the rotation is the center of ellipse that forms the clock face.
The following code shows how the whole clock face is drawn.
C++ |
---|
C++ |
---|
You can download the complete Visual Studio project from Direct2D Clock Sample. (Just for fun, the download version adds a radial gradiant to the clock face.)
Combining Transforms
The four basic transforms can be combined by multiplying two or more matrices. For example, the following code combines a rotation with a translation.
The Matrix3x2F class provides operator*() for matrix multiplication. The order in which you multiply the matrices is important. Setting a transform (M × N) means 'Apply M first, followed by N.' For example, here is rotation followed by translation:
Here is the code for this transform:
Now compare that transform with a transform in the reverse order, translation followed by rotation.
The rotation is performed around the center of the original rectangle. Here is the code for this transform.
As you can see, the matrices are the same, but the order of operations has changed. This happens because matrix multiplication is not commutative: M × N ≠ N × M.
Next
we went over how to do class circles and it was completely different for a rectangle. So.. |
Your statement implies that you have already seen a rectangle class. And you are now asking for detailed info on how to do it?
Any way your statement is not really correct. Obviously a circle has a centre point & radius while a rectangle has width & height, and a circle has lots of different ways of being created, but the organisation of the code is the same.
You have constructors to set member variables when the object is created, and functions to change the value of the member variables (set functions), functions to retrieve values of member variables (get functions), and output functions to, say, print out values.
Professor wrote:
accessor and mutator functions for all data fields |
Try to avoid writing a get / set function for each member variable - only write them if you need them. Remember class functions have direct access to all the member variables, so you can take advantage of this in your Output function. As well as that you can have functions that set several variables at once. IMO, one only needs get functions if the data is required by another class. Having get / set functions for every member variable is effectively the same as making them all public.
If you explain why you didn't have get functions & only 1 set function in this case, you professor would be a bit unfair to mark you down IMO.
Also, having get in a function name is a bit misleading - it implies the retrieval of a member variable. I would just name the functions Area & Perimeter.
There you go, some stuff to think about.