#include <iostream>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>

using namespace std;

void menu()
{
 cout<<"\n1.Translation\n";
 cout<<"2.Rotation\n";
 cout<<"3.Scaling\n";
 cout<<"4.Shear\n";
}

class Line{
public:
int gm,gd=DETECT;
    int x1=15,x2=75,x3=105,x4=45,y1=15,y2=45,y3=105,y4=75,tx1,tx2,tx3,tx4,ty1,ty2,ty3,ty4,shx,shy;
    float sx,sy,r,t;
    int xt,yt;
    int ch;
    int rh[10]={150,60, 280,60, 280,150, 150,150, 150,60};
    int rh1[10]={20,150, 60,70, 110,150, 20,150};
    int tr[10];
    int tr1[8];

void display()
{
           setcolor(12);
           drawpoly(5,rh);
           delay(500);

           setcolor(WHITE);
           drawpoly(5,tr);
           delay(500);        
}
void displaytri()
{
           setcolor(12);
           drawpoly(4,rh1);
           delay(500);

           setcolor(WHITE);
           drawpoly(4s,tr1);
           delay(500);        
}
void Rhombus(int ch)
{
   
   
switch(ch)
  {

    case 1:
           cout<<"Enter the Translation Factor x: ";
           cin>>xt;
           cout<<"Enter the Translation Factor y: ";
           cin>>yt;
           tr[0]=rh[0]+xt; tr[1]=rh[1]+yt;
           tr[2]=rh[2]+xt; tr[3]=rh[3]+yt;
           tr[4]=rh[4]+xt; tr[5]=rh[5]+yt;
           tr[6]=rh[6]+xt; tr[7]=rh[7]+yt;
           tr[8]=rh[8]+xt; tr[9]=rh[9]+yt;
           initgraph(&gd,&gm,NULL);
           display();
           closegraph();
         
          break;

    case 2:
           cout<<"\nEnter the Angle of Translation: ";
           cin>>r;
           t=3.14*r/180;
           tr[0]=abs(rh[0]*cos(t)-rh[1]*sin(t));
           tr[1]=abs(rh[0]*sin(t)+rh[1]*cos(t));
           tr[2]=abs(rh[2]*cos(t)-rh[3]*sin(t));
           tr[3]=abs(rh[2]*sin(t)+rh[3]*cos(t));
           tr[4]=abs(rh[4]*cos(t)-rh[5]*sin(t));
           tr[5]=abs(rh[4]*sin(t)+rh[5]*cos(t));
           tr[6]=abs(rh[6]*cos(t)-rh[7]*sin(t));
           tr[7]=abs(rh[6]*sin(t)+rh[7]*cos(t));
           tr[8]=abs(rh[8]*cos(t)-rh[9]*sin(t));
           tr[9]=abs(rh[8]*sin(t)+rh[9]*cos(t));
           initgraph(&gd,&gm,NULL);
           display();
           closegraph();
          break;

    case 3:
           cout<<"Enter the Scaling Factor x: ";
           cin>>sx;
           cout<<"Enter the Scaling Factor y: ";
           cin>>sy;
           tr[0]=rh[0]*sx; tr[1]=rh[1]*sy;
           tr[2]=rh[2]*sx; tr[3]=rh[3]*sy;
           tr[4]=rh[4]*sx; tr[5]=rh[5]*sy;
           tr[6]=rh[6]*sx; tr[7]=rh[7]*sy;
           tr[8]=rh[8]*sx; tr[9]=rh[9]*sy;
           initgraph(&gd,&gm,NULL);
           display();
           closegraph();        

          break;

    case 4:
           cout<<"Enter the Shear Factor x: ";
           cin>>shx;
           cout<<"Enter the Shear Factor y: ";
           cin>>shy;
           tr[0]=abs(rh[0]+shx*rh[1]);
           tr[1]=abs(rh[2]+shx*rh[3]);
           tr[2]=abs(rh[4]+shx*rh[5]);
           tr[3]=abs(rh[6]+shx*rh[7]);
           tr[4]=abs(rh[1]+shy*rh[0]);
           tr[5]=abs(rh[3]+shy*rh[2]);
           tr[6]=abs(rh[5]+shy*rh[4]);
           tr[7]=abs(rh[7]+shy*rh[6]);
           tr[8]=abs(rh[0]+shx*rh[1]);
           tr[9]=abs(rh[2]+shx*rh[3]);
           initgraph(&gd,&gm,NULL);
           display();
           closegraph();

          break;

   }

}
void Triangle(int ch)
{
switch(ch)
  {

    case 1:
           cout<<"Enter the Translation Factor x: ";
           cin>>xt;
           cout<<"Enter the Translation Factor y: ";
           cin>>yt;
           tr1[0]=rh1[0]+xt; tr1[1]=rh1[1]+yt;
           tr1[2]=rh1[2]+xt; tr1[3]=rh1[3]+yt;
           tr1[4]=rh1[4]+xt; tr1[5]=rh1[5]+yt;
           tr1[6]=rh1[6]+xt; tr1[7]=rh1[7]+yt;
         
           initgraph(&gd,&gm,NULL);
           displaytri();
           closegraph();        
          break;
    case 2:
           cout<<"\nEnter the Angle of Translation: ";
           cin>>r;
           t=3.14*r/180;
           tr1[0]=abs(rh1[0]*cos(t)-rh1[1]*sin(t));
           tr1[1]=abs(rh1[0]*sin(t)+rh1[1]*cos(t));
           tr1[2]=abs(rh1[2]*cos(t)-rh1[3]*sin(t));
           tr1[3]=abs(rh1[2]*sin(t)+rh1[3]*cos(t));
           tr1[4]=abs(rh1[4]*cos(t)-rh1[5]*sin(t));
           tr1[5]=abs(rh1[4]*sin(t)+rh1[5]*cos(t));
           tr1[6]=abs(rh1[6]*cos(t)-rh1[7]*sin(t));
           tr1[7]=abs(rh1[6]*sin(t)+rh1[7]*cos(t));
           initgraph(&gd,&gm,NULL);
           displaytri();
           closegraph();
          break;

    case 3:
           cout<<"Enter the Scaling Factor x: ";
           cin>>sx;
           cout<<"Enter the Scaling Factor y: ";
           cin>>sy;
           tr1[0]=rh1[0]*sx; tr1[1]=rh1[1]*sy;
           tr1[2]=rh1[2]*sx; tr1[3]=rh1[3]*sy;
           tr1[4]=rh1[4]*sx; tr1[5]=rh1[5]*sy;
           tr1[6]=rh1[6]*sx; tr1[7]=rh1[7]*sy;
         
           initgraph(&gd,&gm,NULL);
           displaytri();
           closegraph();        

          break;

    case 4:
           cout<<"Enter the Shear Factor x: ";
           cin>>shx;
           cout<<"Enter the Shear Factor y: ";
           cin>>shy;
           tr1[0]=abs(rh1[0]+shx*rh1[1]);
           tr1[1]=abs(rh1[2]+shx*rh1[3]);
           tr1[2]=abs(rh1[4]+shx*rh1[5]);
           tr1[3]=abs(rh1[1]+shy*rh1[0]);
           tr1[4]=abs(rh1[3]+shy*rh1[2]);
           tr1[5]=abs(rh1[5]+shy*rh1[4]);
           tr1[6]=abs(rh1[0]+shx*rh1[1]);
           tr1[7]=abs(rh1[2]+shx*rh1[3]);
           initgraph(&gd,&gm,NULL);
           displaytri();
           closegraph();
     break;

   }
}
};
int main()
{

Line obj;
 while(1)
{
cout<<"\n1.Rhombus\n";
cout<<"2.Eguilateral Triangle\n";
cout<<"3.Exit\n";

cout<<"Enter Your Choice\n";
cin>>obj.ch;

switch(obj.ch)
{
case 1:
      menu();
      cout<<"\nEnter Your Choice\n";
      cin>>obj.ch;
     obj.Rhombus(obj.ch);    
  break;
case 2:
      menu();
      cout<<"\nEnter Your Choice\n";
      cin>>obj.ch;
      obj.Triangle(obj.ch);
break;
case 3:
cout<<"Exiting...";
exit(1);
break;
}
}
return 0;
}
 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}