Converting Text File To Rectangles

Discussion in 'Java' started by Scyth3, Feb 12, 2013.

  1. Scyth3

    Scyth3 Guest

    I have a save and load method for certain rectangles I need for a game. Say the user wants to save a map. The outputted map would look like this:


    50 250 50 50
    50 350 50 50
    50 400 50 50
    50 500 50 50
    150 300 50 50
    200 450 50 50
    200 500 50 50
    250 150 50 50
    300 100 50 50
    400 50 50 50
    450 150 50 50
    550 100 50 50

    X Y Width Height

    Now, how would I read this, and turn it into a rectangle?
     
    Scyth3, Feb 12, 2013
    #1
    1. Advertising

  2. Scyth3

    Eric Sosman Guest

    On 2/11/2013 8:51 PM, Scyth3 wrote:
    > I have a save and load method for certain rectangles I need for a game. Say the user wants to save a map. The outputted map would look like this:
    >
    >
    > 50 250 50 50
    > 50 350 50 50
    > 50 400 50 50
    > 50 500 50 50
    > 150 300 50 50
    > 200 450 50 50
    > 200 500 50 50
    > 250 150 50 50
    > 300 100 50 50
    > 400 50 50 50
    > 450 150 50 50
    > 550 100 50 50
    >
    > X Y Width Height
    >
    > Now, how would I read this, and turn it into a rectangle?


    Turning it into twelve rectangles would be easy, but I don't
    know how you'd turn it into one. Perhaps you could keep track of
    the minimum X,Y and the maximum implied X,Y and form one rectangle
    that surrounds the entire thing. That rectangle might not be the
    minimal surrounding rectangle, but it would be minimal among those
    whose sides were parallel to the axes.

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 12, 2013
    #2
    1. Advertising

  3. On 02/11/2013 09:51 PM, Scyth3 wrote:
    > I have a save and load method for certain rectangles I need for a game. Say the user wants to save a map. The outputted map would look like this:
    >
    >
    > 50 250 50 50
    > 50 350 50 50
    > 50 400 50 50
    > 50 500 50 50
    > 150 300 50 50
    > 200 450 50 50
    > 200 500 50 50
    > 250 150 50 50
    > 300 100 50 50
    > 400 50 50 50
    > 450 150 50 50
    > 550 100 50 50
    >
    > X Y Width Height
    >
    > Now, how would I read this, and turn it into a rectangle?
    >

    You _already_ have a "save and load" method, or you'd _like to_ have all
    of the above? This is basic enough to practically be a CS assignment;
    even if not, it's not doing you any learning favours to tell you much
    about how to do any of this.

    Think the problem though, and decompose it. You have that file, and
    assume it's been created by a save method operating on a list of
    rectangle objects. Now you need to read it - find the Java packages and
    classes that have to do with reading from text files. Determine how to
    read line by line from such a file. Either operate on each line as you
    read it, or store the lines to do that as a second step. Decide how
    you'll split each line into 4 text representations of numbers, and how
    you'll convert each into a number.

    Do you even have a rectangle class ready to go?

    AHS
     
    Arved Sandstrom, Feb 12, 2013
    #3
  4. Scyth3

    Roedy Green Guest

    On Mon, 11 Feb 2013 17:51:10 -0800 (PST), Scyth3 <>
    wrote, quoted or indirectly quoted someone who said :

    >Now, how would I read this, and turn it into a rectangle?


    see http://mindprod.com/jgloss.canvas.html and follow links.

    To read, put commas between fields and read/write with CSVReader and
    CSVWriter.

    See http://mindprod.com/application/csv.manual.html

    Otherwise see http://mindprod.com/applet/fileio.html to learn how to
    read a line, then read http://mindprod.com/jgloss/regex.html to learn
    how to break it up with Pattern.split
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
     
    Roedy Green, Feb 12, 2013
    #4
  5. Scyth3

    Roedy Green Guest

    On Tue, 12 Feb 2013 09:47:58 +0000, lipska the kat <"nospam at
    neversurrender dot co dot uk"> wrote, quoted or indirectly quoted
    someone who said :

    >class RectData {
    >
    > Integer x, y, width, height;
    >
    > RectData(Integer x, Integer y, Integer width, Integer height) {
    > this.x = x;
    > this.y = y;
    > this.width = width;
    > this.height = height;
    > }


    There is already a built in class like this called Rectangle

    You want int not Integer parms.

    see http://mindprod.com/jgloss/intvsinteger.html

    You could have an array or ArrayList of Rectangles and in you paint
    method, you render them either as outlines or filled
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
     
    Roedy Green, Feb 13, 2013
    #5
  6. On Mon, 11 Feb 2013 21:02:47 -0500, Eric Sosman
    <> wrote:

    >On 2/11/2013 8:51 PM, Scyth3 wrote:
    >> I have a save and load method for certain rectangles I need for a game. Say the user wants to save a map. The outputted map would look like this:
    >>
    >>
    >> 50 250 50 50
    >> 50 350 50 50
    >> 50 400 50 50
    >> 50 500 50 50
    >> 150 300 50 50
    >> 200 450 50 50
    >> 200 500 50 50
    >> 250 150 50 50
    >> 300 100 50 50
    >> 400 50 50 50
    >> 450 150 50 50
    >> 550 100 50 50
    >>
    >> X Y Width Height
    >>
    >> Now, how would I read this, and turn it into a rectangle?

    >
    > Turning it into twelve rectangles would be easy, but I don't
    >know how you'd turn it into one. Perhaps you could keep track of
    >the minimum X,Y and the maximum implied X,Y and form one rectangle
    >that surrounds the entire thing. That rectangle might not be the
    >minimal surrounding rectangle, but it would be minimal among those
    >whose sides were parallel to the axes.


    It would be a minimal regardless of whether the sides were
    parallel to the axes. The vertices of each rectangle give the
    furthest extent of the rectangle in one direction (or two in the
    parallel case).

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Feb 13, 2013
    #6
  7. Scyth3

    Eric Sosman Guest

    On 2/13/2013 12:06 PM, Gene Wirchenko wrote:
    > On Mon, 11 Feb 2013 21:02:47 -0500, Eric Sosman
    > <> wrote:
    >
    >> On 2/11/2013 8:51 PM, Scyth3 wrote:
    >>> I have a save and load method for certain rectangles I need for a game. Say the user wants to save a map. The outputted map would look like this:
    >>>
    >>>
    >>> 50 250 50 50
    >>> 50 350 50 50
    >>> 50 400 50 50
    >>> 50 500 50 50
    >>> 150 300 50 50
    >>> 200 450 50 50
    >>> 200 500 50 50
    >>> 250 150 50 50
    >>> 300 100 50 50
    >>> 400 50 50 50
    >>> 450 150 50 50
    >>> 550 100 50 50
    >>>
    >>> X Y Width Height
    >>>
    >>> Now, how would I read this, and turn it into a rectangle?

    >>
    >> Turning it into twelve rectangles would be easy, but I don't
    >> know how you'd turn it into one. Perhaps you could keep track of
    >> the minimum X,Y and the maximum implied X,Y and form one rectangle
    >> that surrounds the entire thing. That rectangle might not be the
    >> minimal surrounding rectangle, but it would be minimal among those
    >> whose sides were parallel to the axes.

    >
    > It would be a minimal regardless of whether the sides were
    > parallel to the axes. The vertices of each rectangle give the
    > furthest extent of the rectangle in one direction (or two in the
    > parallel case).


    The minimal surrounding rectangle's sides are not necessarily
    parallel to the axes. Counterexample: Let there be two surrounded
    rectangles, each a 2x2 square, one centered at (1,1) and the other
    at (99,99). The minimal axis-aligned surrounding rectangle is a
    square with one corner at the origin and its opposite at (100,100);
    its area is 100*100=10000 and its perimeter is 4*100=400.

    Now consider a long skinny rectangle at a 45-degree angle,
    lying athwart the y=x line. To enclose the two small squares, it
    must be 2*sqrt(2) wide and 100*sqrt(2) long, for an area of
    2*100*2=400 and a perimeter of 2*(2+100)*sqrt(2)~=288.5, both
    considerably smaller than their counterparts on the large square.

    It's true I didn't specify what I meant by "minimal," and
    there may be other measures than area and perimeter that would
    lead to a different conclusion. But for those two, at least,
    requiring the rectangle's sides to be axis-parallel can lead
    to rejecting smaller non-aligned rectangles.

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 13, 2013
    #7
  8. On Wed, 13 Feb 2013 13:25:08 -0500, Eric Sosman
    <> wrote:

    >On 2/13/2013 12:06 PM, Gene Wirchenko wrote:


    [snip]

    >> It would be a minimal regardless of whether the sides were
    >> parallel to the axes. The vertices of each rectangle give the
    >> furthest extent of the rectangle in one direction (or two in the
    >> parallel case).

    >
    > The minimal surrounding rectangle's sides are not necessarily
    >parallel to the axes. Counterexample: Let there be two surrounded


    It seemed that that was being considered. If it is so, then
    there is no need to get into the anglular positions of the rectangles
    being enclosed. If not, then it is much messier.

    [snip]

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Feb 15, 2013
    #8
  9. In article <>,
    "Chris Uppal" <-THIS.org> wrote:

    > Eric Sosman wrote:
    >
    > > lying athwart [...]

    >
    > You've just made my afteroon.


    See also <http://www.shmoop.com/kubla-khan/poem-text.html>.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Feb 16, 2013
    #9
  10. Scyth3

    Eric Sosman Guest

    [OT] Re: Converting Text File To Rectangles

    On 2/16/2013 1:37 PM, John B. Matthews wrote:
    > In article <>,
    > "Chris Uppal" <-THIS.org> wrote:
    >
    >> Eric Sosman wrote:
    >>
    >>> lying athwart [...]

    >>
    >> You've just made my afteroon.

    >
    > See also <http://www.shmoop.com/kubla-khan/poem-text.html>.


    Given all the interest in the word, it's sort of a shame
    that I used it incorrectly ... <Sigh>

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 16, 2013
    #10
  11. Re: [OT] Re: Converting Text File To Rectangles

    In article <kfope5$qof$>,
    Eric Sosman <> wrote:

    > On 2/16/2013 1:37 PM, John B. Matthews wrote:
    > > In article <>,
    > > "Chris Uppal" <-THIS.org> wrote:
    > >
    > >> Eric Sosman wrote:
    > >>
    > >>> lying athwart [...]
    > >>
    > >> You've just made my afteroon.

    > >
    > > See also <http://www.shmoop.com/kubla-khan/poem-text.html>.

    >
    > Given all the interest in the word, it's sort of a shame
    > that I used it incorrectly ... <Sigh>


    I would say your usage was apt, whimsical, if not transcendental,
    and easily understood in context. More than the word itself, I
    would also acknowledge your gentle nudge to think outside the
    (upright rectangular) box.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Feb 17, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    531
  2. Ajay Patil
    Replies:
    0
    Views:
    460
    Ajay Patil
    Dec 10, 2003
  3. Chuck Dillon
    Replies:
    4
    Views:
    1,084
    Steve W. Jackson
    Feb 10, 2004
  4. Abs
    Replies:
    4
    Views:
    8,536
    Jesper Nordenberg
    May 14, 2004
  5. Michal Pleban

    MIDP 1.0: Filled rectangles

    Michal Pleban, May 26, 2004, in forum: Java
    Replies:
    0
    Views:
    432
    Michal Pleban
    May 26, 2004
Loading...

Share This Page