jeremydouglass / rosetta_examples_p5

Example set from Rosetta Code for P5 (Processing)
Other
10 stars 2 forks source link

[ADD EXAMPLE] Fractal tree #50

Closed villares closed 4 years ago

villares commented 4 years ago

I need some repo style guidance on this one I guess... This is a favourite theme for me so I have made 2 variations each both for Java and Python modes...

URL of task

http://rosettacode.org/wiki/Fractal_tree#Processing

Next steps

villares commented 4 years ago

May I name the files FractalTree_rotation.pde and FractalTree_calculation.pde? Should the variation info go into the header or in a comment inside the code? Please advise!

Rotation:

/**
 * FractalTree - Using rotation
 * https://rosettacode.org/wiki/Fractal_tree#Processing
 * Processing 3.4
 * 2020-03-18 Alexandre Villares
 * 
 * Task:
 * 
 * Generate and draw a fractal tree.
 * 
 * 1. Draw the trunk
 * 2. At the end of the trunk, split by some angle and draw two branches
 * 3. Repeat at the end of each branch until a sufficient level of branching is reached
 */

// Using Processing's pushMatrix(), popMatrix() and rotate()

void setup() {
  size(600, 600);
  background(0);
  stroke(255);
  drawTree(300, 550, 9);
}

void drawTree(float x, float y, int depth) {
  float forkAngle = radians(20);
  float baseLen = 10.0;
  if (depth > 0) {
    pushMatrix();
    translate(x, y - baseLen * depth);
    line(0, baseLen * depth, 0, 0);  
    rotate(forkAngle);
    drawTree(0, 0, depth - 1);  
    rotate(2 * -forkAngle);
    drawTree(0, 0, depth - 1); 
    popMatrix();
  }
}

Calculation version:

/**
 * FractalTree - Calculating coordinates
 * https://rosettacode.org/wiki/Fractal_tree#Processing
 * Processing 3.4
 * 2020-03-18 Alexandre Villares
 * 
 * Task:
 * 
 * Generate and draw a fractal tree.
 * 
 * 1. Draw the trunk
 * 2. At the end of the trunk, split by some angle and draw two branches
 * 3. Repeat at the end of each branch until a sufficient level of branching is reached
 */

// Calculating the coordinates of each branch

void setup() {
  size(600, 600);
  background(0);
  stroke(255);
  drawTree(300, 550, -90, 9);
}

void drawTree(float x1, float y1, float angle, int depth) {
  float forkAngle = 20;
  float baseLen = 10.0;
  if (depth > 0) {
    float x2 = x1 + cos(radians(angle)) * depth * baseLen;
    float y2 = y1 + sin(radians(angle)) * depth * baseLen;
    line(x1, y1, x2, y2);
    drawTree(x2, y2, angle - forkAngle, depth - 1);
    drawTree(x2, y2, angle + forkAngle, depth - 1);
  }
}
villares commented 4 years ago

Now the Python mode...

"""
FractalTree - Using rotation
https://rosettacode.org/wiki/Fractal_tree#Processing
Processing 3.4
2020-03-18 Alexandre Villares

Task:

Generate and draw a fractal tree.

1. Draw the trunk
2. At the end of the trunk, split by some angle and draw two branches
3. Repeat at the end of each branch until a sufficient level of branching is reached
"""

# Using Processing's pushMatrix(), popMatrix() and rotate()

def setup():
    size(600, 600)
    background(0)
    stroke(255)
    drawTree(300, 550, 9)

def drawTree(x, y, depth):
    fork_ang = radians(20)
    base_len = 10
    if depth > 0:
        pushMatrix()
        translate(x, y - baseLen * depth)
        line(0, baseLen * depth, 0, 0)  
        rotate(fork_ang)
        drawTree(0, 0, depth - 1)  
        rotate(2 * -fork_ang)
        drawTree(0, 0, depth - 1) 
        popMatrix()

And the other version:

"""
FractalTree - Calculating coordinates
https://rosettacode.org/wiki/Fractal_tree#Processing
Processing 3.4
2020-03-18 Alexandre Villares

Task:

Generate and draw a fractal tree.

1. Draw the trunk
2. At the end of the trunk, split by some angle and draw two branches
3. Repeat at the end of each branch until a sufficient level of branching is reached
"""
# Calculating the coordinates of each branch

def setup():
    size(600, 600)
    background(0)
    stroke(255)
    drawTree(300, 550, -90, 9)

def drawTree(x1, y1, angle, depth):
    fork_angle = 20
    base_len = 10.0
    if depth > 0:
        x2 = x1 + cos(radians(angle)) * depth * base_len
        y2 = y1 + sin(radians(angle)) * depth * base_len
        line(x1, y1, x2, y2)
        drawTree(x2, y2, angle - fork_angle, depth - 1)
        drawTree(x2, y2, angle + fork_angle, depth - 1)
jeremydouglass commented 4 years ago

May I name the files FractalTree_rotation.pde and FractalTree_calculation.pde? Should the variation info go into the header or in a comment inside the code? Please advise!

For .pde we are using a single underscore to separate the task in Java style camelcase -- so FractalTree -- from the approach in Java-style camel

ArchimedeanSpiral_Lines
ArchimedeanSpiral_LinesRotated
ArchimedeanSpiral_Points
ArchimedeanSpiral_PointsRotatedMatrix
ArchimedeanSpiral_PointsRotatedPVector

So:

FractalTree_Rotation.pde
FractalTree_Calculation.pde

...just the way you already did with: ArchimedeanSpiral_Points.pyde

The header is written with spaces and dashes

Archimedean Spiral -- Points

So, header title:

Fractal Tree -- Rotation

For URLs, it can be the base URL of #Processing on the page or something more specific.

https:#rosettacode.org/wiki/Archimedean_spiral#Processing

I can see that we need a style guide in CONTRIBUTING.md. And maybe mirror it to a wik page?

jeremydouglass commented 4 years ago

Closed by e369f5d , db61817 , 6fa0518 !

...and I'll use some of our chat here to start a CONTRIBUTING style guide