2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -14,7 +14,7 @@ do
|
|||
exit
|
||||
end if
|
||||
end do
|
||||
call system('clear')
|
||||
call execute_command_line('cls')
|
||||
|
||||
c_ang = s_ang*pi/180.0D0
|
||||
p_ang = c_ang
|
||||
|
|
@ -23,7 +23,7 @@ call display(c_ang)
|
|||
do
|
||||
call next_time_step(c_ang,p_ang,g,l,dt,n_ang)
|
||||
if(abs(c_ang-p_ang).ge.0.05D0) then
|
||||
call system('clear')
|
||||
call execute_command_line('cls')
|
||||
call display(c_ang)
|
||||
end if
|
||||
end do
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ VEL =: 0 NB. ms_1
|
|||
|
||||
PEND=: noun define
|
||||
pc pend;pn "Pendulum";
|
||||
minwh 320 200; cc isi isigraph type flush;
|
||||
minwh 320 200; cc isi isigraph flush;
|
||||
)
|
||||
|
||||
pend_run=: verb define
|
||||
|
|
|
|||
62
Task/Animate-a-pendulum/JavaScript/animate-a-pendulum-2.js
Normal file
62
Task/Animate-a-pendulum/JavaScript/animate-a-pendulum-2.js
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Swinging Pendulum Simulation</title>
|
||||
</head>
|
||||
<body><center>
|
||||
<svg id="scene" height="200" width="300">
|
||||
<line id="string" x1="150" y1="50" x2="250" y2="50" stroke="brown" stroke-width="4" />
|
||||
<circle id="ball" cx="250" cy="50" r="20" fill="black" />
|
||||
</svg>
|
||||
<br>
|
||||
Initial angle:<input id="in_angle" type="number" min="0" max="180" onchange="condReset()"/>(degrees)
|
||||
<br>
|
||||
<button type="button" onclick="startAnimation()">Start</button>
|
||||
<button type="button" onclick="stopAnimation()">Stop</button>
|
||||
<button type="button" onclick="reset()">Reset</button>
|
||||
<script>
|
||||
in_angle.value = 0;
|
||||
var cx = 150, cy = 50;
|
||||
var radius = 100; // cm
|
||||
var g = 981; // cm/s^2
|
||||
var angle = 0; // radians
|
||||
var vel = 0; // cm/s
|
||||
var dx = 0.02; // s
|
||||
var acc, vel, penx, peny;
|
||||
var timerFunction = null;
|
||||
function stopAnimation() {
|
||||
if(timerFunction != null){
|
||||
clearInterval(timerFunction);
|
||||
timerFunction = null;
|
||||
}
|
||||
}
|
||||
function startAnimation() {
|
||||
if(!timerFunction) timerFunction = setInterval(swing, dx * 1000);
|
||||
}
|
||||
function swing(){
|
||||
acc = g * Math.cos(angle) * dx;
|
||||
vel += acc * dx;
|
||||
angle += vel * dx;
|
||||
setPenPos();
|
||||
}
|
||||
function setPenPos(){
|
||||
penx = cx + radius * Math.cos(angle);
|
||||
peny = cy + radius * Math.sin(angle);
|
||||
scene.getElementById("string").setAttribute("x2", penx);
|
||||
scene.getElementById("string").setAttribute("y2", peny);
|
||||
scene.getElementById("ball").setAttribute("cx", penx);
|
||||
scene.getElementById("ball").setAttribute("cy", peny);
|
||||
}
|
||||
function reset(){
|
||||
var val = parseInt(in_angle.value)*0.0174532925199;
|
||||
if (val) angle = val;
|
||||
else angle = 0;
|
||||
acc = 0;
|
||||
vel = 0;
|
||||
setPenPos();
|
||||
}
|
||||
function condReset(){
|
||||
if (!timerFunction) reset();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,5 +1,3 @@
|
|||
package pendulum
|
||||
|
||||
import java.awt.*
|
||||
import java.util.concurrent.*
|
||||
import javax.swing.*
|
||||
|
|
@ -15,14 +13,16 @@ class Pendulum(private val length: Int) : JPanel(), Runnable {
|
|||
}
|
||||
|
||||
override fun paint(g: Graphics) {
|
||||
g.color = Color.WHITE
|
||||
g.fillRect(0, 0, width, height)
|
||||
g.color = Color.BLACK
|
||||
val anchor = Element(width / 2, height / 4)
|
||||
val ball = Element((anchor.x + Math.sin(angle) * length).toInt(), (anchor.y + Math.cos(angle) * length).toInt())
|
||||
g.drawLine(anchor.x, anchor.y, ball.x, ball.y)
|
||||
g.fillOval(anchor.x - 3, anchor.y - 4, 7, 7)
|
||||
g.fillOval(ball.x - 7, ball.y - 7, 14, 14)
|
||||
with(g) {
|
||||
color = Color.WHITE
|
||||
fillRect(0, 0, width, height)
|
||||
color = Color.BLACK
|
||||
val anchor = Element(width / 2, height / 4)
|
||||
val ball = Element((anchor.x + Math.sin(angle) * length).toInt(), (anchor.y + Math.cos(angle) * length).toInt())
|
||||
drawLine(anchor.x, anchor.y, ball.x, ball.y)
|
||||
fillOval(anchor.x - 3, anchor.y - 4, 7, 7)
|
||||
fillOval(ball.x - 7, ball.y - 7, 14, 14)
|
||||
}
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
|
|
|
|||
104
Task/Animate-a-pendulum/Ruby/animate-a-pendulum-3.rb
Normal file
104
Task/Animate-a-pendulum/Ruby/animate-a-pendulum-3.rb
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
#!/bin/ruby
|
||||
|
||||
begin; require 'rubygems'; rescue; end
|
||||
|
||||
require 'gosu'
|
||||
include Gosu
|
||||
|
||||
# Screen size
|
||||
W = 640
|
||||
H = 480
|
||||
|
||||
# Full-screen mode
|
||||
FS = false
|
||||
|
||||
# Screen update rate (Hz)
|
||||
FPS = 60
|
||||
|
||||
class Pendulum
|
||||
|
||||
attr_accessor :theta, :friction
|
||||
|
||||
def initialize( win, x, y, length, radius, bob = true, friction = false)
|
||||
@win = win
|
||||
@centerX = x
|
||||
@centerY = y
|
||||
@length = length
|
||||
@radius = radius
|
||||
@bob = bob
|
||||
@friction = friction
|
||||
|
||||
@theta = 60.0
|
||||
@omega = 0.0
|
||||
@scale = 2.0 / FPS
|
||||
end
|
||||
|
||||
def draw
|
||||
@win.translate(@centerX, @centerY) {
|
||||
@win.rotate(@theta) {
|
||||
@win.draw_quad(-1, 0, 0x3F_FF_FF_FF, 1, 0, 0x3F_FF_FF_00, 1, @length, 0x3F_FF_FF_00, -1, @length, 0x3F_FF_FF_FF )
|
||||
if @bob
|
||||
@win.translate(0, @length) {
|
||||
@win.draw_quad(0, -@radius, Color::RED, @radius, 0, Color::BLUE, 0, @radius, Color::WHITE, -@radius, 0, Color::BLUE )
|
||||
}
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def update
|
||||
# Thanks to Hugo Elias for the formula (and explanation thereof)
|
||||
@theta += @omega
|
||||
@omega = @omega - (Math.sin(@theta * Math::PI / 180) / (@length * @scale))
|
||||
@theta *= 0.999 if @friction
|
||||
end
|
||||
|
||||
end # Pendulum class
|
||||
|
||||
class GfxWindow < Window
|
||||
|
||||
def initialize
|
||||
# Initialize the base class
|
||||
super W, H, FS, 1.0 / FPS * 1000
|
||||
# self.caption = "You're getting sleeeeepy..."
|
||||
self.caption = "Ruby/Gosu Pendulum Simulator (Space toggles friction)"
|
||||
|
||||
@n = 1 # Try changing this number!
|
||||
@pendulums = []
|
||||
(1..@n).each do |i|
|
||||
@pendulums.push Pendulum.new( self, W / 2, H / 10, H * 0.75 * (i / @n.to_f), H / 60 )
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def draw
|
||||
@pendulums.each { |pen| pen.draw }
|
||||
end
|
||||
|
||||
def update
|
||||
@pendulums.each { |pen| pen.update }
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
if id == KbSpace
|
||||
@pendulums.each { |pen|
|
||||
pen.friction = !pen.friction
|
||||
pen.theta = (pen.theta <=> 0) * 45.0 unless pen.friction
|
||||
}
|
||||
else
|
||||
close
|
||||
end
|
||||
end
|
||||
|
||||
def needs_cursor?()
|
||||
true
|
||||
end
|
||||
|
||||
end # GfxWindow class
|
||||
|
||||
begin
|
||||
GfxWindow.new.show
|
||||
rescue Exception => e
|
||||
puts e.message, e.backtrace
|
||||
gets
|
||||
end
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
10 OVER 1: CLS
|
||||
20 LET theta=1
|
||||
30 LET g=9.81
|
||||
40 LET l=0.5
|
||||
50 LET speed=0
|
||||
100 LET pivotx=120
|
||||
110 LET pivoty=140
|
||||
120 LET bobx=pivotx+l*100*SIN (theta)
|
||||
130 LET boby=pivoty+l*100*COS (theta)
|
||||
140 GO SUB 1000: PAUSE 1: GO SUB 1000
|
||||
190 LET accel=g*SIN (theta)/l/100
|
||||
200 LET speed=speed+accel/100
|
||||
210 LET theta=theta+speed
|
||||
220 GO TO 100
|
||||
1000 PLOT pivotx,pivoty: DRAW bobx-pivotx,boby-pivoty
|
||||
1010 CIRCLE bobx,boby,3
|
||||
1020 RETURN
|
||||
Loading…
Add table
Add a link
Reference in a new issue