代码拉取完成,页面将自动刷新
同步操作将从 飞行器智能感知与控制/python_turtle 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Pendulum
// A Simple Pendulum Class
// Includes functionality for user can click and drag the pendulum
class Pendulum {
PVector position; // position of pendulum ball
PVector origin; // position of arm origin
float r; // Length of arm
float angle; // Pendulum arm angle
float aVelocity; // Angle velocity
float aAcceleration; // Angle acceleration
float ballr; // Ball radius
float damping; // Arbitary damping amount
boolean dragging = false;
// This constructor could be improved to allow a greater variety of pendulums
Pendulum(PVector origin_, float r_) {
// Fill all variables
origin = origin_.get();
position = new PVector();
r = r_;
angle = PI/4;
aVelocity = 0.0;
aAcceleration = 0.0;
damping = 0.995; // Arbitrary damping
ballr = 48.0; // Arbitrary ball radius
}
void go() {
update();
drag(); //for user interaction
display();
}
// Function to update position
void update() {
// As long as we aren't dragging the pendulum, let it swing!
if (!dragging) {
float gravity = 0.4; // Arbitrary constant
aAcceleration = (-1 * gravity / r) * sin(angle); // Calculate acceleration (see: http://www.myphysicslab.com/pendulum1.html)
aVelocity += aAcceleration; // Increment velocity
aVelocity *= damping; // Arbitrary damping
angle += aVelocity; // Increment angle
}
}
void display() {
position.set(r*sin(angle), r*cos(angle), 0); // Polar to cartesian conversion
position.add(origin); // Make sure the position is relative to the pendulum's origin
stroke(0);
strokeWeight(2);
// Draw the arm
line(origin.x, origin.y, position.x, position.y);
ellipseMode(CENTER);
fill(175);
if (dragging) fill(0);
// Draw the ball
ellipse(position.x, position.y, ballr, ballr);
}
// The methods below are for mouse interaction
// This checks to see if we clicked on the pendulum ball
void clicked(int mx, int my) {
float d = dist(mx, my, position.x, position.y);
if (d < ballr) {
dragging = true;
}
}
// This tells us we are not longer clicking on the ball
void stopDragging() {
aVelocity = 0; // No velocity once you let go
dragging = false;
}
void drag() {
// If we are draging the ball, we calculate the angle between the
// pendulum origin and mouse position
// we assign that angle to the pendulum
if (dragging) {
PVector diff = PVector.sub(origin, new PVector(mouseX, mouseY)); // Difference between 2 points
angle = atan2(-1*diff.y, diff.x) - radians(90); // Angle relative to vertical axis
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。