Converting Text File To Rectangles

S

Scyth3

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?
 
E

Eric Sosman

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.
 
A

Arved Sandstrom

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
 
R

Roedy Green

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
 
R

Roedy Green

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
 
G

Gene Wirchenko

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
 
E

Eric Sosman

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.
 
G

Gene Wirchenko

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
 
J

John B. Matthews

Eric Sosman said:
Chris Uppal said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top