Reputation: 11
I am trying to understand the Rectangle Class and MyCircle class, specifically the containsPoint methods in each one. Here is the code:
public class MyRectangle extends GridItem {
private int height;
private int width;
public MyRectangle(int xValue, int yValue, int w, int h) {
x = xValue;
y = yValue;
width = w;
height = h;
}
public double getArea() {
return height * width;
}
public boolean containsPoint(int xValue, int yValue)
{
return xValue >= x &&
xValue <= x + width &&
yValue >= y &&
yValue <= y + height;
}
}
The confusion I'm having is, what does the containsPoint method mean? How was this current code set up in this particular way, since isn't that supposed to return a boolean and not data types of the int?
Same dilemma for the MyCircle class.
public class MyCircle extends GridItem {
private int radius;
public MyCircle(int xValue, int yValue, int r)
{
x = xValue;
y = yValue;
radius = r;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public boolean containsPoint(int xValue, int yValue) {
double dx = x - xValue;
double dy = y - yValue;
double distance = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
return distance <= radius;
}
}
What exactly are they meaning by the containsPoint method? How do you interpret this? Been stumped for days and this is part of a bigger assignment, but cannot comprehend the containsPoint method so it's affect the development of mySquare class.....
So far I've got this..
public class MySquare extends GridItem
{
private int side;
public MySquare(int xValue, int yValue, int s)
{
x = xValue;
y = yValue;
side = s;
}
@Override
public double getArea()
{
return side*side;
}
@Override
public boolean containsPoint(int xValue, int yValue)
{
return x && y;
}
}
How does one apply the containsPoint method in the Square class?
Thanks!
Upvotes: 1
Views: 872
Reputation: 17880
Let's consider the containsPoint
of Rectangle.
Let's assume you have a rectangle of height 2 and width 3 starting at co-ordinate (1,1). So your rectangle would look like this
(1,3) (4,3)
------------
| |
| |
------------
(1,1) (4,1)
(In the above example) given two points xValue
and yValue
, containsPoint returns true if
xValue
is between 1 and 4 (inclusive) and
yValue
is between 1 and 3 (inclusive)
and false otherwise
Thus, containsPoint
tells whether a given point lies on/within a shape.
The containsPoint
method of a circle also does the same thing (whether a point lies within/on the circle), however the formula is a bit more involved. You can refer to the Euclidean distance for two dimensions to understand it better.
The containsPoint
for a Square will be very similar to that of a rectangle except for using width
and heigth
, you would have only one side
.
return xValue >= x &&
xValue <= x + side &&
yValue >= y &&
yValue <= y + side;
Upvotes: 0
Reputation: 17454
what does the containsPoint method mean?
The method just checks if the given point (the given x,y coordinates i.e. xValue, yValue) is within the current Square or Rectangle.
How was this current code set up in this particular way, since isn't that supposed to return a boolean and not data types of the int?
The method arguments are int
because they indicate the x and y coordinates for the given point.
Been stumped for days and this is part of a bigger assignment, but cannot comprehend the containsPoint method so it's affect the development of mySquare class.....
Your sub-classes such as the Sqaure
class is supposed to have a set of attributes such as x
, y
, width
, height
which indicates the position and size of the square. Based on this set of attributes, check if any given point (xValue
, yValue
) is within your current square. The same applies for Rectangle
class.
Upvotes: 1
Reputation: 2282
Comparing variables with each other will result in an boolean value.
Each comparison in containsPoint()
in MyRectangle
yields a boolean value which are then connected via and. This means that it will only return true if every single comparison yields true.
You would need to apply the same principle to MySquare
.
Think about how the coordinates of the square compare to the coordinates of a point if the point is inside the square.
Upvotes: 0
Reputation: 18235
The containsPoint
is the method to check if a point is inside a specific rectangle / circle / shapes on 2D plane.
Upvotes: 0