Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #22277 > unrolled thread

Converting Text File To Rectangles

Started byScyth3 <zfollette@gmail.com>
First post2013-02-11 17:51 -0800
Last post2013-02-12 11:41 -0800
Articles 15 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Converting Text File To Rectangles Scyth3 <zfollette@gmail.com> - 2013-02-11 17:51 -0800
    Re: Converting Text File To Rectangles Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-02-11 21:02 -0500
      Re: Converting Text File To Rectangles Gene Wirchenko <genew@telus.net> - 2013-02-13 09:06 -0800
        Re: Converting Text File To Rectangles Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-02-13 13:25 -0500
          Re: Converting Text File To Rectangles "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-02-15 15:25 +0000
            Re: Converting Text File To Rectangles "John B. Matthews" <nospam@nospam.invalid> - 2013-02-16 13:37 -0500
              [OT] Re: Converting Text File To Rectangles Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-02-16 15:16 -0500
                Re: [OT] Re: Converting Text File To Rectangles "John B. Matthews" <nospam@nospam.invalid> - 2013-02-17 14:16 -0500
          Re: Converting Text File To Rectangles Gene Wirchenko <genew@telus.net> - 2013-02-15 13:09 -0800
    Re: Converting Text File To Rectangles Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-02-12 04:47 -0400
      Re: Converting Text File To Rectangles lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-02-12 12:14 +0000
    Re: Converting Text File To Rectangles lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-02-12 09:47 +0000
      Re: Converting Text File To Rectangles lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-02-12 09:50 +0000
      Re: Converting Text File To Rectangles Roedy Green <see_website@mindprod.com.invalid> - 2013-02-12 18:23 -0800
    Re: Converting Text File To Rectangles Roedy Green <see_website@mindprod.com.invalid> - 2013-02-12 11:41 -0800

#22277 — Converting Text File To Rectangles

FromScyth3 <zfollette@gmail.com>
Date2013-02-11 17:51 -0800
SubjectConverting Text File To Rectangles
Message-ID<7f49c487-f016-43a3-83a4-feebe072f76c@googlegroups.com>
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?

[toc] | [next] | [standalone]


#22279

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-02-11 21:02 -0500
Message-ID<kfc7r5$rd1$1@dont-email.me>
In reply to#22277
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

[toc] | [prev] | [next] | [standalone]


#22292

FromGene Wirchenko <genew@telus.net>
Date2013-02-13 09:06 -0800
Message-ID<2rhnh817qca2g575nk2l0p6sqe2g8t03fi@4ax.com>
In reply to#22279
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

[toc] | [prev] | [next] | [standalone]


#22294

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-02-13 13:25 -0500
Message-ID<kfglor$s6i$1@dont-email.me>
In reply to#22292
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

[toc] | [prev] | [next] | [standalone]


#22325

From"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org>
Date2013-02-15 15:25 +0000
Message-ID<RqGdnSDwDL_7zoPMnZ2dnUVZ8sKdnZ2d@bt.com>
In reply to#22294
Eric Sosman wrote:

> lying athwart [...]

You've just made my afteroon.

Thanks

    -- chris 

[toc] | [prev] | [next] | [standalone]


#22332

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-02-16 13:37 -0500
Message-ID<nospam-118D8E.13372816022013@news.aioe.org>
In reply to#22325
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>

[toc] | [prev] | [next] | [standalone]


#22333 — [OT] Re: Converting Text File To Rectangles

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-02-16 15:16 -0500
Subject[OT] Re: Converting Text File To Rectangles
Message-ID<kfope5$qof$1@dont-email.me>
In reply to#22332
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

[toc] | [prev] | [next] | [standalone]


#22336 — Re: [OT] Re: Converting Text File To Rectangles

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-02-17 14:16 -0500
SubjectRe: [OT] Re: Converting Text File To Rectangles
Message-ID<nospam-72F520.14162617022013@news.aioe.org>
In reply to#22333
In article <kfope5$qof$1@dont-email.me>,
 Eric Sosman <esosman@comcast-dot-net.invalid> wrote:

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

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>

[toc] | [prev] | [next] | [standalone]


#22328

FromGene Wirchenko <genew@telus.net>
Date2013-02-15 13:09 -0800
Message-ID<qr8th85agonp30ndovfg0pno44a0dr80ik@4ax.com>
In reply to#22294
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

[toc] | [prev] | [next] | [standalone]


#22281

FromArved Sandstrom <asandstrom2@eastlink.ca>
Date2013-02-12 04:47 -0400
Message-ID<hknSs.136174$EO2.136144@newsfe04.iad>
In reply to#22277
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

[toc] | [prev] | [next] | [standalone]


#22284

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-02-12 12:14 +0000
Message-ID<CPadnai5MOXZr4fMnZ2dnUVZ8rudnZ2d@bt.com>
In reply to#22281
On 12/02/13 08:47, Arved Sandstrom wrote:
> 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:

[snip]

>
> Do you even have a rectangle class ready to go?

I thought of this (a Rectangle class) but went with RectData
because to my mind, abstractly, a Rectangle is just a length and a width 
and wouldn't contain coordinates and I was going 'quick and dirty'. I 
suppose a better way might be

class Rectangle{
    length, width
    ...
}

class Coord{
    x,y
    ...
}

class CoordinatedRectangle{
    Rectangle rect
    Coord coord
    ...
}


But I'm sure you have/had a better idea.

lipska

-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

[toc] | [prev] | [next] | [standalone]


#22282

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-02-12 09:47 +0000
Message-ID<j62dnbkquNlNkofMnZ2dnUVZ8n2dnZ2d@bt.com>
In reply to#22277
On 12/02/13 01:51, 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:

[snip]

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

Off the top of my head, on the way out the door to the swimming pool, 
and for multiple rectangles (squares?).

Refactor as required


import java.awt.Graphics;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {

	LinkedList<RectData> data = null;

	MyCanvas(LinkedList<RectData> data) {
		this.data = data;
	}

	public void paint(Graphics g) {
		Iterator<RectData> iter = data.iterator();
		RectData i = null;
		while (iter.hasNext()) {
			i = iter.next();
			g.drawRect(i.x, i.y, i.width, i.height);
		}
	}
}

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;
	}
}

public class DrawRect {

	static LinkedList<RectData> getData() {
		
		LinkedList<RectData> data = new LinkedList<RectData>();
		
		data.add(new RectData(50, 250, 50, 50));
		data.add(new RectData(50, 350, 50, 50));
		data.add(new RectData(50, 400, 50, 50));
		data.add(new RectData(50, 500, 50, 50));
		data.add(new RectData(150, 300, 50, 50));
		data.add(new RectData(200, 450, 50, 50));
		data.add(new RectData(200, 500, 50, 50));
		data.add(new RectData(250, 150, 50, 50));
		data.add(new RectData(300, 100, 50, 50));
		data.add(new RectData(400, 50, 50, 50));
		data.add(new RectData(450, 150, 50, 50));
		data.add(new RectData(550, 100, 50, 50));
		
		return data;
	}

	public static void main(String[] a) {
		JFrame window = new JFrame();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setBounds(30, 30, 600, 600);
		window.getContentPane().add(new MyCanvas(getData()));
		window.setVisible(true);
	}
}


lipska

-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

[toc] | [prev] | [next] | [standalone]


#22283

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-02-12 09:50 +0000
Message-ID<j62dnbgquNnkjYfMnZ2dnUVZ8n2dnZ2d@bt.com>
In reply to#22282
On 12/02/13 09:47, lipska the kat wrote:
> On 12/02/13 01:51, 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:
>
> [snip]
>
>> Now, how would I read this, and turn it into a rectangle?
>
> Off the top of my head, on the way out the door to the swimming pool,
> and for multiple rectangles (squares?).
>
> Refactor as required

arse

forgot to ask if homework, sorry :-(

lipska


-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

[toc] | [prev] | [next] | [standalone]


#22289

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-02-12 18:23 -0800
Message-ID<sutlh89ohk1tbbjrvud7tkrt4m0cdbbelh@4ax.com>
In reply to#22282
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 

[toc] | [prev] | [next] | [standalone]


#22286

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-02-12 11:41 -0800
Message-ID<gd6lh8dhfs4ocg5r652nb7t18gk3t4r457@4ax.com>
In reply to#22277
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 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web