473,414 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Adding 2 polygons together

Hey everyone. I'm writing a program which allows the user to enter information about 2 shapes, either a square, triangle, or rectangle, then 'adds' the shaps together. The resulting shape is a polygon. I understand how this is done, just not sure about the code. I need to overload the + operator to add the shapes. Also I know that I need to make a comparison between sides begin combined, and subtract the smaller side from the area. Also I understand that I need to renumber the sides. I'm just not sure how to do this. Code is posted below

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. class Polygon
  5. {
  6.   protected:
  7.     int length[20],num_sides;
  8.   public:
  9.     void set_values (int sl[], int n);
  10.     Polygon();
  11.     ~Polygon(){}
  12.     void dimensions(int length[],int a);
  13.     void perimeter(int length[],int a);
  14.     void area (int length[],int a);
  15.     Polygon operator+(Polygon &);
  16. };
  17. Polygon::Polygon()
  18. {
  19.     int i;
  20.     for (i=0;i<20;++i)
  21.     {
  22.         length[i]=0;
  23.     }
  24.     num_sides=0;
  25. }
  26. void Polygon::set_values(int sl[],int n)
  27. {
  28.     int i=0,j=1, length[20];
  29.     for(i=0;i<n;++i)
  30.     {
  31.         length[i]=sl[i];
  32.     }
  33.     perimeter(length,n);
  34.     area(length,n);
  35.     dimensions(length,n);
  36. }
  37. //Polygon Polygon::operator+(Polygon &value)
  38. //{
  39. //        return(0);
  40. //}
  41. void Polygon::dimensions(int length[], int a)
  42. {
  43.     int i;
  44.     if (a==1)
  45.     {
  46.         cout<<"Dimensions:  "<<length[0]<<"x"<<length[0]<<endl;
  47.     }
  48.     else if (a==2)
  49.     {
  50.         cout<<"Dimensions:  "<<length[0]<<"x"<<length[1]<<endl;
  51.     }
  52.     else
  53.         cout<<"Dimensions:  "<<length[0]<<"x"<<length[1]<<"x"<<length[2]<<endl;
  54. }
  55. void Polygon::perimeter(int length[],int a)
  56. {
  57.     int perim=0,i=0;
  58.     if (a==2)
  59.     {
  60.         perim=(2*length[0]+2*length[1]);    
  61.     }
  62.     else if (a==1)
  63.     {
  64.         perim=4*length[0];
  65.     }
  66.     else if (a==3)
  67.     {
  68.         perim=length[0]+length[1]+length[2];
  69.     }
  70.     cout<<"Perimeter:   "<<perim<<endl;
  71. }
  72. void Polygon::area(int length[],int a)
  73. {
  74.     int area=0;
  75.     if (a==1)
  76.     {
  77.         area=length[0]*length[0];
  78.     }
  79.     else if (a==2)
  80.     {
  81.         area=length[0]*length[1];    
  82.     }
  83.     else if (a==3)
  84.     {
  85.         area=(length[0]*length[1])/2;
  86.     }
  87.     cout<<"Area:        "<<area<<endl;
  88. }
  89. class Rectangle: public Polygon 
  90. {
  91. public:
  92.     Rectangle(){}
  93.     ~Rectangle(){}
  94.     void set_values();
  95. };
  96. void Rectangle::set_values()
  97. {
  98.     int i, rs[4]={0};
  99.     cout<<"Enter the length of your side: "<<endl;
  100.     cin>>rs[0];
  101.     rs[2]=rs[0];
  102.     cout<<"Enter the width of your side: "<<endl;
  103.     cin>>rs[1];
  104.     rs[3]=rs[1];
  105.     Polygon::set_values(rs,2);
  106. }
  107. class Triangle: public Polygon
  108. {
  109. public:
  110.     Triangle(){}
  111.     ~Triangle(){}
  112.     void set_values ();
  113. };
  114. void Triangle::set_values()
  115. {
  116.     int ls[3]={0};
  117.     cout<<"Enter the length of side 1 of your triangle: "<<endl;
  118.     cin>>ls[0];
  119.     cout<<"Enter the length of side 2 of your triangle: "<<endl;
  120.     cin>>ls[1];
  121.     cout<<"Enter the length of side 3 of your triangle: "<<endl;
  122.     cin>>ls[2];
  123.     Polygon::set_values(ls, 3);
  124. }
  125. class Square: public Polygon
  126. {
  127. public:
  128.     Square (){}
  129.     ~Square(){}
  130.     void set_values ();
  131. };
  132. void Square::set_values()
  133. {
  134.     int i=0, ss[1]={0};
  135.     cout<<"Enter the length of the side of your square: "<<endl;
  136.     cin>>ss[0];
  137.     Polygon::set_values(ss,1);
  138. }
  139. void main () 
  140. {
  141.   int ans=0,sel=0,sel1=0,n=0;
  142.   Polygon x,y,z;
  143.   Rectangle rect;
  144.   Triangle trgl;
  145.   Square sqr;
  146.   cout<<"What would you like to do?"<<endl;
  147.   do
  148.   {
  149.   cout<<"Enter 1 to enter and display dimensions of a shape."<<endl;
  150.   cout<<"Enter 2 to add two shapes."<<endl;
  151.   cin>>sel;
  152.   if (sel==1)
  153.   {
  154.     do
  155.       {
  156.       cout<<"Enter 1 for rectangle, 2 for square, 3 for triangle"<<endl;
  157.       cin>>ans;
  158.       if(ans==1)
  159.       {
  160.           cout<<"You have chosen a rectangle!"<<endl;
  161.           rect.set_values();
  162.       }
  163.       else if (ans==2)
  164.       {
  165.           cout<<"You have chosen a square!"<<endl;
  166.           sqr.set_values();
  167.       }
  168.       else if (ans==3)
  169.       {
  170.           cout<<"You have chosen a triangle!"<<endl;
  171.           trgl.set_values();
  172.       }
  173.       else
  174.           cout<<"Make a selection from the menu!"<<endl<<endl;
  175.       }
  176.       while (ans<1||ans>3);
  177.   }
  178.   else if (sel==2)
  179.   {
  180.       while (n<2)
  181.       {
  182.       cout<<"Select your shape: "<<endl;
  183.       cout<<"Enter 1 for rectangle, 2 for square, 3 for triangle."<<endl;
  184.       cin>>sel1;
  185.       if (sel==1)
  186.       {
  187.           cout<<"Enter your dimensions for the rectangle"<<endl;
  188.           rect.set_values();
  189.           ++n;
  190.       }
  191.       else if (sel==2)
  192.       {
  193.           cout<<"Enter your dimensions for the square"<<endl;
  194.           sqr.set_values();
  195.           ++n;
  196.       }
  197.       else if (sel==3)
  198.       {
  199.           cout<<"Enter your dimensions for the triangle"<<endl;
  200.           trgl.set_values();
  201.           ++n;
  202.       }
  203.       else
  204.           cout<<"Make a selection from the menu."<<endl;
  205.       }
  206.   }
  207.   else
  208.       cout<<"Make a selection from the menu."<<endl;
  209.   }
  210.   while(sel<1||sel>2);
  211. }
  212.  
  213.  
Thanks for looking,
J
Jul 25 '07 #1
8 2700
ravenspoint
111 100+
Also I know that I need to make a comparison between sides begin combined, and subtract the smaller side from the area.
I do not understand what this means.
Jul 25 '07 #2
I do not understand what this means.

Heh...yeah that doesn't make sense. Say I am adding a triangle and a rectangle. When those shapes are added together, I will have a shape which is now neither a triangle or rectangle...it will be a polygon. The length of the side that is on the triangle, where the rectangle is added, will change. That is where the comparison comes in. The length of the side (on the triangle) will have to have the length of the rectangle side subtracted from it.

Say you have a triangle with each side being 3 inches, and you have a rectangle with a side that is 2 inches. When you combine the two sides, you have to subtract the 2 inches off, because you now have some odd shape, but 1 inch is left on the original triangle side.

Ugh...does that make sense, it's hard to explain without a diagram.

J
Jul 25 '07 #3
ravenspoint
111 100+
How do you decide which side to make common between the two polygons?
Jul 25 '07 #4
ravenspoint
111 100+
Of, course, if you use only squares and triangles with equal sides, it doesn't matter.
Attached Images
File Type: gif padd.GIF (2.6 KB, 219 views)
Jul 25 '07 #5
How do you decide which side to make common between the two polygons?

I can pick any side....the instructor said it doesn't matter

J
Jul 25 '07 #6
ravenspoint
111 100+
It doesn't matter?!?

This seems to be a poorly defined problem. No wonder you are having difficulties with it.

In order to design an algorithm to do something, the designer needs to know what is required.

I suggest you come up with what are sometimes called "use cases". Invent some concrete examples of "adding" polygons together and write down the inputs and outputs. This exercise should show what your options are, and you can pin down exactly what you want to do.

Some questions you might want to think about.

Do you want to keep the area of the summed polygons equal to the summed area of the originals?

How about the perimiter?

If I remember the original code, then it can only handle squares, rectangles and triangles. How many vertices does the sum of a triangle and a square have? How will you code deal with more than 4?
Jul 26 '07 #7
Ok so I've worked on the problem some more, and unfortunately I can't edit the other post to remove that code. I have added 6 overloaded operator functions however I am receiving an error. The error has to do with the return value in those newly added functions. I receive a syntax error ';' on the return statement line, however if I remove the semicolon I receive another error.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. class Polygon
  5. {
  6.   protected:
  7.     int length[20],num_sides;
  8.   public:
  9.     void set_values (int sl[], int n);
  10.     Polygon();
  11.     ~Polygon(){}
  12.     void dimensions(int length[],int a);
  13.     void perimeter(int length[],int a);
  14.     void area (int length[],int a);
  15.  
  16. };
  17. Polygon::Polygon()
  18. {
  19.     int i;
  20.     for (i=0;i<20;++i)
  21.     {
  22.         length[i]=0;
  23.     }
  24.     num_sides=0;
  25. }
  26. void Polygon::set_values(int sl[],int n)
  27. {
  28.     int i=0,j=1, length[20];
  29.     for(i=0;i<n;++i)
  30.     {
  31.         length[i]=sl[i];
  32.     }
  33.     perimeter(length,n);
  34.     area(length,n);
  35.     dimensions(length,n);
  36. }
  37.  
  38. void Polygon::dimensions(int length[], int a)
  39. {
  40.     int i;
  41.     if (a==1)
  42.     {
  43.         cout<<"Dimensions:  "<<length[0]<<"x"<<length[0]<<endl;
  44.     }
  45.     else if (a==2)
  46.     {
  47.         cout<<"Dimensions:  "<<length[0]<<"x"<<length[1]<<endl;
  48.     }
  49.     else
  50.         cout<<"Dimensions:  "<<length[0]<<"x"<<length[1]<<"x"<<length[2]<<endl;
  51. }
  52. void Polygon::perimeter(int length[],int a)
  53. {
  54.     int perim=0,i=0;
  55.     if (a==2)
  56.     {
  57.         perim=(2*length[0]+2*length[1]);    
  58.     }
  59.     else if (a==1)
  60.     {
  61.         perim=4*length[0];
  62.     }
  63.     else if (a==3)
  64.     {
  65.         perim=length[0]+length[1]+length[2];
  66.     }
  67.     cout<<"Perimeter:   "<<perim<<endl;
  68. }
  69. void Polygon::area(int length[],int a)
  70. {
  71.     int area=0;
  72.     if (a==1)
  73.     {
  74.         area=length[0]*length[0];
  75.     }
  76.     else if (a==2)
  77.     {
  78.         area=length[0]*length[1];    
  79.     }
  80.     else if (a==3)
  81.     {
  82.         area=(length[0]*length[1])/2;
  83.     }
  84.     cout<<"Area:        "<<area<<endl;
  85. }
  86. class Rectangle: public Polygon 
  87. {
  88. public:
  89.     Rectangle(){}
  90.     ~Rectangle(){}
  91.     Polygon Rectangle::operator+(Rectangle A);
  92.     void set_values();
  93. };
  94. void Rectangle::set_values()
  95. {
  96.     int i, rs[4]={0};
  97.     cout<<"Enter the length of your side: "<<endl;
  98.     cin>>rs[0];
  99.     rs[2]=rs[0];
  100.     cout<<"Enter the width of your side: "<<endl;
  101.     cin>>rs[1];
  102.     rs[3]=rs[1];
  103.     Polygon::set_values(rs,2);
  104. }
  105. Polygon Rectangle::operator +(Rectangle A)
  106. {
  107.     Polygon temp;
  108.     return (Polygon);
  109. }
  110. class Triangle: public Polygon
  111. {
  112. public:
  113.     Triangle(){}
  114.     ~Triangle(){}
  115.     Polygon Triangle::operator+(Triangle A);
  116.     Polygon Triangle::operator +(Rectangle A);
  117.     void set_values ();
  118. };
  119. void Triangle::set_values()
  120. {
  121.     int ls[3]={0};
  122.     cout<<"Enter the length of side 1 of your triangle: "<<endl;
  123.     cin>>ls[0];
  124.     cout<<"Enter the length of side 2 of your triangle: "<<endl;
  125.     cin>>ls[1];
  126.     cout<<"Enter the length of side 3 of your triangle: "<<endl;
  127.     cin>>ls[2];
  128.     Polygon::set_values(ls, 3);
  129. }
  130. Polygon Triangle::operator+(Triangle A)
  131. {
  132.     Polygon temp;
  133.     return(temp);
  134. }
  135. Polygon Triangle::operator +(Rectangle A)
  136. {
  137.     Polygon temp;
  138.     return(Polygon);
  139. }
  140. class Square: public Polygon
  141. {
  142. public:
  143.     Square (){}
  144.     ~Square(){}
  145.     Polygon Square::operator+(Square A);
  146.     Polygon Square::operator +(Rectangle A);
  147.     Polygon Square::operator +(Triangle A);
  148.     void set_values ();
  149. };
  150. void Square::set_values()
  151. {
  152.     int i=0, ss[1]={0};
  153.     cout<<"Enter the length of the side of your square: "<<endl;
  154.     cin>>ss[0];
  155.     Polygon::set_values(ss,1);
  156. }
  157. Polygon Square::operator +(Square A)
  158. {
  159.     Polygon temp;
  160.     return (temp);
  161. }
  162. Polygon Square::operator +(Rectangle A)
  163. {
  164.     Polygon temp;
  165.     if(*this->side1>A.side1)
  166.     {
  167.         *this->side1=*this->side1-A.side1;
  168.         temp.side1=*this->side1;
  169.     }
  170.     return (temp);
  171. }
  172. Polygon Square::operator +(Triangle A)
  173. {
  174.     return (Polygon);
  175. }
  176. void main () 
  177. {
  178.   int ans=0,sel=0,sel1=0,sel2=0,n=0;
  179.   Polygon x,y,z;
  180.   Rectangle rect;
  181.   Triangle trgl;
  182.   Square sqr;
  183.   cout<<"What would you like to do?"<<endl;
  184.   do
  185.   {
  186.   cout<<"Enter 1 to enter and display dimensions of a shape."<<endl;
  187.   cout<<"Enter 2 to add two shapes."<<endl;
  188.   cin>>sel;
  189.   if (sel==1)
  190.   {
  191.     do
  192.       {
  193.       cout<<"Enter 1 for rectangle, 2 for square, 3 for triangle"<<endl;
  194.       cin>>ans;
  195.       if(ans==1)
  196.       {
  197.           cout<<"You have chosen a rectangle!"<<endl;
  198.           rect.set_values();
  199.       }
  200.       else if (ans==2)
  201.       {
  202.           cout<<"You have chosen a square!"<<endl;
  203.           sqr.set_values();
  204.       }
  205.       else if (ans==3)
  206.       {
  207.           cout<<"You have chosen a triangle!"<<endl;
  208.           trgl.set_values();
  209.       }
  210.       else
  211.           cout<<"Make a selection from the menu!"<<endl<<endl;
  212.       }
  213.       while (ans<1||ans>3);
  214.   }
  215.   else if (sel==2)
  216.   {
  217.       cout<<"Select your shape: "<<endl;
  218.       cout<<"Enter 1 for rectangle, 2 for square, 3 for triangle."<<endl;
  219.       cin>>sel1;
  220.       if (sel1==1)
  221.       {
  222.           cout<<"Enter your dimensions for the rectangle"<<endl;
  223.           rect.set_values();
  224.           ++n;
  225.       }
  226.       else if (sel1==2)
  227.       {
  228.           cout<<"Enter your dimensions for the square"<<endl;
  229.           sqr.set_values();
  230.           ++n;
  231.       }
  232.       else if (sel1==3)
  233.       {
  234.           cout<<"Enter your dimensions for the triangle"<<endl;
  235.           trgl.set_values();
  236.           ++n;
  237.       }
  238.       else
  239.       {
  240.           cout<<"Make a selection from the menu."<<endl;
  241.       }
  242.       cout<<"Select your second shape."<<endl;
  243.       cin>>sel2;
  244.       if (sel2==1)
  245.       {
  246.           cout<<"Enter your dimensions for the rectangle"<<endl;
  247.           rect.set_values();
  248.           ++n;
  249.       }
  250.       else if (sel2==2)
  251.       {
  252.           cout<<"Enter your dimensions for the square"<<endl;
  253.           sqr.set_values();
  254.           ++n;
  255.       }
  256.       else if (sel2==3)
  257.       {
  258.           cout<<"Enter your dimensions for the triangle"<<endl;
  259.           trgl.set_values();
  260.           ++n;
  261.       }
  262.       else
  263.       cout<<"Make a selection from the menu."<<endl;
  264.   }
  265.   while(sel<1||sel>2);
  266.   if (sel1==1&&sel2==1)
  267.   {
  268.      x=trgl+trgl;
  269.   }
  270.   else if (sel1==1&&sel2==2||sel1==2&&sel2==1)
  271.   {
  272.      x=sqr+rect;
  273.   }
  274.   else if (sel1==1&&sel2==3||sel1==3&&sel2==1)
  275.   {
  276.       x=trgl+rect;
  277.   }
  278.   else if (sel1==2&&sel2==3||sel1==3&&sel2==2)
  279.   {
  280.       x=sqr+trgl;
  281.   }
  282.   else if (sel1==2&&sel2==2)
  283.   {
  284.       x=sqr+sqr;
  285.   }
  286.   else if (sel1==1&&sel2==1)
  287.   {
  288.       x=rect+rect;
  289.   }
  290.   }
  291. }
  292.  
  293.  
Thanks for looking,

J
Aug 9 '07 #8
Just a stab, but it looks like in some cases you are returning the data type and not a variable of the data type...
Aug 9 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David | last post by:
i am using the imagepolygon function to create a shape on an image i have, however i want to put more than just one polygon onto this image, i have no idea how to do this. Do i have to call the...
7
by: David | last post by:
I have an array that contains numbers in string elements , i want to convert this to integers, i have made a for loop that uses the number of elements in the array and then adds 0, thereby...
1
by: smjmitchell | last post by:
Hi All, I am developing a application in VB 6.0. I need to plot a 3D mesh of quadrilateral and triangular polygons. I have this working OK in wireframe. The question I have is what is the...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
1
by: weberwhennner | last post by:
Hi All, Since two days Ive been trying to draw lines and polygons interactively (using mouse clicks), but all ive found is some libraries that draw shapes using predefined cooredinates. Ive...
2
by: 418928 | last post by:
Hi everybody, I would like to do things like zooming a svg image and (if possible) adding labels to identify some polygons. Is there any tool (if possible, free) that can help me with this? Of...
6
by: Number 11950 - GPEMC! Replace number with 11950 | last post by:
Are polygonal buttons or perhaps polygons with tooltips and and click events available in VS8 or is it too early yet? Thanks in advance... -- Timothy Casey GPEMC! >11950 is the...
6
by: santiago | last post by:
I guess one cannot do this: arraytot = arraytot + arraydet; So, what's the trick to adding arrays like this? Thanks.
1
by: nttamdn | last post by:
Anyone help me please. I want anytime mousemove to one polygon, it's opacity becomes 0.3, and mouseout polygon, i'ts opacity return to 0 again. I means mousemove to show the polygon and hiding it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.