Cartesian plane in C++
Posted by Samath
Last Updated: January 07, 2021

The following formula gives the distance between two points, (x1, y1) and (x2, y2) in the Cartesian plane:

 

Given the center and a point on the circle, you can use this formula to find the radius of the circle. Write a program that prompts the use to enter the center and point on the circle. The program should then output the circle’s radius, diameter, circumference, and area. Your program must have at least the following functions:

distance: This function takes as its parameters four numbers that represent two points in the plane and returns the distance between them.

radius: This function takes as its parameters four numbers that represent the center and a point on the circle, calls the function distance to find the radius of the circle, and returns the circle’s radius.

circumference: This function takes as its parameter a number that represents the radius of the circle, and returns the circle’s circumference. (If r is the radius, the circumference is 2pr)

area: This function takes as its parameter a number that represents the radius of the circle, and returns the circle’s area. (If r is the radius, the circumference is pr2)

Assume that p = 3.1416

 

Output:

Code:

#include <iostream>
#include<cmath>
#include<iomanip>
 
#define PI  3.1416
#define sq  2
 
using namespace std;
 
double distance(double x1, double x2, double y1, double y2);
double raduis(double x1, double x2, double y1, double y2);
double circumference(double r);
double area(double r);
 
 
 
int main(int argc, char *argv[])
{
	 double x1;
	 double x2;
	 double y1;
	 double y2;
	 
    
    cout<<"Enter the x and y cordinates of center of the circle: ";
    cin>>x1>>y1;
    
    cout <<endl<<"Enter the x and y coordinates of a point on the circle: ";
    cin>>x2>>y2;
    
    
        cout<<endl<<"Raduis = "<<fixed<<setprecision(2)<<raduis(x1,x2,y1,y2)<<endl;
        
        cout<<"Diameter = "<<raduis(x1,x2,y1,y2) + raduis(x1,x2,y1,y2)<<endl;
        
        cout<<"Circumference = "<<circumference(raduis(x1,x2,y1,y2))<<endl;
        
        cout<<"Area = "<<area(raduis(x1,x2,y1,y2))<<endl;
        
        
   system("pause");
	return 0;
}
 
double distance(double x1, double x2, double y1, double y2)
{         
      return sqrt(pow(x2 - x1, sq) + pow(y2 - y1, sq));                    
}
 
double raduis(double x1, double x2, double y1, double y2)
{
  return distance(x1, x2, y1, y2);                                   
}
 
double circumference(double r)
{
   return sq * PI * r;
}
 
double area(double r)
{
   return PI * pow(r, sq);
}


Related Content