We are about to switch to a new forum software. Until then we have removed the registration on this forum.
the code works perfectly the way i want, im just curious as to why when i initialise the size value in the form of float the code works, but it dosnt work as int size; can someone explain why? just sheer curiosity for the future.
(edit was just to make the text fit better)
int y = 100;
int speed = 3;
float size = 50;
void setup(){
size(500,500);
stroke(0);
fill(175);
}
void draw(){
background(255);
y+= speed;
if ((y>500) || (y<0)) {
speed = speed * -1;
}
ellipse(y, y,size, size);
}
void mouseReleased(){
size = random(20,60);
redraw();
}
Answers
random() returns a float. you need to cast it to an int if you need it to be an int
(best not to use variables with the same name as processing reservered words)
float
value: https://Processing.org/reference/random_.htmlint
, can't store it.(int)
:(int) random(20, 60);
.round(random(20, 60));
.is it better to use int or float? or does it depend on the situation? i understand float is increments (technically i know its just a refresh not actually decimals as 2.5 or 2.7 etc dosnt exist)
thanks very much, what does downcast mean?
Whole datatypes, like
byte
,short
,char
,int
&long
, are slightly faster.And they are safer, b/c they're free of rounding errors.
why use float then? would int or its counterparts not be better in every situation or is there certain other commands that require float?
Java automatically coerces a simpler number type to a complex 1 whenever needed. ~O)
That's called automatic upcasting number type promotion. :-B
Thank you very much :) very informative and understandable thank you ^:)^
There are many. Consider
arc()
. It takes an argument of how many radians around the circle you want it to start and stop.That means if you want a quarter of a circle, you need half of PI.
You could also approximate this with 3.1415/2:
...so that is just one example of where you need floats -- circles and angles.
just don't indent your text and it'll show it like text. if you indent it it'll treat it as code and not wrap it.
use a blank line between paragraphs.
If you check the markdown examples, you will understand why posting in the forum behaves the way it does: https://en.wikipedia.org/wiki/Markdown
Notice this link is available right below any comment box: You can use Markdown in your post.
Kf