Hello everybody. I want to ask for some help. I need to type program that calculates areas of rectangle, triangle, circle and ellipse ( user input: rectangle = lower left corner point, length and width; triangle = top point, height, base; circle = radius and center point; ellipse = center point, width and length). Also i need to find if another point user typed in is in rectangle or triangle or circle or ellipse. So here is my code:

#include <iostream>
using namespace std;

double PI = 3.14159265;

double squareArea(double, double);
double triangleArea(double, double);
double circleArea (double);
double ellipseArea(double, double);
bool insideSquare(double, double, double, double, double, double);
bool insideCircle (double, double, double, double, double);
bool insideEllipse (double, double, double, double, double, double);
bool insideTriangle (double, double, double, double, double, double);

int main ()
{
	int choice;
	double width, length, base, height, radius, p_x, p_y, p_find_x, p_find_y;
	
	cout << "Make a choice: \n";
	cout << "1. Area & point for rectrangle\n2. Area & point for triangle\n3. Area & point for circle\n";
	cout << "4. Area & point for ellipse." << endl;
	cin  >> choice;
	switch (choice)
	{
		case 1:
			cout << "Enter width and length (put space in between): ";
			cin  >> width >> length;
			cout << "Area of a rectangle is: " << squareArea(width, length) << endl;
			cout << "Enter a lower left point of rectangle: ";
			cin  >> p_x >> p_y;
			cout << "Enter a point you want to find out, if it's inside the rectangle: ";
			cin  >> p_find_x >> p_find_y;
			if (insideSquare(width, length, p_x, p_y, p_find_x, p_find_y) == true)
				cout << "Point is in the rectangle.";
			else
				cout << "Point is not in the rectangle";
			break;
		case 2:
			cout << "Enter base and height (put space in between): ";
			cin  >> base >> height;
			cout << "Area of a trangle is: " << triangleArea(base, height) << endl;
			cout << "Enter an upper point of triangle: ";
			cin  >> p_x >> p_y;
			cout << "Enter a point you want to find out, if it's inside the triangle: ";
			cin  >> p_find_x >> p_find_y;
			if (insideTriangle(base, height, p_x, p_y, p_find_x, p_find_y) == true)
				cout << "Point is in the triangle.";
			else
				cout << "Point is not in the triangle";
			break;
		case 3:
			cout << "Enter radius: ";
			cin  >> radius;
			cout << "Area of a trangle is: " << circleArea(radius) << endl;
			cout << "Enter a center point of circle: ";
			cin  >> p_x >> p_y;
			cout << "Enter a point you want to find out, if it's inside the circle: ";
			cin  >> p_find_x >> p_find_y;
			if (insideCircle(radius, p_x, p_y, p_find_x, p_find_y) == true)
				cout << "Point is in the circle.";
			else
				cout <<"Point is not in the circle";
			break;
		case 4:
			cout << "Enter width and length (put space in between): ";
			cin  >> width >> length;
			cout << "Area of a ellipse is: " << ellipseArea(width, length) << endl;
			cout << "Enter a center point of ellipse: ";
			cin  >> p_x >> p_y;
			cout << "Enter a point you want to find out, if it's inside the ellipse: ";
			cin  >> p_find_x >> p_find_y;
			if (insideEllipse(width, length, p_x, p_y, p_find_x, p_find_y) == true)
				cout << "Point is in the ellipse.";
			else
				cout <<"Point is not in the ellipse";
			break;
		default:
			cout << "Not the right choice.";
	}
	
	return 0;
}

//Functions of Area.
double squareArea (double width, double length)
{
	double area = width * length;
	return area;
}

double triangleArea (double base, double height)
{
	double area = (base * height)/2;
	return area;
}

double circleArea (double radius)
{
	double area = (radius* radius)* PI; 
	return area;
}

double ellipseArea (double width, double length)
{
	double area = (width / 2) * (length / 2) * PI;
	return area;
}

//Functions of Point.

bool insideSquare(double width, double length, double p_x, double p_y, double p_find_x, double p_find_y)
{
	if (p_find_x >= p_x && p_find_x <= width)
	{
		if (p_find_y >= p_y && p_find_y <= length)
		{
			return true;
		}
	}
	return false;
}

bool insideCircle (double radius, double p_x, double p_y, double p_find_x, double p_find_y)
{
	if (p_find_x >= p_x && p_find_x <= radius)
	{
		if (p_find_y >= p_y && p_find_y <= radius)
		{
			return true;
		}
	}
	return false;
}

bool insideEllipse (double width, double length, double p_x, double p_y, double p_find_x, double p_find_y)
{
	if (p_find_x >= p_x && p_find_x <= (width / 2))
	{
		if (p_find_y >= p_y && p_find_y <= (length / 2))
			return true;
	}
	if (p_find_x <= p_x && p_find_x >= (width / 2))
	{
		if (p_find_y <= p_y && p_find_y >= (length / 2))
			return true;
	}
	return false;
}

bool insideTriangle (double base, double height, double p_x, double p_y, double p_find_x, double p_find_y)
{
	double aX, bX, cY, centerY;
	aX = base - p_x;
	centerY = cY - height;
	if (p_find_x <= bX && p_find_x >= aX)
	{
		if (p_find_y <= cY && p_find_y >= centerY)
			return true;
	}	
	return false;
}

Recommended Answers

All 4 Replies

Great looking code, but what is the problem with it?

After a ver quick look into the code, at least insideTriangle() uses uninitialized variables.

insideTriangle (double base, double height, double p_x, double p_y, double p_find_x, double p_find_y)
{
  double aX, bX, cY, centerY;
  aX = base - p_x;
  centerY = [B]cY[/B] - height; 
  if (p_find_x <= [B]bX[/B] && p_find_x >= aX)

i just think i didn't do the triangle write i mean the actual equation for inside point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.