Rosetta Examples development

Thank you so much, @noel. The Floodfill looks great!

To fix the import issue on openprocessing, I think we could replace java.awt.Point with PVector. Could you try that? We might also need to / want to change the linkedlist / deque into ArrayList, I’m not sure.

1 Like

That’s already done. @GoToLoop made an excellent class. but unfortunately, it’s still not running on OpenProcessing. See here.

2 Likes

Wow, this looks great fun. I am looking at all the unfinished topics looking for one to tackle. The Rosetta project shows the aesthetic beauty of code ipso facto. Sometimes the code is more beautiful than the output and too often hidden.

3 Likes

I would like to play too! :smiley:

from collections import deque

tolerance = 15
allowed = False

def setup():
    global img, fill_color
    size(600, 400)
    img = loadImage("image.png")
    fill_color = color(250, 0, 0)
    fill(0, 0, 100)

    image(img, 0, 0, width, height)
    textSize(18)
    text("Tolerance = {}    (Use mouse wheel to change)".format(tolerance),
         100, height - 30)
    text("Right click to reset", 100, height - 10)

def draw():
    global allowed
    if allowed:
        image(img, 0, 0, width, height)
        text("Tolerance = {}    (Use mouse wheel to change)".format(tolerance), 100, height - 30)
        text("Right click to reset", 100, height - 10)
        allowed = False

def mousePressed():
    global img, allowed
    img.loadPixels()
    flood(mouseX, mouseY)
    img.updatePixels()
    allowed = True
    if mouseButton == RIGHT:
        img = loadImage("image.png")

def mouseWheel(event):
    global allowed, tolerance
    e = event.getCount()
    tolerance += 2 * e
    if tolerance > 256:
        tolerance = 256
    if tolerance < 0:
        tolerance = 0
    allowed = True

def flood(x, y):
    target_color = img.pixels[pixel_position(mouseX, mouseY)]
    queue = deque()
    queue.append((x, y))
    while len(queue) > 0:
        p_x, p_y = queue.popleft()
        if (check(p_x, p_y, target_color)):
            queue.append((p_x, p_y - 1))
            queue.append((p_x, p_y + 1))
            queue.append((p_x - 1, p_y))
            queue.append((p_x + 1, p_y))

def pixel_position(x, y):
    return x + (y * img.width)

def check(x, y, target_color):
    if x < 0 or y < 0 or y >= img.height or x >= img.width:
        return False
    pp = img.pixels[pixel_position(x, y)]
    test_tolerance = (abs(green(target_color) - green(pp)) < tolerance
                      and abs(red(target_color) - red(pp)) < tolerance
                      and abs(blue(target_color) - blue(pp)) < tolerance)
    if not test_tolerance:
        return False
    img.pixels[pixel_position(x, y)] = fill_color
    return True
2 Likes

@noel @villares me too. See on rosetta code, I used suggested image, and this is the result:-
flood_fill

2 Likes

I’ve added a link to play the Bitmap/Floodfill task here. Unfortunately, if the Device Pixel Aspect Ratio of the monitor is not 1.00, the sketch will probably not run properly. This happens for instance when you chose a different text percentage (like 125%) in your “windows appearance” settings. You will have to zoom in/out on your browser to adjust this. This aspect ratio you can discover on this site. If anyone knows a solution for this I will be grateful.

1 Like

I’ve added the Maze Generation task. Here is a link to the animated version

1 Like

It’s Brillant Maze Generation! Thank you very much!
I have just ported it to Python mode: http://www.rosettacode.org/wiki/Maze_generation#Processing_Python_mode

I am optimizing the variable names and adding comments for better understanding Also I am taking out some leftover garbage. So maybe you want to check this later on.

1 Like

OK! Count on me, I’ll have a look.

@jeremydouglass @villares Were you aware of processing ‘plasma effect’ example? I have ported it to JRubyArt Plasma effect - Rosetta Code. I have also ported colored bars task.

1 Like

Cool! I’ll try my hand at porting :slight_smile:

@noel, @villares I’ve just posted a somewhat object orientated version of the Sierpinski Pentagon on rosetta code. Possibly another task to try?

2 Likes

@paulstgeorge You still want to join us? This is a nice one?
Because this is the message I got from discourse:

Let others join the conversation

This topic is clearly important to you – you’ve posted more than 28% of the replies here.

It could be even better if you gave other people space to share their points of view, too. Can you invite them over?

1 Like

@Noel The churlish bot has made a mistake. Its logic only works if there is a fixed amount of total space. Only then could you give others space by contributing less.

Anyway, I have looked again and now I know the task I want to tackle: https://www.rosettacode.org/wiki/Combinations.

I assume it is cheating (and boring) to import a library that does the task.

2 Likes

@noel @jeremydouglass @villares The Sierpinksi arrow head curve is missing for processing and python. I’ve just published the JRubyArt version. For the benefit of @villares heres a sierpinski lsystem in processing.py I did earlier.

4 Likes

My second sketch in P5.js. Somewhat concise?
But now I remember; it has to be in P5.java isn’t it?

const s = {x: 20, y: 30, angle: 60};

function setup() {
  createCanvas(450, 400);
  background(0, 0, 200);
  angleMode(DEGREES);
  stroke(255);
  sc(7, 400, -60);
}

function sc(o, l, angle) {
  if (o == 0) {
    const {x, y, angle} = s;
    s.x += cos(angle) * l;
    s.y += sin(angle) * l;
    line(x, y, s.x, s.y);
  } else {
    sc(o-1, l/2, -angle);
    s.angle += angle;
    sc(o-1, l/2, angle);
    s.angle += angle;
    sc(o-1, l/2, -angle);
  }
}

sp

2 Likes

Did you succeed?..

Your Sierpinski p5.js version w/ less lines: :upside_down_face:

"use strict";

const t = { x: 20, y: 30, a: 60 };

function setup() {
  createCanvas(450, 400);
  background(0, 0, 200).stroke(255).angleMode(DEGREES);
  sc(7, 400, -60);
}

function sc(o, l, a, s = t) {
  if (o) {
    sc(--o, l *= .5, -a).a += a;
    sc(o, l, a).a += a;
    sc(o, l, -a);
  } else line(s.x, s.y, s.x += cos(s.a) * l, s.y += sin(s.a) * l);
  return s;
}
2 Likes

I guess no one is going to beat that. :grinning:
Maybe just removing"use strict"
It isn’t strict necessary is it?
Googled a bit, and it’s said that it’s good to get rid of many “silent” errors that can occur .
Is that it?