1 Star 0 Fork 15

卢星宇/python_turtle

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Pendulum.pde 2.87 KB
一键复制 编辑 原始数据 按行查看 历史
布树辉 提交于 2022-02-25 13:09 +08:00 . Rearrange dirs
// 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
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/poised/python_turtle.git
git@gitee.com:poised/python_turtle.git
poised
python_turtle
python_turtle
master

搜索帮助