Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ELEC 230 C++ & OOP Assignment 1 (2022-2023)

ProgramA (70%):

1.    Write a class called polygon which can initialise regular2  Two-Dimensional (2D) polygons, and translate them in the +/-x and +/-y directions (or both x and y). It should also be able to rotate them by any number of degrees about any point. Come up with your own names for all class members except N and r. The following members must be included in your class, exactly as instructed, and must serve a meaningful purpose in your program:

•    Private members:

o Member of type std::string, to store a ‘user’-defined polygon name (e.g. “t_ 1”)

•   Note: the user’-defined polygon name must always take the form l_i where l is a letter, i an integer; this also applies in ProgramB.

o Member (r) of type double, to store the radius

o Member (N) of type integer, to store the number of vertices

o Member of type std::vector, to store the x-coordinates of the polygon vertices

o Member of type std::vector, to store the y-coordinates of the polygon vertices

o Member function which prints any object’s name, number of vertices and coordinates (as a coordinate pair for each vertex) on screen using the standard output stream. The printed output must take the following exact form (although obviously the user’- defined polygon name, the coordinates and the number of coordinate pairs will vary). All numerical printed output should be rounded to 5 decimal places:

t_1=polygon( (1.73205,-1),(0,2),(-1.73205,-1))

•    Public members:

o Constructor function which, firstly, accepts and stores the name, r and N. Secondly, it then uses this data to initialise and store the coordinates of the polygon vertices. When initialised, each polygon must be centred on the origin (0,0), and its base must be horizontal (parallel with the x axis).

o Member function to translate any object, and efficiently print the object name and coordinates before and after translation, and the translation vector applied, in the following exact form:

Before translation:

t_1=polygon ( (1.73205,-1),(0,2),(-1.73205,-1))

After translation by [-4,0]:

t_1=polygon( (-2.26795,-1),(-4,2),(-5.73205,-1))

o Member function to rotate an object of this class about any point, and efficiently print the  object name  and coordinates before  and  after rotation;  also, how many degrees of rotation were applied and what point the rotation was around. (Positive rotation is anticlockwise.) In the following exact form:

Before rotation:

t_1=polygon ( (1.73205,-1),(0,2),(-1.73205,-1))

After rotation 90 degrees anticlockwise about (1,2):

t_1=polygon ( (4,2.73205),(1,1),(4,-0.73205))

o The word  efficiently’ underlined twice above implies that there are opportunities here to avoid repeating code (DRY). Can you work out how?

2.    Write a program incorporating the above class in the global scope. In your main’ function, instantiate a triangle,  a  square and  an n-gon  (you  choose n > 4) using your constructor function created above. For each polygon, following instantiation, call sequentially first the translation, then the rotation function. Rotatation must be by 90o  about (a,b) and translation by (a,-b), where a and b are the final two non-zero digits in your student ID. For each stage, (instantiation, rotation and translation) copy and paste the relevant terminal output into into https://www.desmos.com/calculator  to  generate  an  image  of a polygon,  and  include

screenshots of both the terminal output and the Desmos polygon in your report, as evidence of your working program (see also Marking Criteria).

ProgramB (30%):

Write a second program3 by following the steps below:

1) Copy and alter your class polygon” to create a new class3, “polygon3d”, that can initialise, store and print any shape regular or irregular in Three-Dimensional (3D) space. Get rid of the member variable for radius, which is no longer meaningful, and alter the constructor function so that it now directly accepts and store the coordinates of the vertices. Delete N too. Don’t make other changes yet.

2) Rotation  and translation need not be  defined  in polygon3d.  Therefore,  turn the  functions  for translation and rotation into pure virtual members, making polygon3d into an abstract base class. Convert the private members of polygon3d into protected’ members to allow a layer of inheritance.

3)   Create three new classes publicly inherited from polygon3d, called  polygon_xy, polygon_xz and polygon_yz. Each class must only be able to perform rotations and translations in its own active set of planes (defined by xy, xz or yz). You should achieve this by overriding polygon3d’s translation / rotation functions in each inherited class.

•   E.g. objects of the polygon_xy class can be translated by (1,2,0) but not by (1,2,5), since this would involve an alteration of the z-coordinates i.e. ‘movement’ in a plane other than an xy plane.

•   E.g. objects of the polygon_xy class can only be rotated in an xy plane again, z-coordinates can’t be altered by rotations, since this would entail movement’ in a plane other than an xy plane.

•   Similar rules apply for polygon_xz and polygon_yz in accordance with the planes given in their names.

See the Assignment 1 Support folder (which will be updated periodically) for help with constructor inheritance.

Important: the format of your efficient printed output to terminal should now be changed slightly so that the line containing name and coordinates looks like the following exact form (for example):

t_1=Polygon ({(0,0,0), (2,1,3), (1,3,4)})

In a similar way to ProgramA, create a program, instantiate one object of each class, and apply a        translation and a rotation sequentially to each object. This time, choose your own (non-zero) rotation and translation coordinates. Test and produce evidence for your report inhttps://www.geogebra.org/calculatorusing  the ‘3D Calculator’ option (in a similar way as before with Desmos), and with terminal output screenshots.

Now choose either Task 4 or Task 5:

4)  For one of the above inherited classes, add a public member function which overloads the + operator to add one triangle’s coordinates to another’s in a single operation. You should be  able  to code  “object3  =  object1  +  object2”  with  the  result  that the coordinates of  object3’s  vertices in  each  dimension  become  a  sum  of  those  of  object1 and object2. E.g.:

0

o  object1 vertex coordinates:                                      (1,2,5),                  (2,8,5),                  (4,1,5)

o  object2 vertex coordinates:                                      (2,4,6),                  (3,5,8),                  (2,1,8)

o  → object3 vertex coordinates become:                    (3,6,11),                (5,13,13),              (6,2,13)

Your function should also be able to efficiently achieve printing of information about the operation,   and object details before/after it, just as with the rotation/translation functions above & in ProgramA.

In your main’, instantiate some triangular objects & call this function to demonstrate its operation. Provide appropriate evidence of this with screenshots of GeoGebra and terminal output.

[See next page for Task 5 as an alternative to Task 4]

5) Alternatively, in your main’ function, instantiate 3 objects, each of a different inherited class from (3). Then, by using an array of base class pointers, pointing to objects of the derived classes in question (3), write code in your main’ function to batch-translate the objects of the different derived classes, with a single for-loop. Translations should be non-zero in the appropriate plane for each object. (Again, choose your own translation vectors). You will utilise pre-existing functions so, just as above and in ProgramA, details of the individual operations and starting/finishing object details should be  efficiently printed  as part  of the process. Provide  appropriate  evidence  of the batch- translation with screenshots of GeoGebra and terminal output, as before.

Notes for BOTH Parts A and B:

•     All objects should be stored in the code (e.g. creating a new object of class polygon should not have to involve deleting a previous object of this class.)

     You should find that the transformations performed by your member functions actually alter the stored objects attributes. So, for example, if a translation is followed by a rotation  on  the  same  object,  the   coordinates  passed  into  the  rotation  are  the  new coordinates following the translation. (This is a requirement of the Assignment.)

     Dont try to interface with the user’ – there is no need to issue messages to the user inviting them to  enter input, etc. When instantiating or manipulating objects, just hard- code these actions into your main function, on behalf of the user’ . Similarly, dont try to create a menu-driven program!

Important Further Hints and Notes:

1.   In this particular assignment, it isn’t the destination it’s the journey. This means that in this assignment, the learning objectives in respect of C++ and OOP are met by following the design instructions  closely,  paying  particular  attention  to  object-oriented-programming  and  code efficiency where possible/relevant, and not worrying too much about (perceived) usefulness of the resulting programs. Don’t try and shortcut the assignment instructions!

2.   Refer to the Assignment 1 Support folder – and keep referring back, as it will be periodically updated (it may not contain much when you first look there.)

3.   When it comes to writing your report, use the Submission Template which will be added to the above folder in due course.

4.   I would like to emphasise something already mentioned: DO NOT try and invite a user’ to instantiate or manipulate objects at runtime just instantiate/manipulate directly’ (on behalf of the ‘user’, if you will) within the code of your main’ function. So there will not be any call and response at the terminal when your programs are run, they will simply generate a bunch of output according to what instructions you have given in the main function.

5.   To initialise each polygon in Part A, you will need to first set its coordinates using N and r, then rotate it about the origin by some amount so that the base lies horizontal’ .

6.   Rotational matrix equation for 2D rotation of a single coordinate point, where x, y are the starting coordinates, x′, y′ are the coordinates after rotation, and A is the angle of rotation about the origin:

[y(x)′(′)] = [] [y(x)]

7.   For the rotation function, you may find that you want to create a copy of your object. Note, firstly, that the ‘=’ operator will automatically invoke the default copy constructor (without you needing to write a copy constructor). But what to make it equal to? That’s where the this’ keyword could come in handy. ‘this’ is a keyword which refers to the current object. Since this’ is actually a pointer, dereference it  (*this) and make your dummy/copy object equal to it.

8.   If in doubt, assume within reason that the same rules about functionality and output apply for ProgramB as ProgramA unless instructed differently. If still unsure, ask!

9.   Read  the  Marking  Criteria  on  Page  2  of this Assignment  brief,  and take  a  look  at  the Assignment  1 Discussion Board on Canvas even if you don’t have any questions, the answers to questions there might reveal misconceptions you didn’t know you had.

Note: In the Robot Operating System (ROS), 3D rotations are achieved with quaternions. Although this Assignment does not ask you to perform full 3D rotations or use quaternions, you can read more about themhere.

What to submit

1. Commented standalone code for each program, as two separate .cpp files in a zipped folder.

2. A Report (not zipped) as a pdf, Word or similar word-processed, machine-readable document:

•   Your  Report  should  include  the  following,  in  a  separate  section  for  each  program (ProgramA and ProgramB):

Screenshots for each task, showing (a) the program output at the terminal and (b) the corresponding polygons generated in Desmos or GeoGebra. (These should, of course, be presented as figures with figure numbers and captions.)

o A discussion of each program’s design and results, showing your understanding of your  own  code  and  of C++  features  used. You  can  enhance this with  snapshots  or snippets of your code, however do not paste your entire code in the report.

•   Your Report should also an Appendix with full-screen screenshots as instructed in the Marking Critera, p.2, in the same order of appearance as in the main body ofthe report.

•   Follow also all the other instructions in the Marking Criteria, p.2.