This site requires JavaScript, please enable it in your browser! # pythagoras tree

public class Pythagoras2 extends Actor { /** * Act - do whatever the Pythagoras2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ final int depth = 7; public Pythagoras2() { GreenfootImage gfim = new GreenfootImage(900,600); drawTree(gfim, 275, 500, 375, 500, 0); setImage(gfim); } private void drawTree(GreenfootImage gfim, float x1, float y1, float x2, float y2, int depth) { if (depth == 0) return; float dx = x2 - x1; float dy = y1 - y2; float x3 = x2 - dy; float y3 = y2 - dx; float x4 = x1 - dy; float y4 = y1 - dx; float x5 = x4 + 0.5F * (dx - dy); float y5 = y4 - 0.5F * (dx + dy); gfim.setColor(Color.BLUE); gfim.drawLine ((int)x1, (int)y1, (int)x2, (int)y2); gfim.drawLine ((int)x2, (int)y2, (int)x4, (int)y4); gfim.drawLine ((int)x4, (int)y4, (int)x3, (int)y3); gfim.drawLine ((int)x1, (int)y1, (int)x3, (int)y3); gfim.setColor(Color.GREEN); gfim.fillPolygon(new int {(int)x1,(int)x2,(int)x4,(int)x3}, new int {(int)y1,(int)y2,(int)y4,(int)y3}, 4); drawTree(gfim, x4, y4, x5, y5, depth + 1); drawTree(gfim, x5, y5, x3, y3, depth + 1); } } I started to make a Pythagorean tree from various source codes, it does not work but does not indicate any error, I think I'm on the right track, I hope I did not do anything Thank you for your help
Please use code tags for your code (see 'Posting code? read this!!" below the reply box. Then, try to explain what you thing each line (or set of lines) does in your code.
```import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Pythagoras2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Pythagoras2 extends Actor
{
/**
* Act - do whatever the Pythagoras2 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
final int depth = 7;

public Pythagoras2()
{
GreenfootImage gfim = new GreenfootImage(900,600);
drawTree(gfim, 275, 500, 375, 500, 0);
setImage(gfim);
}

private void drawTree(GreenfootImage gfim, float x1, float y1, float x2, float y2,
int depth)
{

if (depth == 0)
return;

float dx = x2 - x1;
float dy = y1 - y2;

float x3 = x2 - dy;
float y3 = y2 - dx;
float x4 = x1 - dy;
float y4 = y1 - dx;

float x5 = x4 + 0.5F * (dx - dy);
float y5 = y4 - 0.5F * (dx + dy);

gfim.setColor(Color.BLUE);
gfim.drawLine ((int)x1, (int)y1, (int)x2, (int)y2);
gfim.drawLine ((int)x2, (int)y2, (int)x4, (int)y4);
gfim.drawLine ((int)x4, (int)y4, (int)x3, (int)y3);
gfim.drawLine ((int)x1, (int)y1, (int)x3, (int)y3);

gfim.setColor(Color.GREEN);
gfim.fillPolygon(new int[] {(int)x1,(int)x2,(int)x4,(int)x3},
new int[] {(int)y1,(int)y2,(int)y4,(int)y3}, 4);

drawTree(gfim, x4, y4, x5, y5, depth + 1);
drawTree(gfim, x5, y5, x3, y3, depth + 1);

}

}
```
first of all line 9 = pythagoras2 is the superclass that inherits the actor class line 15 = int final variable that has an instance line 18 to 22 = constructor with class name gfim is the subject of greenfootimage drawTree with parameters x1, y1, x2, y2 setimage is a bit like setVisible line 25-26 = method with parameters line 29-30 = loop if equal 0 then return line 23 to 41 = variable with instance I guess dx means direction x line 45 sets the color line 46 to 49 = draw the lines x1, y1, etc which may form a square line 52 defines the color line 53-54 fills the polygon polygon with 4 parameters, I think I was wrong, I will have to put 3 parameters, which must form a triangle line 58-59 draws a tree with parameters that are shown in the class pythagoras2 if I'm not mistaken
You were not wrong -- a Pythagorean tree is made up of squares. You calculations are for a square that is oriented like a square (laying flat). They will not work on a square that is standing on a corner (one that looks more like a diamond). So, the second iteration will produce erroneous results which totally ruins any future iteration.
if I understand you correctly, it's not going to work because the pythagoras tree is made of squares and even with drawRect or drawPolygon to form a square it will not work.
ronald wrote...
if I understand you correctly, it's not going to work because the pythagoras tree is made of squares and even with drawRect or drawPolygon to form a square it will not work.
No. It will work if done correctly. Lines 40 and 41 have incorrect coordinates going into drawTree. on lines 58 and 59.
excuse me I misunderstood
I know what lines 40 and 41 are supposed to do; but, they aren't doing it. The 4 coordinates given the method via parameter arguments are for the endpoints of a line segment that makes the base of a new square. Lines 40 and 41 are supposed to compute the coordinates of a point that is one and one-half the length of the line segment "above" the midpoint of the segment. You need two points to get a midpoint; yet, those lines of code only utilize one point (y4). Also, you have dx and dy directly interacting with each other in those lines. That can not be good for anything except for maybe testing their relationship in an if conditional, but not in an assignment statement.
Thank you, I found the solution
You need to login to post a reply.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6314666,"math_prob":0.98682773,"size":1307,"snap":"2019-35-2019-39","text_gpt3_token_len":478,"char_repetition_ratio":0.16039908,"word_repetition_ratio":0.0,"special_character_ratio":0.39020658,"punctuation_ratio":0.25320512,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.983105,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-18T13:25:03Z\",\"WARC-Record-ID\":\"<urn:uuid:9d5cb367-a728-4ef0-831d-c74c1e79bb85>\",\"Content-Length\":\"21031\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d8334c53-24b2-4a79-b6d1-220be86989f6>\",\"WARC-Concurrent-To\":\"<urn:uuid:45a37568-efc4-4c58-b429-c1d047ee75d6>\",\"WARC-IP-Address\":\"137.73.113.104\",\"WARC-Target-URI\":\"https://www.greenfoot.org/topics/62581/0\",\"WARC-Payload-Digest\":\"sha1:BC3INMME5WXCKCAUQ5YEJFZTBQC7HER5\",\"WARC-Block-Digest\":\"sha1:MQFJIQKZH2TCBWSFSC4I33RPAU4YBXPW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514573289.83_warc_CC-MAIN-20190918131429-20190918153429-00359.warc.gz\"}"}