Question

this assignment should be in C++ In this assignment, you will implement two classes. Point: A...

this assignment should be in C++

In this assignment, you will implement two classes.

  1. Point: A Point in a two dimensional plane has an integer x coordinate value and an integer y coordinate value.

  2. Rectangle: A Rectangle is a class that has three attributes: 1) Point type data that represent the top-left point of the rectangle, 2) integer length and 3) integer width.

Write the appropriate class definition for Point class and Rectangle class with necessary constructors, mutator and accessor functions.

Write a function that will take two objects of Rectangle class as parameter and return whether they intersect or not.

Write a main function that will take inputs from user for two rectangles. Each rectangle requires 4 set of inputs: 1) x of top-left point, 2) y of top-left point, 3) length and 4) width. The main function will return the 4 set of coordinate values of the rectangles and whether they intersect or not. Print the 4 set of co- ordinate values of a rectangle in the following order:

  1. Top-left point

  2. Bottom-left point

  3. Bottom-right point

  4. Top-right point

Sample Input:

x of rectangle 1: 2
y of rectangle 1: 4 length of rectangle 1: 2

width of rectangle 1: 3

x of rectangle 2: 4
y of rectangle 2: 3

length of rectangle 2: 2 width of rectangle 2: 3

Sample Output:

Co-ordinates of rectangle 1: (2,4), (2,2), (5,2), (5,4)

Co-ordinates of rectangle 2: (4,3), (4,1), (7,1), (7,3)

They intersect.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

class PointT {

public:

double x, y;

};

class RectT {

public:

PointT location;

double l, w;

};


int chk_intersect(RectT a, RectT b) {

// left upper and lower right coordinates of rect a

PointT l1, r1;

// left upper and lower right coordinates of rect b

PointT l2, r2;

l1.x = a.location.x;

l1.y = a.location.y;

r1.x = a.location.x + a.w;

r1.y = a.location.y - a.l;

l2.x = b.location.x;

l2.y = b.location.y;

r2.x = b.location.x + b.w;

r2.y = b.location.y - b.l;

// If one rectangle is on left side of other

if (l1.x > r2.x || l2.x > r1.x)

return 0;

// If one rectangle is above other

if (l1.y < r2.y || l2.y < r1.y)

return 0;

return 1;

}

void inputRect(RectT *a) {

cout << "Enter x coordinate of top left corner: ";

cin >> a->location.x;

cout << "Enter y coordinate of top left corner: ";

cin >> a->location.y;

cout << "Enter length of rectangle: ";

cin >> a->l;

cout << "Enter width of rectangle: ";

cin >> a->w;

}

void printRect(RectT *a) {

cout << "(" << a->location.x << ", " << a->location.y << ") ";

cout << "(" << a->location.x << ", " << (a->location.y - a->l) << ") ";

cout << "(" << (a->location.x + a->w) << ", " << (a->location.y - a->l) << ") ";

cout << "(" << (a->location.x + a->w) << ", " << a->location.y << ") ";

cout << endl;

}

int main(void) {

RectT a, b;

cout << "Rectangle 1:\n";

inputRect(&a);

cout << "\nRectangle 2:\n";

inputRect(&b);

cout << "\n";

int intersects = chk_intersect(a, b);

cout << "Co-ordinates of rectangle 1: ";

printRect(&a);

cout << "Co-ordinates of rectangle 2: ";

printRect(&b);

if(intersects) {

cout << "Rectangles intersect!\n";

} else {

cout << "Rectangles do not intersect!\n";

}

}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

#include <iomanip> using namespace std; typedef struct Point { double x, y; } PointT; clang version 7.0.0-3-ubuntuo.18.04.1 (t ags/RELEASE_700/final) ? clang++-7 -pthread -o main main.cpp 3./main Rectangle 1: Enter x coordinate of top left corner: 2 Enter y coordinate of top left corner: 4 Enter length of rectangle: 2 Enter width of rectangle: 3 10 typedef struct Rect { Point location; double 1, w; } RectT; Rectangle 2: Enter x coordinate of top left corner: 4 Enter y coordinate of top left corner: 3 Enter length of rectangle: 2 Enter width of rectangle: 3 Co-ordinates of rectangle 1: (2, 4) (2, 2) (5, 4) (5, 2) Co-ordinates of rectangle 2: (4, 3) (4, 1) (7, 3) (7, 1) Rectangles overlap! int chk_intersect(RectT a, RectT b) { // left upper and lower right coordinates of rect a PointT 11, r1; // left upper and lower right coordinates of rect b Point 12, r2; 11.x = a.location.x; 11.y = a.location.y; r1.x = a.location. x + a.W; r1.y = a.location.y - a.l; 12.x = b.location.x; 12.y = b. location.y; r2.x = b.location.x + b.w; r2.y = b. location.y - b.l;

Add a comment
Know the answer?
Add Answer to:
this assignment should be in C++ In this assignment, you will implement two classes. Point: A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose there are two non-empty list variables of equal length in Python called coords and sides. The variable coords contains sub-lists of size 2, with each of these values representing an x and a y...

    Suppose there are two non-empty list variables of equal length in Python called coords and sides. The variable coords contains sub-lists of size 2, with each of these values representing an x and a y co-ordinate. The variable sides also contains sub-lists of size 2, with each of these values representing firstly the length thern the width of a rectangle. Also assume there is also a function in Python called draw_rectangle which takes as its parameters two integers representing firstly...

  • 1. In IntelliJ create a new project called F1_2 2. In the Project window create a...

    1. In IntelliJ create a new project called F1_2 2. In the Project window create a new Java package called F1_2. This can be done by right clicking on src and going to New ! Package. 3. In package lab1 2 create a class called Rectangle. This can be done by right clicking on the package and going to New ! Java Class 4. At the beginning of Rectangle.java add the line package lab1 2; 5. In Rectangle.java create a...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

  • java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named...

    java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named "MyRectangle3d" Your class must have the following attributes or variables · x (the x coordinate of the rectangle, an integer) * y (the y coordinate of the rectangle, an integer) * length (the length of the rectangle, an integer) * width (the width of the rectangle, an integer) * height (the height of the rectangle, an integer) * color( the color of the rectangle,...

  • In Java please! Problem You will be using the point class discussed in class to create...

    In Java please! Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...

  • The both files are included. Where are these which are colorful. Point.h and point.cpp Hor this assignment you are provided the files for a class called point. Download the h and .cpp files and incl...

    The both files are included. Where are these which are colorful. Point.h and point.cpp Hor this assignment you are provided the files for a class called point. Download the h and .cpp files and include them in your project. IheじML diagram below details the class Point くくfriend>> ostream& operator.((ostream&, point&) <ごfriend::. İstream& operator:..イ1stream&-point& - : double - v doublc getX) double getYO double - sctX( double): void - set Y(double) : void - point(double-0.0, double-0.0 operator-(const point& bool perator< const...

  • Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are...

    Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions: 1. A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter. 2. A function to return the area of a rectangle. This function shall...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT