Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to write a program which will answer a pair of questions for squares, tr

ID: 3551805 • Letter: Y

Question

You are to write a program which will answer a pair of questions for squares, triangles, circles and ellipses. A square is defined by its lower left corner (SLLx,SLLy), and the length of one side SW. An isosceles triangle is defined by the topmost point (Tx,Ty), its height TH, and its base TB. A circle is defined by its center (Cx,Cy) and its radius CR. An ellipse is defined by its center (EX,EY), its vertical height EH and its horizontal width EW. The square, circle, ellipse, and triangle are aligned as follows with respect to the x and y coordinate axes.


The first question involves area. Write functions squareArea, triangleArea, circleArea and ellipseArea which will compute areas from the information describing the object.

The second question involves the notion of inside. Write four functions insideSquare, insideTriangle, insideCircle and insideEllipse which will take the same information describing the object as well as a coordinate pair (x,y) and determine whether or not (x,y) lies inside the object. Display these results as well. You may assume that the triangle is isoscles (but not necessarily equilateral). You may also assume that a point which lies exactly on the edge of an object should be considered "inside" the object.

Write function main to prompt the user for the information for one of each object and pass it to the area and "inside" functions; the returned results should then be displayed in a pleasing format for the user. Note that none of the functions outlined above is to do any input/output - you may do the necessary I/O in main(), or you could create additional "helper" functions to perform these steps. The functions outlined above should be written to be as generally useful as possible (this will be part of the grading considerations). Although not required by this assignment, any additional functions you create would help to demonstrate your grasp of the concepts of top-down design and modular coding.

Assignment: More Function Examples You are to write a program which will answer a pair of questions for squares, triangles, circles and ellipses. A square is defined by its lower left corner (SLLx, SLLy), and the length of one side Sw. An isosceles triangle is defined by the topmost point (Tx,Ty), its height TH, and its base TB. A circle is defined by its center (cx,cy) and its radius cR. An ellipse is defined by its center (ExEy), its vertical height Eh and its horizontal width Ew. The square, circle, ellipse, and triangle are aligned as follows with respect to the x and y· coordinate axes. The first question involves area. Write functions squareArea, triangleArea, circleArea and ellipseArea which will compute areas from the information describing the object. The second question involves the notion of inside. Write four functions insideSquare , insideTriangle , insideCircle and insideEllipse which will take the same information describing the object as well as a coordinate pair (x,y) and determine whether or not (x,y) lies inside the object. Display these results as well. You may assume that the triangle is isoscles (but not necessarily equilateral). You may also assume that a point which lies exactly on the edge of an object should be considered "inside" the object. Write function main to prompt the user for the information for one of each object and pass it to the area and "inside" functions; the returned results should then be displayed in a pleasing format for the user. Note that none of the functions outlined above is to do any input/output - you may do the necessary I/O in main () , or you could create additional "helper" functions to perform these steps. The functions outlined above should be written to be as generally useful as possible (this will be part of the grading considerations). Although not required by this assignment, any additional functions you create would help to demonstrate your grasp of the concepts of top-down design and modular coding. The file should be named sp06hw.cpp.

Explanation / Answer

Dropbox-link:

https://dl.dropboxusercontent.com/u/78383401/chegg/geofig.cpp



Code:


#include <iostream>
#include <cmath>

using namespace std;


//Area functions
float squareArea(float side){
    return side*side;        // A = side*side
}

float triangleArea(float base, float height){
    return 0.5*base*height;            // A = 1/2*base*height
}

float circleArea(float radius){
    return M_PI*radius*radius;        // A = pi*r*r
}

float ellipseArea(float width, float height){
    return M_PI*width*height;        // A = pi*a*b
}


//inside figure functions

// sum of distances of given point from two horizontal sides = side AND sum of distances of given point from two vertical sides = side
bool insideSquare(float side, float s_x, float s_y, float px, float py){
    float h1=abs(py-s_y);
    float h2=abs(s_y+side-py);
    float w1=abs(px-s_x);
    float w2=abs(s_x+side-px);
    if( (h1+h2)==side && (w1+w2)==side )
        return true;
    else
        return false;
}

// helper function to compute the area of triangle given all its coordinates
float area(float x1, float y1, float x2, float y2, float x3, float y3)
{
   return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}

// sum of the areas of the three triangles formed = area of the given triangle
bool insideTriangle(float base, float height, float t_x, float t_y, float px, float py)
{
   float x1 = t_x, y1 = t_y, x2 = (x1+base/2), y2 = y1-height, x3=(x1-base/2), y3=y2;
   /* Calculate area of triangle ABC */
   float A = area (x1, y1, x2, y2, x3, y3);

   /* Calculate area of triangle PBC */
   float A1 = area (px, py, x2, y2, x3, y3);

   /* Calculate area of triangle PAC */
   float A2 = area (x1, y1, px, py, x3, y3);

   /* Calculate area of triangle PAB */  
   float A3 = area (x1, y1, x2, y2, px, py);
  
   /* Check if sum of A1, A2 and A3 is same as A */
   return (A == A1 + A2 + A3);
}

// distance of the point from the centre = radius
bool insideCircle(float c_x, float c_y, float radius, float px, float py){
    return((pow(c_x-px,2) + pow(c_y-py,2)) <= pow(radius,2) );
}

// If the given point (x,y) satisfies the euqation ((x-ex)/a)^2 + ((y-ey)/b)^2 <= 1), then the point is inside the ellipse
bool insideEllipse(float e_x, float e_y, float height, float width, float px, float py){
    return ((pow(px-e_x,2)/pow(width,2)) + (pow(py-e_y,2)/pow(height,2)) <= 1);
}

int main(){
   
   
    bool isInside;
    float sllx, slly, sq_length, tx, ty, height, base, cx, cy, radius, ex, ey, vert_height, hori_width, px,py;
   
    // IO for square
    cout<<"Enter lower left coordinates(x y) and side length of square: " << endl;
    cin >> sllx >> slly >> sq_length;
    cout << "Area of the square: " << squareArea(sq_length) << endl <<endl;
   
   
    // IO for triangle
    cout << "Enter the topmost point(x y), height and base of isosceles triangle: " << endl;
    cin >> tx >> ty >> height >> base;
    cout << "Area of the triangle: " << triangleArea(base, height) << endl <<endl;
   
    // IO for circle
    cout << "Enter the centre(x y) and radius of the circle: "<<endl;
    cin >> cx >> cy >> radius;
    cout << "Area of the centre: " << circleArea(radius) << endl <<endl;
   
   
    // IO for ellipse
    cout << "Enter the centre(x y), vertical_height and horizontal_width of ellipse: " <<endl;
    cin >> ex >> ey >> vert_height >> hori_width;
    cout << "Area of the ellipse: " << ellipseArea(hori_width, vert_height) << endl <<endl;
   
   
    // Checking if point lies inside the four different figures
    cout << "Enter the point to be checked: " << endl;
    cin >> px >> py;
   
    isInside = insideSquare(sq_length, sllx, slly, px, py);
    if(isInside)
        cout << "Point (" << px <<","<<py<<") lies inside the square" << endl;
    else
        cout << "Point (" << px <<","<<py<<") lies outside the square" << endl;
   
    cout << endl;
   
    isInside = insideTriangle(base, height, tx, ty, px, py);
    if(isInside)
        cout << "Point (" << px <<","<<py<<") lies inside the triangle" << endl;
    else
        cout << "Point (" << px <<","<<py<<") lies outside the triangle" << endl;
   
    cout << endl;
   
    isInside = insideCircle(cx, cy, radius, px, py);
    if(isInside)
        cout << "Point (" << px <<","<<py<<") lies inside the circle" << endl;
    else
        cout << "Point (" << px <<","<<py<<") lies outside the circle" << endl;
   
    cout << endl;
   
    isInside = insideEllipse(ex, ey, vert_height, hori_width, px, py);
    if(isInside)
        cout << "Point (" << px <<","<<py<<") lies inside the ellipse" << endl;
    else
        cout << "Point (" << px <<","<<py<<") lies outside the ellipse" << endl;
   
    return 0;
}


Compilation and Execution:

g++ geofig.cpp

./a.out



Output:



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote