We are about to switch to a new forum software. Until then we have removed the registration on this forum.
float radius = (min(width, height)/2)*0.22;
// it's supposed to determine what the length of the hand is
void setup(){
size(480,480);
stroke(55);
strokeWeight (4);
}
//run-of-the-mill setup
void draw(){
background (255);
stroke(0);
float time = millis()/10;
float sec= map(time,0,1000,0,TWO_PI)-HALF_PI;
float cx = width/2;
float cy = height/2;
float time2 = millis();
float direction1 = cx*cos(sec)*radius;
float direction2 = cy*sin(sec)*radius;
//variables, cx cy to center the origin
if(time2%1000==0){
direction1 *=-1;
direction2 *=-1;
}
// i may have gotten my trigonometry completely wrong,
// but this is SUPPOSED to reverse the direction of the
// moving line every 1 second, but nothing happens.
// What do I do wrong?
line(cx, cy, direction1 , direction2);
}
So what's supposed to happen is that every one second the direction of the moving hand reverses, but nothing happens except for a smoothly running hand going across the screen. What did I do wrong?
Additionally: how do I limit the length of my hand? I think I set it too long but I'm not sure why/how...
Answers
==0
in lineif(time2%1000==0){
won‘t workit’s running too slow to meet exactly 1000 every time
Instead store oldTime :
oldTime = millis();
Then compare to current time and check with
millis()
:float radius = (min(width, height)/2)*0.22;
width and height are 100 before size() is called. so this will not be what you are expecting.
To understand why, try these test sketches:
a few errors:
Error on radius
you were using
width
andheight
beforesize
in definingradius
in line 1. It's not defined beforesize()
command though. So radius was always wrong.Error on center
here
you had an obvious typo, center must be added not multiplied:
Time and angle
Also I found it hard to use time and correct angle both.
So I decided to strip the sketch down and use the angle (named time) as a turning point on both sides left and right.
I was adjusting the speed (I was adding a value to the angle, called time in my sketch) so that it came out at one second as a turning point.
I fail the math to dive deeper into it. In theory, this
was very good.
Best, Chrisir ;-)
here is the entire code
ah, I solved it!
Here is a better version, that works not with adding something to the angle but with a proper timer.
The times are also measured, they vary between 1000 and 1016 millis on windows system but are almost accurate.
The times are also measured, they vary between 1000 and 1016 millis on windows system but are almost accurate.
After extensive study of your code, I managed to solve the problem. Thanks for helping me!