/* @pjs globalKeyEvents="true"; */ /* @pjs pauseOnBlur="true"; */ // Size of the ship float r = 12; boolean showarrows = true; boolean enteredB = false; // Draw the ship and other stuff void display() { translate(0,height); scale(1,-1); background(255); stroke(0); strokeWeight(2); fill(0); scale(1,-1); translate(0,-height); text("Press H to hide the arrows, press U to un-hide",10,10); translate(0,height); scale(1,-1); if (B != 0) { image(bfld,x_plate_right,0,372,520); } image(img,x-15,y,25,25); if ( key == 'h') { showarrows = false; } else if (key == 'u') { showarrows = true; } int tri_width=7; if (showarrows) { int x_line=10; int y_line=25; int line_len=100; line(x_line,y_line,x_line,y_line+line_len); line(x_line,y_line,x_line+line_len,y_line); triangle(x_line-tri_width/2,y_line+line_len,x_line+tri_width/2,y_line+line_len,x_line,y_line+line_len+10); triangle(x_line+line_len,y_line-tri_width/2,x_line+line_len,y_line+tri_width/2,x_line+line_len+10,y_line); scale(1,-1); translate(0,-height); text("+x",x_line+line_len+12,height-y_line); text("+y",x_line,height-(y_line+line_len+25)); translate(0,height); scale(1,-1); } // Draw velocity arrow float v_scaling=1.0; float x_center = x; float y_center = y + r; // Make sure the arrow starts from the center of the ship stroke(255,0,0); // makes the line red strokeWeight(3); // makes the line thicker if ( ((vx != 0) || (vy != 0)) && showarrows) { line(x_center,y_center,x_center+v_scaling*vx,y_center+v_scaling*vy); float vel_angle = -atan2(vy,vx); fill(255,0,0); // makes the triangle red triangle(x_center+v_scaling*vx+sin(vel_angle)*tri_width/2,y_center+v_scaling*vy+cos(vel_angle)*tri_width/2,x_center+v_scaling*vx-sin(vel_angle)*tri_width/2,y_center+v_scaling*vy-cos(vel_angle)*tri_width/2,x_center+v_scaling*vx+cos(vel_angle)*10,y_center+v_scaling*vy-sin(vel_angle)*10); } // Draw force arrow // float f_scaling=2.25; float f_scaling=3.0; float Fx = mass*deltaVx/dt; float Fy = mass*deltaVy/dt; float f_angle = -atan2(Fy,Fx); if (((Fx != 0) || (Fy != 0)) && showarrows) { // if (((Fx != 0) || (Fy != 0)) && 0 ) { stroke(51,102,0); // makes the line green line(x_center,y_center,x_center+f_scaling*Fx,y_center+f_scaling*Fy); fill(51,102,0); // makes the triangle green triangle(x_center+f_scaling*Fx+sin(f_angle)*tri_width/2,y_center+f_scaling*Fy+cos(f_angle)*tri_width/2,x_center+f_scaling*Fx-sin(f_angle)*tri_width/2,y_center+f_scaling*Fy-cos(f_angle)*tri_width/2,x_center+f_scaling*Fx+cos(f_angle)*10,y_center+f_scaling*Fy-sin(f_angle)*10); } // float a_scaling=f_scaling; float a_scaling=2.0; float a_angle = f_angle; float ax = deltaVx/dt; float ay = deltaVy/dt; if (((ax != 0) || (ay != 0)) && showarrows) { stroke(204,0,204); // makes the line purple line(x_center,y_center,x_center+a_scaling*ax,y_center+a_scaling*ay); fill(204,0,204); // makes the triangle purple triangle(x_center+a_scaling*ax+sin(f_angle)*tri_width/2,y_center+a_scaling*ay+cos(f_angle)*tri_width/2,x_center+a_scaling*ax-sin(f_angle)*tri_width/2,y_center+a_scaling*ay-cos(f_angle)*tri_width/2,x_center+a_scaling*ax+cos(f_angle)*10,y_center+a_scaling*ay-sin(f_angle)*10); } scale(1,-1); translate(0,-height); if (showarrows) { textSize(20); fill(255,0,0); text("Velocity",10,30); fill(51,102,0); text("Force",10,55); fill(0,0,255); text("Electric Field",10,80); fill(204,0,204); text("Acceleration",10,105); fill(34,177,76); text("Magnetic Field",10,130); } textSize(12); stroke(0,0,0); // black float y_opening=38; line(x_plate_left,0,x_plate_left,0.467*height-y_opening/2); line(x_plate_left,0.467*height+y_opening/2,x_plate_left,height); line(x_plate_right,0,x_plate_right,0.467*height-y_opening/2); line(x_plate_right,0.467*height+y_opening/2,x_plate_right,height); int Narrows=8; for (int i = 1; i<=Narrows; i+=1) { fill(0,0,255); stroke(0,0,255); float y_arrow = height*i/Narrows-43; line(x_plate_left+20,y_arrow,x_plate_right-20,y_arrow); if (E < 0) { triangle(x_plate_left+20,y_arrow-tri_width/2,x_plate_left+20,y_arrow+tri_width/2,x_plate_left+20-10,y_arrow); } if (E > 0 ) { triangle(x_plate_right-20,y_arrow-tri_width/2,x_plate_right-20,y_arrow+tri_width/2,x_plate_right-20+10,y_arrow); } } fill(0,0,0); //If more text is written elsewhere make sure the default is black stroke(0,0,0); // If more lines are drawn elsewhere make sure the default is black // Position info text("Initial x = ",10,175); text(xinitial,100,175); text("Initial y = ",10,200); text(yinitial,100,200); if ( x > x_plate_right ) { enteredB = true; } // Stop if we leave window, or curve and touch plate if ((x > width) || ( ( x < x_plate_right) & enteredB ) ){ text("Final Velocity x = ",width/2-125,0.467*height); text(vx,width/2+30,0.467*height); text("Final Velocity y = ",width/2-125,0.467*height+24); text(vy,width/2+30,0.467*height+24); text("Final x = ",10,300); text(x,100,300); text("Final y = ",10,325); text(y,100,325); noLoop(); } }