Skip to content

Commit cadb3b5

Browse files
committed
Adds fromgrams from web ide on Sylvanite
1 parent 5c86570 commit cadb3b5

File tree

11 files changed

+763
-1
lines changed

11 files changed

+763
-1
lines changed

binaryTrees/drawTree.grace

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import "graphix" as graphix
2+
import "binaryTree" as tree
3+
import "unicode" as u
4+
5+
class layout(t) {
6+
// creates an object that lays-out the tree t
7+
var col := 0
8+
def locationOf = dictionary [ ]
9+
method knuthLayout(root, depth) {
10+
if (root.left.isEmpty.not) then { knuthLayout(root.left, depth + 1) }
11+
locationOf.at(root) put(col@depth)
12+
col := col + 1
13+
if (root.right.isEmpty.not) then { knuthLayout(root.right, depth + 1) }
14+
}
15+
16+
method draw(root) {
17+
if (root.left.isEmpty.not) then {
18+
drawEdgeFrom(locationOf.at(root)) to (locationOf.at(root.left))
19+
draw(root.left)
20+
}
21+
if (root.right.isEmpty.not) then {
22+
drawEdgeFrom(locationOf.at(root)) to (locationOf.at(root.right))
23+
draw(root.right)
24+
}
25+
drawNode(locationOf.at(root), root.data)
26+
}
27+
knuthLayout(t.root, 0)
28+
draw(t.root)
29+
}
30+
31+
// Drawing stuff
32+
def g = graphix.create(200, 300)
33+
def scale = 20
34+
def offset = 15@30
35+
36+
method drawNode(location, label) {
37+
def shape = g.addCircle.setRadius(10).filled(true).colored "yellow".
38+
at(location * scale + offset).draw
39+
def labelText = g.addText.setContent(label).setFont "9px Arial".
40+
at((location * scale) + offset - (2@6)).draw
41+
}
42+
43+
method drawEdgeFrom(s) to (e) {
44+
g.addLine.setStart((s * scale) + offset).setEnd((e * scale )+ offset).draw
45+
}
46+
47+
// An example tree to demonstrate the algorithm
48+
def exampleTree = tree.with [ ]
49+
tournamentTree(exampleTree, 1, 10)
50+
method tournamentTree(grow, lo, hi) {
51+
if (lo <= hi) then {
52+
def k = ((lo + hi) / 2).truncated
53+
def d = u.create("a".ord - 1 + k)
54+
grow.at(k) put (d)
55+
tournamentTree(grow, lo, k-1)
56+
tournamentTree(grow, k+1, hi)
57+
}
58+
}
59+
60+
layout(exampleTree)

fallingApples/andrewsAppleLab.grace

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import "graphix" as graphix
2+
import "random" as random
3+
4+
var graphics := graphix.create(200,700)
5+
6+
//
7+
// Game class
8+
//
9+
class createAppleGame(numberOfApples) {
10+
method isBadGuess(char) -> Boolean{
11+
// answers true if char has not been guessed before, and is not in the word.
12+
// NOT IMPLEMENTED
13+
true
14+
}
15+
method wordSize -> Number {
16+
// the size of the current word
17+
// NOT IMPLEMENTED
18+
}
19+
method numberOfBadGuesses -> Number { numberOfApples }
20+
21+
// add more methods as required
22+
}
23+
24+
def display = createDisplayForGame(createAppleGame(6))
25+
26+
//
27+
// GameDisplay class
28+
//
29+
class createDisplayForGame(g) {
30+
def trunk is public = graphics.addRectangle.at((110@75)).setSize(40@325).colored "SaddleBrown".filled(true).draw
31+
def leaves is public = graphics.addEllipse.at((5@30)).setSize(250@140).colored "DarkGreen".filled(true).draw
32+
// NOT IMPLEMENTED - Create the correct number of apples.
33+
// The starter code creates only one apple.
34+
def apple1 = createAppleAt(50@70)
35+
def newGameButton = graphics.addButton.setText "new game" .setSize(75@33).at(10@450).draw
36+
def inputBox = graphics.addInputBox.setWidth(40).at(80@450).draw
37+
// NOT IMPLEMENTED - starter code does not create the row of boxes for the letters to fill.
38+
inputBox.onSubmitDo { apple1.fall }
39+
newGameButton.onClick := {
40+
// NOT IMPLEMENTED - should request the game to start over
41+
apple1.at(50@75)
42+
apple1.draw
43+
}
44+
}
45+
46+
//
47+
// wordList object
48+
//
49+
def wordList = object {
50+
def words = list [
51+
"Apple", "Apricot", "Avocado", "Banana", "Bilberry", "Blackberry",
52+
"Blackcurrant", "Blueberry", "Boysenberry", "Cantaloupe", "Currant", "Cherry",
53+
"Cherimoya", "Cloudberry", "Coconut", "Cranberry", "Damson", "Date",
54+
"Dragonfruit", "Durian", "Elderberry", "Feijoa", "Fig", "Grapefruit", "Guava",
55+
"Huckleberry", "Jackfruit", "Kiwi", "Kumquat", "Lemon", "Lime", "Loquat",
56+
"Lychee", "Mango", "Marionberry", "Melon", "Cantaloupe", "Honeydew",
57+
"Watermelon", "Mulberry", "Nectarine", "Olive", "Orange", "Clementine",
58+
"Mandarine", "Tangerine", "Papaya", "Passionfruit", "Peach", "Pear",
59+
"Persimmon", "Physalis", "Plum", "Pineapple", "Pomegranate", "Pomelo",
60+
"Mangosteen", "Quince", "Raspberry", "Salmonberry", "Raspberry",
61+
"Rambutan", "Redcurrant", "Satsuma", "Starfruit", "Strawberry", "Tamarillo"]
62+
63+
method atRandom -> String {
64+
// returns one of the strings from in the list words, chosen at random.
65+
def index = (random.between0And1 * words.size).truncated + 1
66+
words.at(index)
67+
}
68+
}
69+
70+
//
71+
// Apple class
72+
//
73+
class createAppleAt(position) {
74+
inherits graphics.addCircle
75+
self.at(position).setRadius 10.colored "YellowGreen".filled(true).draw
76+
var applePath
77+
def fallAction = {
78+
if (applePath.isEmpty) then {
79+
graphics.clearTicker
80+
} else {
81+
self.location := applePath.first
82+
self.draw
83+
applePath.removeFirst
84+
}
85+
}
86+
87+
method fall {
88+
applePath := fallPath
89+
graphics.tickEvent(fallAction, 20)
90+
}
91+
method fallPath is confidential {
92+
def duration = 500
93+
def path = list [ ]
94+
def gravity = 0@2
95+
def damping = 0.5
96+
var initial := self.location
97+
def ground = initial.x@390
98+
var tick := 0
99+
var p := initial
100+
while { p.y < ground.y } do {
101+
// the fall from rest to the ground
102+
// uses s = ut + (a.t^2)/2
103+
tick := tick + 1
104+
p := initial + (gravity * tick * tick / 2)
105+
path.addLast(p)
106+
}
107+
var v := gravity * tick
108+
repeat 5 times {
109+
def u = (0@0) - (v * damping)
110+
v := u
111+
tick := 0
112+
do {
113+
// the bounce:
114+
// uses s = ut + (a.t^2)/2 and v = u + at
115+
tick := tick + 1
116+
p := ground + (u * tick) + (gravity * tick * tick / 2)
117+
v := u + (gravity * tick)
118+
path.addLast(p)
119+
} while { p.y < ground.y }
120+
}
121+
path
122+
}
123+
}

fallingApples/graphixAnimation.grace

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

joeTheBox/boxesUnderGravity.grace

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
dialect "objectdraw"
2+
import "noahBox" as box
3+
import "animation" as anim
4+
inherits graphicApplicationSize(1000@1000)
5+
6+
// Noah Zentzis
7+
// CS 420 (Object Oriented Programming)
8+
// HW 1 - Dancing Boxes
9+
// April 5, 2016
10+
11+
def boundary = filledRectAt(0@0) size (1000@1000) on (canvas)
12+
boundary.color := color.neutral
13+
boundary.addToCanvas(canvas)
14+
15+
startGraphics
16+
17+
def alice = box.named "A"
18+
alice.showOn(canvas)
19+
alice.moveTo(450@450)
20+
21+
def bob = box.named "B"
22+
bob.showOn(canvas)
23+
bob.moveTo(550@550)
24+
25+
def nullForce = object {
26+
method calculate(state, time) { 0@0 }
27+
}
28+
29+
var mouseForce := nullForce
30+
31+
class gravity.towards(positionBlock) and(other) {
32+
def posBlock = positionBlock
33+
def last = other
34+
35+
method calculate(state, time) {
36+
// F_g = 0.5 * (G * m1 * m2) / (d ^ 2)
37+
// not using real gravity, because this is more interesting
38+
def pos = posBlock.apply
39+
def dist = state.position.distanceTo(pos)
40+
def multiplier = 0.5 * 20000 / (dist)
41+
def vector = (pos) - state.position
42+
def unit = vector / vector.length
43+
44+
(unit * multiplier) + last.apply.calculate(state, time)
45+
}
46+
}
47+
48+
method onMousePress(pt:Point) -> Done {
49+
mouseForce := gravity.towards { pt } and {nullForce}
50+
}
51+
52+
method onMouseDrag(pt:Point) -> Done {
53+
mouseForce := gravity.towards { pt } and {nullForce}
54+
}
55+
56+
method onMouseRelease(pt:Point) -> Done {
57+
mouseForce := nullForce
58+
}
59+
60+
def bobForce = gravity.towards {alice.position} and {mouseForce}
61+
def aliceForce = gravity.towards {bob.position} and {mouseForce}
62+
63+
anim.while {true} pausing 10 do {
64+
alice.moveFor(0.01) accelerating(aliceForce)
65+
bob.moveFor(0.01) accelerating(bobForce)
66+
67+
if(alice.isOutsideBounds(1000@1000)) then {
68+
alice.repositionInside(1000@1000)
69+
}
70+
if(bob.isOutsideBounds(1000@1000)) then {
71+
bob.repositionInside(1000@1000)
72+
}
73+
}

0 commit comments

Comments
 (0)