We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello 2gether,
i want to generate objects (ellipse) on a typo of an Image (pic.png).
Sadly, the code doesnt work and the objects on the entire screen.
Thanks for HELP!!!!
Heres the Code:
ArrayList <PVector> circles = new ArrayList <PVector> ();
float diameter;
PImage typo;
void setup() {
background(255);
size(500, 500);
colorMode(RGB, 100, 100, 100);
noStroke();
smooth();
typo = loadImage("pic.png");
}
void draw() {
addCircle();
for (int i=0; i<circles.size(); i++) {
PVector p = circles.get(i);
fill(i % 360, 100, 100);
ellipse(p.x, p.y, p.z, p.z);
}
}
void addCircle() {
diameter=random(10, 40);
PVector c = randomVector();
int tries = 1000000;
color pixel;
pixel = typo.get((int)c.x, (int)c.y);
while (overlap(c) && tries > 0) {
c = randomVector();
tries--;
}
if (!overlap(c) && brightness(pixel)>0) {
circles.add(c);
} else {
addCircle();
}
}
PVector randomVector() {
return new PVector(random(width), random(height), diameter);
}
boolean overlap(PVector c) {
for (PVector p : circles) {
if ((dist(c.x, c.y, p.x, p.y) < (c.z + p.z)*0.5)) {
return true;
}
}
return false;
}
Answers
What doesn't work about it? You are generating circles that cover the majority of the screen... Oh, but you can't see them because they get white very fast, and you are drawing white circles on a white background.
Try changing line 19 to this:
And you will see all your circles.
No, i want to generate a typo like this
But the circles on the complete Screen.
Those are two incomplete sentences... I can't tell what is not working, what the program should be doing. Provide more details about your input image and what the algorithm suppose to do with your image. Also, consider sharing your PNG file.
I think things could be more clear if you explain more about this:
I think you meant to say that image contains text and you want to generate random circles on top of the text (?)
Kf
Yes, youre right. Sorry for my bad explaination. I have an image and i want to create random circles on the top of the letters. But this Code:
if (!overlap(c) && brightness(pixel)>0) {
doesnt work and the circles over the complete screen. The letters are black and the background white (RGB).
Currently you are adding a new circle if it doesnt overlap. Surely you meant if it does...?
In this next sketch, you will need to load your text graphics into the pg object within the draw() function or in setup. The text must be white in black background. If your text is black on white background, you need to apply an invert filter. In the demo below, click on the image to toggle the masking effect on and off.
When you toggle between masking action, you will see that when the mask is not being applied, there is an edge effect. In your case, it should not matter as you want the mask active. I will be opening another post to address that issue.
Kf
}