Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Converting Text File To Rectangles (http://www.velocityreviews.com/forums/t957532-converting-text-file-to-rectangles.html)

Scyth3 02-12-2013 01:51 AM

Converting Text File To Rectangles
 
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?

Eric Sosman 02-12-2013 02:02 AM

Re: Converting Text File To Rectangles
 
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
esosman@comcast-dot-net.invalid

Arved Sandstrom 02-12-2013 08:47 AM

Re: Converting Text File To Rectangles
 
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

Roedy Green 02-12-2013 07:41 PM

Re: Converting Text File To Rectangles
 
On Mon, 11 Feb 2013 17:51:10 -0800 (PST), Scyth3 <zfollette@gmail.com>
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 02-13-2013 02:23 AM

Re: Converting Text File To Rectangles
 
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

Gene Wirchenko 02-13-2013 05:06 PM

Re: Converting Text File To Rectangles
 
On Mon, 11 Feb 2013 21:02:47 -0500, Eric Sosman
<esosman@comcast-dot-net.invalid> 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

Eric Sosman 02-13-2013 06:25 PM

Re: Converting Text File To Rectangles
 
On 2/13/2013 12:06 PM, Gene Wirchenko wrote:
> On Mon, 11 Feb 2013 21:02:47 -0500, Eric Sosman
> <esosman@comcast-dot-net.invalid> 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
esosman@comcast-dot-net.invalid

Gene Wirchenko 02-15-2013 09:09 PM

Re: Converting Text File To Rectangles
 
On Wed, 13 Feb 2013 13:25:08 -0500, Eric Sosman
<esosman@comcast-dot-net.invalid> 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

John B. Matthews 02-16-2013 06:37 PM

Re: Converting Text File To Rectangles
 
In article <RqGdnSDwDL_7zoPMnZ2dnUVZ8sKdnZ2d@bt.com>,
"Chris Uppal" <chris.uppal@metagnostic.REMOVE-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>

Eric Sosman 02-16-2013 08:16 PM

[OT] Re: Converting Text File To Rectangles
 
On 2/16/2013 1:37 PM, John B. Matthews wrote:
> In article <RqGdnSDwDL_7zoPMnZ2dnUVZ8sKdnZ2d@bt.com>,
> "Chris Uppal" <chris.uppal@metagnostic.REMOVE-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
esosman@comcast-dot-net.invalid


All times are GMT. The time now is 06:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.