Jump to content

Draw labyrinth ( I need help)

- - - - -

  • Please log in to reply
12 replies to this topic

#1
ramin

ramin

    Newbie

  • Members
  • Pip
  • 9 posts
Hello!

I have a big problem with a exercise. its about drawing a labyrinth.
The question is :

Write a program that uses the DrawingPanel to draw the following spiral figure:

Posted Image

The window is 170 pixels wide and 170 pixels tall. The background is white and the foreground is black. The spiral line begins at point (0, 10) and spirals inward. There are 10 pixels between each arm of the spiral. 8 spirals are made in total. The initial spiral touches points (0, 10), (160, 10), (160, 160), (10, 160), and (10, 20).

For additional challenge, parameterize your program with parameters such as the window size and the number of spiral loops desired.

plz help me

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
It's impossible to help unless we know what code you've written already. What specific part are you having trouble with? Post code snippits and indicate where you're having trouble and we'll do the best we can.

If you haven't anything written yet and don't know where to begin, might I suggest using loops to accomplish this.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Check this out:
http://forum.codecal...bly-8086-a.html (post #7 is in particular what I'm talking about, but I think it would be worthy to at least read the first two posts as well)

It might not be exactly what you're doing, but I think it might be helpful, somewhat. You could then use equations and formulas to find where from, and where to, to draw the lines.

#4
ramin

ramin

    Newbie

  • Members
  • Pip
  • 9 posts
This is my code that i have written. I know it's wrong beacause the instructions i have tells me to create a static method drawLabyrinth that draws the maze task and set a parameter to the method to set the window size and number of turns in the maze. how do i do that? and do i need to change a lot in my code?

import java.awt.*;

public class labyrint {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawingPanel panel = new DrawingPanel(170,170);
panel.setBackground(Color.WHITE);
Graphics g = panel.getGraphics();

g.setColor(Color.BLACK);
g.drawLine(160 ,10, 0, 10);
g.drawLine(160 ,160, 160, 10);
g.drawLine(10 ,160, 160, 160);
g.drawLine(10 ,20, 10, 160);

g.drawLine(150 ,20, 10, 20);
g.drawLine(150 ,150, 150, 20);
g.drawLine(20 ,150, 150, 150);
g.drawLine(20 ,30, 20, 150);

g.drawLine(140 ,30, 20, 30);
g.drawLine(140 ,140, 140, 30);
g.drawLine(30 ,140, 140, 140);
g.drawLine(30 ,40, 30, 140);

g.drawLine(130 ,40, 30, 40);
g.drawLine(130 ,130, 130, 40);
g.drawLine(40 ,130, 130, 130);
g.drawLine(40 ,50, 40, 130);

g.drawLine(120 ,50, 40, 50);
g.drawLine(120 ,120, 120, 50);
g.drawLine(50 ,120, 120, 120);
g.drawLine(50 ,60, 50, 120);

g.drawLine(110 ,60, 50, 60);
g.drawLine(110 ,110, 110, 60);
g.drawLine(60 ,110, 110, 110);
g.drawLine(60 ,70, 60, 110);

g.drawLine(100 ,70, 60, 70);
g.drawLine(100 ,100, 100, 70);
g.drawLine(70 ,100, 100, 100);
g.drawLine(70 ,80, 70, 100);

g.drawLine(90 ,80, 70, 80);
g.drawLine(90 ,90, 90, 80);
g.drawLine(80 ,90, 90, 90);
g.drawLine(80 ,90, 80, 90);


}
}

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Like suggested you will need loops for this.

Replace your current code so it only draws this:

Attached File  maze.png   2.94K   8 downloads

Then try finding the steps the numbers take each line (like x1 will go +10 pixels every new line, y1 will do +10 as well, etc etc) and pour it into a loop.
Start by just the lines of the image, doing it all at once just makes it harder for yourself to find / understand.

#6
ramin

ramin

    Newbie

  • Members
  • Pip
  • 9 posts

wim DC said:

Like suggested you will need loops for this.

Replace your current code so it only draws this:

[ATTACH=CONFIG]4464[/ATTACH]

Then try finding the steps the numbers take each line (like x1 will go +10 pixels every new line, y1 will do +10 as well, etc etc) and pour it into a loop.
Start by just the lines of the image, doing it all at once just makes it harder for yourself to find / understand.


thank you but how can i change the code to what you are showing me? im quite new to this and do not understand so much java, i really want to do this exercise

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

ramin said:

thank you but how can i change the code to what you are showing me? im quite new to this and do not understand so much java, i really want to do this exercise
Remove lines 2,3 and 4 of all those 4 line blocks that draw a line.

And you shouldn't copy paste code from others if you don't understand it.

#8
ramin

ramin

    Newbie

  • Members
  • Pip
  • 9 posts

wim DC said:

Remove lines 2,3 and 4 of all those 4 line blocks that draw a line.

And you shouldn't copy paste code from others if you don't understand it.
i haven't copied anything i wrote this myself all i need is an explanation for how to write the loop correctly
or clues

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
How can you write all those lines yourself, and not know how to get only the top half to draw?

Anyway, if you draw only the horizontal top half lines you are left with this:
g.drawLine(160 ,10, 0, 10);
g.drawLine(150 ,20, 10, 20);
g.drawLine(140 ,30, 20, 30);
g.drawLine(130 ,40, 30, 40);
g.drawLine(120 ,50, 40, 50);
g.drawLine(110 ,60, 50, 60);
g.drawLine(100 ,70, 60, 70);
g.drawLine(90 ,80, 70, 80);
If you make a loop that loops 8 times:

for(int i=0 ; i<8 ; i++){
    g.drawLine(x1, y1, x2, y2);
}

Then it's just a matter of finding a link between "i" and the value you need:

[TABLE="class: grid, width: 500, align: left"]
[TR]
[TD]i[/TD]
[TD]x1[/TD]
[TD]y1[/TD]
[TD]x2[/TD]
[TD]y2[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]160[/TD]
[TD]10[/TD]
[TD]0[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]150[/TD]
[TD]20[/TD]
[TD]10[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]140[/TD]
[TD]30[/TD]
[TD]20[/TD]
[TD]30[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]...[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[/TABLE]

Now of course you don't want that 160 hardcoded, but you need to find a way to calculate 160 using the value of i.
So if i is 1 (second loop), then you want 150 for x1, you could for example try "i*150" that becomes 150, so it's correct for the second loop.
In the first loop, however i would be 0, and "i*150" would be 0 instead of the required 160.

It's just a matter of finding the required calculation with i to find the correct values for every column.
I'll give you the value to calculate x2: it's "(i*10)" Go try fill the value if i in every loop and it will be what you need for x2. Just find the same link between i and x1, y1 and y2.

Use the calculated values in
g.drawLine(x1, y1, x2, y2);
and the tophalf is done. Repeat 3 times for the other 3 quarters of the maze.

[TABLE="class: grid, width: 500, align: left"]
[TR]
[TD]i[/TD]
[TD]x1[/TD]
[TD]y1[/TD]
[TD]x2[/TD]
[TD]y2[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]160[/TD]
[TD]10[/TD]
[TD](i*10)=0[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]150[/TD]
[TD]20[/TD]
[TD](i*10)=10[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]...[/TD]
[TD][/TD]
[TD](i*10)=20[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD](i*10)=30[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD](i*10)=40[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD](i*10)=50[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[TD](i*10)=60[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD](i*10)=70[/TD]
[TD][/TD]
[/TR]
[/TABLE]

#10
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

wim DC said:

for(int i=0 ; i<8 ; i++){

    g.drawLine(x1, y1, x2, y2);

}

Wouldn't it make more sense to instead do this?:
int i; 

for (i= 0; i < 8; i++){ 

    g.drawLine (x1, y1, x2, y2); 

} 
, with the i declaration above the i usage? I mean, what if someone would want to reuse that code multiple times in the same program, so then wouldn't it give an error because they would try re-declaring i?

#11
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

RhetoricalRuvim said:

Wouldn't it make more sense to instead do this?:
int i; 

for (i= 0; i < 8; i++){ 

    g.drawLine (x1, y1, x2, y2); 

} 
, with the i declaration above the i usage? I mean, what if someone would want to reuse that code multiple times in the same program, so then wouldn't it give an error because they would try re-declaring i?

How would it give an error by re-declaring it?
This is a valid set of code:

	public static void main(String[] args) {

		for(int i = 0; i < 10; ++i);

		for(int i = 0; i < 10; ++i);

		for(int i = 0; i < 10; ++i);

		int i;

	}


#12
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

lethalwire said:

How would it give an error by re-declaring it?

I don't know; I haven't tested that sort of thing, so I wouldn't know for sure. In any case, it makes more sense to me to declare the variables first, and then to use them - in case you ever need to convert what you have to assembly language :D .




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users