//Define Global Variables
float x, y; //updating coordinates
float a = 0; //find theta angle
float r = 200; //radius
void setup(){
size(640,480);
}
void draw(){
background(#6E96A7); //bluish background
drawCoordinateAxes();
updateServoHornPosition();
drawServoHorn();
}
void drawCoordinateAxes(){
stroke(0);
translate(width/2,height/2);
line(0,height,0,-height);
line(-width,0,width,0);
fill(0,255,0);
text("-X",width/-2, 15);
fill(0,255,0);
text("X",width/2.1,15);//2.1 to keep on edge
fill(255,0,0);
text("Y", 15, height/-2.1);
fill(255,0,0);
text("-Y", 15, height/2.1);
}
void updateServoHornPosition(){
float angle = radians(a); //convert to radians
x = r*cos(angle);
y = r*sin(angle);
print("x=",x);//print x coordinates
println();//next line
print("y=",y);//print y coordinates
println();//add line
a = a - 1; //update theta
if(a == -180) //range (theta = (0-180] degrees)
{
a = a +1;
}
}
void drawServoHorn()
{
strokeWeight(3);
stroke(#F50C0C);
line(0,0,x,y);
}
1 Like
a = a - 1; //update theta
if(a == -180) //range (theta = (0-180] degrees)
{ a = a +1; }
you are printing x and y
but not a ( outside and inside the IF )
then you might understand the problem
change to thinking of direction:
if a > 180 dir = -
if a < -180 dir = +
a +=dir
what would be the correct syntax for this?
try like
//Define Global Variables
float x, y; //updating coordinates
float dir = 1,a = 0; //find direction and theta angle
float r = 200; //radius
boolean diagp = true;
void setup() {
size(640, 480);
}
void draw() {
background(#6E96A7); //bluish background
drawCoordinateAxes();
updateServoHornPosition();
drawServoHorn();
}
void drawCoordinateAxes() {
stroke(0);
translate(width/2, height/2);
line(0, height, 0, -height);
line(-width, 0, width, 0);
fill(0, 255, 0);
text("-X", width/-2, 15);
fill(0, 255, 0);
text("X", width/2.1, 15);//2.1 to keep on edge
fill(255, 0, 0);
text("Y", 15, height/-2.1);
fill(255, 0, 0);
text("-Y", 15, height/2.1);
}
void updateServoHornPosition() {
x = r*cos(radians(a));
y = r*sin(radians(a));
if ( a > 0 ) dir = -1;
if ( a < -180) dir = 1;
a += dir; //update theta
if ( diagp ) println("x "+x+" y "+ y+" a "+a);
}
void drawServoHorn() {
strokeWeight(3);
stroke(#F50C0C);
line(0, 0, x, y);
}
2 Likes