2 years ago

#36846

test-img

Dima Tkachuk

I was asked to write a code that returns a rectangle that blocks any given polygon between 3 and 10 points (java+arrays+loops)

what the method needs to do: to put it simply, we receive any number of points(from class Point) like (1,4)...(7,2) and there can be anywhere from 3 to 10 (if less then 3 it returns null) and the methods needs to return the rectangle(which needs to be from class Polygon) that blocks the Polygon created from the given points, (in other words we get points that create a polygon and we need to find the rectangle that blocks the polygon),

the problem: I believe the code i created should work? but my main problem is I don't know how to write the return command to return those values that are from class Point and print them in this method which is supposed to be class Polygon, I have a toString command that prints out everything i just don't know how can i take those point values from SW,SE,NE,NW and insert return them with toString inside the getBoundingBox() method

Thanks if you can help i will add the code below, the method i am having trouble with in the return command is public Polygon getBoundingBox()

//tester
Polygon boundingBox = polygon.getBoundingBox();
String boundingBoxStr = boundingBox.toString();
if (boundingBoxStr.equals("The polygon has 4 vertices:\n((1,0),(7,0),(7,6),(1,6))"))
    System.out.println("\ngetBoundingBox - OK");
else
{
    System.out.println("\nError in getBoundingBox or in toString");
    System.out.println("Your bounding box is:\n" + boundingBox + "\nExpected bounding box is:\nThe polygon has 4 vertices:\n((1,0),(7,0),(7,6),(1,6))");
}
// instance variables -
private Point [] _vertices;
private int _noOfVertices;

private  final int MAX_VERTICES = 10;
// constructor
public Polygon()
{
    _vertices = new Point[MAX_VERTICES];
    _noOfVertices = 0;
}
// add vertex(or point)
public boolean addVertex(int x, int y)
{
    if (_noOfVertices == MAX_VERTICES)
        return false;

    _vertices[_noOfVertices++] = new Point(x,y);
    return true;
}
//toString
public String toString()
{
    String str = "";

    if(_noOfVertices == 0)  
    {
        str = "The polygon has 0 vertices.";
    }

    int i;
    if(_noOfVertices > 0)  
    {
        str += "The polygon has " + _noOfVertices +  " vertices:" + "\n";
        str += "" + ("(");

        for(i=0; i < _noOfVertices; i++)
            if (i == _noOfVertices-1)
            {
                str += "" + _vertices[i];

            }
            else
            {
                str += "" + _vertices[i] + ",";

            }

        if(i ==_noOfVertices)
        {
            str += "" + (")");
        }

    }
    return str;
}
public Polygon getBoundingBox()
{
    Polygon rectangle = new Polygon();
    String str = "";

    if(_noOfVertices < 3)
        return null;

    Point SW = _vertices[0];
    Point SE = _vertices[0];
    Point NE = _vertices[0];
    Point NW = _vertices[0];

    for (int i=1; i<_noOfVertices; i++)
    {
        if(_vertices[i].isLeft(SW) && _vertices[i].isUnder(SW))
        {
            SW = _vertices[i];
        }

        if(_vertices[i].isRight(SE) && _vertices[i].isUnder(SE))
        {
            SE = _vertices[i];
        }

        if(_vertices[i].isRight(NE) && _vertices[i].isAbove(NE))
        {
            NE = _vertices[i];
        }

        if(_vertices[i].isLeft(NW) && _vertices[i].isAbove(NW))
        {
            NW = _vertices[i];
        }

    }

    return new Polygon(SW,SE,NE,NW); // if i try to add the points SW,SE,NE,NW to Polygon i get an error "constructor polygon in class polygon can't be 
    // to given types

    // I am suppsoed to print it out like this using toString command:
    // The polygon has 5 vertices: 
    // ((SW,SE,NE,NW))
}

java

arrays

loops

polygon

point

0 Answers

Your Answer

Accepted video resources