Skip to content

Commit 7366421

Browse files
committed
Adds more files from Silverstone
1 parent 4c642d4 commit 7366421

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

fallingApples/appleAnimation.grace

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import "graphix" as g
2+
import "random" as random
3+
4+
// Create the graphics stage
5+
var graphics := g.create(200,400)
6+
7+
def trunk = graphics.addRectangle.at(110@75).setSize(40@325).colored "SaddleBrown".filled(true).draw
8+
def leaves = graphics.addEllipse.at(5@30).setSize(250@140).colored "DarkGreen".filled(true).draw
9+
def apple = graphics.addCircle.at(50@70).setRadius 10 .colored "YellowGreen".filled(true).draw
10+
11+
trunk.onClickDo { apple.drawAt(50@70) }
12+
var applePath
13+
14+
apple.onClick := {
15+
applePath := fallPath
16+
graphics.tickEvent(fall, 20)
17+
}
18+
method fallPath {
19+
def duration = 500
20+
def path = list []
21+
def gravity = (random.between (-0.3) and 0.3)@2
22+
def damping = 0.6
23+
var initial := apple.location
24+
def ground = initial.x@390
25+
var tick := 0
26+
var p := initial
27+
while { p.y < ground.y } do {
28+
// the fall from rest to the ground
29+
// uses s = ut + (a.t^2)/2
30+
tick := tick + 1
31+
p := initial + (gravity * tick * tick / 2)
32+
path.addLast(p)
33+
}
34+
var v := gravity * tick
35+
repeat 10 times {
36+
def u = - (v * damping)
37+
v := u
38+
tick := 0
39+
do {
40+
// the bounce:
41+
// uses s = ut + (a.t^2)/2 and v = u + at
42+
tick := tick + 1
43+
p := ground + (u * tick) + (gravity * tick * tick / 2)
44+
v := u + (gravity * tick)
45+
path.addLast(p)
46+
} while { p.y < ground.y }
47+
}
48+
path
49+
}
50+
51+
52+
def fall = {
53+
if (applePath.isEmpty) then {
54+
graphics.clearTicker
55+
} else {
56+
apple.drawAt(applePath.removeFirst)
57+
}
58+
}

nameResolutionExample.grace

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
method foo { print "outer" }
2+
3+
class app {
4+
method barf { foo }
5+
}
6+
7+
class bar {
8+
inherit app
9+
method foo { print "bar" }
10+
}
11+
12+
class baz {
13+
inherit bar
14+
method barf { foo } // ambigous: self.foo or outer.foo?
15+
}
16+
17+
18+
app.barf //prints "outer"
19+
bar.barf //prints "outer"
20+
baz.barf //prints "bar" or "outer", depending on the resolution of foo at line 14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var sieve // sieve.at(i) represents the primeness of (2*i) + 1
2+
var limit
3+
4+
method initialize(maxPrime) {
5+
limit := maxPrime
6+
sieve := (1..((limit-1)/2).truncated).map { each -> true }.asList
7+
}
8+
method markNot(n) {
9+
// mark the fact that n is not a prime
10+
sieve.at((n-1)/2) put(false)
11+
}
12+
method next(n) {
13+
def start = (n-1)/2
14+
((start+1)..limit).do { candidate ->
15+
if (sieve.at(candidate)) then { return (2*candidate) + 1 }
16+
}
17+
}
18+
19+
method print {
20+
sieve.keysAndValuesDo { n, b ->
21+
if (b) then { prelude.print((2*n) + 1) }
22+
}
23+
}
24+
25+
method runSieve {
26+
var currentPrime := 3
27+
var ix
28+
do {
29+
ix := currentPrime * 2
30+
while {ix ≤ limit} do {
31+
self.markNot(ix)
32+
ix := ix + currentPrime
33+
}
34+
currentPrime := self.next(currentPrime)
35+
} while { (currentPrime * currentPrime) ≤ limit }
36+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var sieve:ListBoolean// sieve.at(i) represents the primeness of (2*i) + 1
2+
var limit:Number
3+
4+
method initialize(maxPrime:Number) -> Done {
5+
limit := maxPrime
6+
sieve := (1..((limit-1)/2).truncated).map { each:Number -> true }.asList
7+
}
8+
method markNot(n:Number) -> Done {
9+
// mark the fact that n is not a prime
10+
sieve.at((n-1)/2) put(false)
11+
}
12+
method next(n:Number) -> Number {
13+
def start = (n-1)/2
14+
((start+1)..limit).do { candidate ->
15+
if (sieve.at(candidate)) then { return (2*candidate) + 1 }
16+
}
17+
}
18+
19+
method print -> Done {
20+
sieve.keysAndValuesDo { n, b ->
21+
if (b) then { prelude.print((2*n) + 1) }
22+
}
23+
}
24+
25+
method runSieve -> Done {
26+
var currentPrime:Number := 3
27+
var ix:Number
28+
_prelude.do {
29+
ix := currentPrime * 2
30+
while {ix ≤ limit} do {
31+
self.markNot(ix)
32+
ix := ix + currentPrime
33+
}
34+
currentPrime := self.next(currentPrime)
35+
} while { (currentPrime * currentPrime) ≤ limit }
36+
}

sieveOfEratosthenes/runSieve.grace

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "primes-withTypes" as primes
2+
primes.initialize(1000)
3+
primes.runSieve
4+
primes.print
5+

0 commit comments

Comments
 (0)