September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,55 @@
100 PROGRAM "Pendulum.bas"
110 LET THETA=RAD(50):LET G=9.81:LET L=.5
120 CALL INIC
130 CALL DRAWING
140 CALL ANIMATE
150 CALL RESET
160 END
170 DEF INIC
180 CLOSE #102
190 OPTION ANGLE RADIANS
200 SET STATUS OFF:SET INTERRUPT STOP OFF:SET BORDER 56
210 SET VIDEO MODE 1:SET VIDEO COLOR 1:SET VIDEO X 14:SET VIDEO Y 8
220 FOR I=1 TO 24
230 OPEN #I:"video:"
240 SET #I:PALETTE 56,0,255,YELLOW
250 NEXT
260 END DEF
270 DEF DRAWING
280 LET SPD=0
290 FOR I=1 TO 24
300 DISPLAY #I:AT 3 FROM 1 TO 8
310 SET #I:INK 2
320 PLOT #I:224,280,ELLIPSE 10,10
330 PLOT #I:0,280;214,280,234,280;446,280
340 SET #I:INK 1
350 CALL PENDULUM(THETA,L,I)
360 LET ACC=-G*SIN(THETA)/L/100
370 LET SPD=SPD+ACC/10.5
380 LET THETA=THETA+SPD
390 NEXT
400 END DEF
410 DEF PENDULUM(A,L,CH)
420 LET PX=224:LET PY=280
430 LET BX=PX+L*460*SIN(A)
440 LET BY=PY-L*460*COS(A)
450 PLOT #CH:PX,PY;BX,BY
460 PLOT #CH:BX+24*SIN(A),BY-24*COS(A),ELLIPSE 20,20,
470 SET #CH:INK 3:PLOT #CH:PAINT
480 END DEF
490 DEF ANIMATE
500 DO
510 FOR I=1 TO 24
520 DISPLAY #I:AT 3 FROM 1 TO 8
530 NEXT
540 FOR I=23 TO 2 STEP-1
550 DISPLAY #I:AT 3 FROM 1 TO 8
560 NEXT
570 LOOP UNTIL INKEY$=CHR$(27)
580 END DEF
590 DEF RESET
600 TEXT 40:SET STATUS ON:SET INTERRUPT STOP ON:SET BORDER 0
610 FOR I=24 TO 1 STEP-1
620 CLOSE #I
630 NEXT
640 END DEF

View file

@ -10,7 +10,7 @@ const framerate = 50.0 # intended frame rate/sec
const t0 = 0.0 # start time (s)
const tf = 2.3 # end simulation time (s)
const dtframe = 1.0/framerate # time increment per frame
const tspan = linspace(t0, tf, Int(floor(tf*framerate))) # array of time points in animation
const tspan = LinRange(t0, tf, Int(floor(tf*framerate))) # array of time points in animation
const bgcolor = "black" # gif background
const leaderhue = (0.80, 0.70, 0.20) # gif swing arm hue light gold
@ -19,9 +19,9 @@ const hslcolors = [HSL(col) for col in (distinguishable_colors(
const giffilename = "pendulum.gif" # output file
# differential equations
simplependulum(t,u,du) = (θ=u[1]; dθ=u[2]; du[1]=dθ; du[2]=-(g/L)*sin(θ))
bc2(residual, ua, ub) = (residual[1] = ua[1] + pi/2; residual[2] = ub[1] + pi/2)
bvp2 = TwoPointBVProblem(simplependulum, bc2, [pi/2,pi/2], (tspan[1],tspan[end]))
simplependulum(du, u, p, t) = (θ=u[1]; dθ=u[2]; du[1]=dθ; du[2]=-(g/L)*sin(θ))
bc2(residual, u, p, t) = (residual[1] = u[end÷2][1] + pi/2; residual[2] = u[end][1] - pi/2)
bvp2 = BVProblem(simplependulum, bc2, [pi/2,pi/2], (tspan[1],tspan[end]))
sol2 = solve(bvp2, MIRK4(), dt=dtframe) # use the MIRK4 solver for TwoPointBVProblem
# movie making background

View file

@ -0,0 +1,90 @@
use SDL2::Raw;
use Cairo;
my $width = 1000;
my $height = 400;
SDL_Init(VIDEO);
my $window = SDL_CreateWindow(
'Pendulum - Perl 6',
SDL_WINDOWPOS_CENTERED_MASK,
SDL_WINDOWPOS_CENTERED_MASK,
$width, $height, RESIZABLE
);
my $render = SDL_CreateRenderer($window, -1, ACCELERATED +| PRESENTVSYNC);
my $bob = Cairo::Image.create( Cairo::FORMAT_ARGB32, 32, 32 );
given Cairo::Context.new($bob) {
my Cairo::Pattern::Gradient::Radial $sphere .=
create(13.3, 12.8, 3.2, 12.8, 12.8, 32);
$sphere.add_color_stop_rgba(0, 1, 1, .698, 1);
$sphere.add_color_stop_rgba(1, .623, .669, .144, 1);
.pattern($sphere);
.arc(16, 16, 15, 0, 2 * pi);
.fill;
$sphere.destroy;
}
my $bob_texture = SDL_CreateTexture(
$render, %PIXELFORMAT<ARGB8888>,
STATIC, 32, 32
);
SDL_UpdateTexture(
$bob_texture,
SDL_Rect.new(:x(0), :y(0), :w(32), :h(32)),
$bob.data, $bob.stride // 32
);
SDL_SetTextureBlendMode($bob_texture, 1);
SDL_SetRenderDrawBlendMode($render, 1);
my $event = SDL_Event.new;
my $now = now; # time
my = -π/3; # start angle
my $ppi = 500; # scale
my $g = -9.81; # accelaration of gravity
my $ax = $width/2; # anchor x
my $ay = 25; # anchor y
my $len = $height - 75; # 'rope' length
my $vel; # velocity
my $dt; # delta time
main: loop {
while SDL_PollEvent($event) {
my $casted_event = SDL_CastEvent($event);
given $casted_event {
when *.type == QUIT { last main }
when *.type == WINDOWEVENT {
if .event == 5 {
$width = .data1;
$height = .data2;
$ax = $width/2;
$len = $height - 75;
}
}
}
}
$dt = now - $now;
$now = now;
$vel += $g / $len * sin() * $ppi * $dt;
+= $vel * $dt;
my $bx = $ax + sin() * $len;
my $by = $ay + cos() * $len;
SDL_SetRenderDrawColor($render, 255, 255, 255, 255);
SDL_RenderDrawLine($render, |($ax, $ay, $bx, $by)».round);
SDL_RenderCopy( $render, $bob_texture, Nil,
SDL_Rect.new($bx - 16, $by - 16, 32, 32)
);
SDL_RenderPresent($render);
SDL_SetRenderDrawColor($render, 0, 0, 0, 0);
SDL_RenderClear($render);
}
SDL_Quit();

View file

@ -1,7 +1,4 @@
--
-- demo\rosetta\animate_pendulum2.exw
-- ==================================
--
include pGUI.e
Ihandle dlg, canvas, timer

View file

@ -1,34 +1,22 @@
import java.awt.Color
import scala.actors.Actor
import scala.swing.{ Graphics2D, MainFrame, Panel, SimpleSwingApplication }
import java.util.concurrent.{Executors, TimeUnit}
import scala.swing.{Graphics2D, MainFrame, Panel, SimpleSwingApplication}
import scala.swing.Swing.pair2Dimension
object Pendulum extends SimpleSwingApplication {
val length = 100
lazy val ui = new Panel {
import scala.math.{ cos, Pi, sin }
import scala.math.{cos, Pi, sin}
background = Color.white
preferredSize = (2 * length + 50, length / 2 * 3)
peer.setDoubleBuffered(true)
var angle: Double = Pi / 2
def pendular = new Actor {
var angleVelocity = 0.0
val dt = 0.1
def act() {
while (true) {
angleVelocity += (-9.81 / length * sin(angle)) * dt
angle += angleVelocity * dt
repaint()
Thread.sleep(15)
}
}
}
override def paintComponent(g: Graphics2D) = {
override def paintComponent(g: Graphics2D): Unit = {
super.paintComponent(g)
val (anchorX, anchorY) = (size.width / 2, size.height / 4)
@ -43,12 +31,25 @@ object Pendulum extends SimpleSwingApplication {
g.setColor(Color.yellow)
g.fillOval(ballX - 7, ballY - 7, 14, 14)
}
val animate: Runnable = new Runnable {
var angleVelocity = 0.0
var dt = 0.1
override def run(): Unit = {
angleVelocity += -9.81 / length * Math.sin(angle) * dt
angle += angleVelocity * dt
repaint()
}
}
}
def top = new MainFrame {
override def top = new MainFrame {
title = "Rosetta Code >>> Task: Animate a pendulum | Language: Scala"
contents = ui
centerOnScreen
ui.pendular.start
centerOnScreen()
Executors.
newSingleThreadScheduledExecutor().
scheduleAtFixedRate(ui.animate, 0, 15, TimeUnit.MILLISECONDS)
}
}