We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › making ellipse assume the color from image
Page Index Toggle Pages: 1
making ellipse assume the color from image (Read 521 times)
making ellipse assume the color from image
Jun 8th, 2009, 7:14am
 
hey! im trying to  make an ellipse assume the color from an  image on its x and y position. the x and y are random and the get() does'nt accept floats so i tried to increment an int of 0 with a float but i don't seem to be getting anywhere... can someone help me?

here's my code Code:
import processing.pdf.*;

int x = 0;
int y = 0;
void setup(){
size(595,842);
beginRecord(PDF, "cartaz.pdf");
smooth();
background(0);
PImage myImage = loadImage("Imagem.jpg");
image(myImage, 0, 0);


}

void draw(){


stroke(10);

float xpos = random(width);
float ypos = random(height);
color pixelColor = get(x,y);
fill(pixelColor);
float prop = random(20);

ellipse(xpos,ypos,prop,prop);

x += xpos;
y += ypos;
println(xpos);

if(mousePressed){
endRecord();
exit();
}
}
Re: making ellipse assume the color from image
Reply #1 - Jun 8th, 2009, 7:58am
 
You are incrementing x and y at random values. Eventually you will go out of bounds of the image.

Try this (psuedo code) where img is your image variable

int x = random(img.width,img.height)
int y = random(img.width,img.height)
color col = img.get(x,y)

fill(col)

ellipse(x,y,2,2)

Re: making ellipse assume the color from image
Reply #2 - Jun 8th, 2009, 8:04am
 
its not possible because int cant support random, only float can..
Re: making ellipse assume the color from image
Reply #3 - Jun 8th, 2009, 8:06am
 
Look at float() and int() functions to convert between these two types.
Re: making ellipse assume the color from image
Reply #4 - Jun 8th, 2009, 10:30am
 
Here we go, I was in a bit of a rush earlier so it was slightly incorrect

as PhiLho mentioned, you need to cast the return value of random to an int with int()

Code:
PImage img;

void setup()
{
size(500,500);
img = loadImage("flower.jpg");
noStroke();
}

void draw()
{
for(int i = 0; i < 100; i++)
{
int x = (int)random(img.width);
int y = (int)random(img.height);
color col = img.get(x,y);
fill(col);
ellipse(x,y,2,2);
}
}


Re: making ellipse assume the color from image
Reply #5 - Jun 8th, 2009, 1:06pm
 
thanksss!
Page Index Toggle Pages: 1