Created
November 29, 2011 17:29
-
-
Save seaneagan/1405629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**instance of Library*/ | |
interface Isolate { | |
final LibraryMirror library; | |
} | |
interface LibraryMirror extends ScopeMirror { | |
final LinkedHashMap<Import> imports; | |
final LinkedHashMap<Source> sources; | |
final Map <Dynamic> topLevels; | |
} | |
interface Import { | |
String get url; | |
String get prefix; | |
Library get library; | |
} | |
interface Source { | |
String url; | |
Library get library; | |
} | |
// () operator | |
interface Function { | |
// Matches any signature, all arguments optional | |
operator () (... List positional, [... Map<String> named]); | |
} | |
// Function typedefs just become interfaces | |
typedef Z F (X x, [Y y]); | |
// becomes | |
interface F extends Function { | |
Z operator () (X x, [Y y]); | |
} | |
class Foo implements F { | |
Z operator () (X x, [Y y]) {/*...*/} | |
} | |
// Function becomes extensible | |
interface NiceFunction extends Function factory NiceFunctionFactory { | |
NiceFunction memoize(Function hashFunction); | |
NiceFunction wrap (Function f); | |
NiceFunction compose(Function f); | |
NiceFunction wrap (Function f); | |
} | |
// | |
class FooFunction extends NiceFunctionFactory { | |
operator () (Bar bar, Baz baz) { | |
} | |
} | |
// generic functions via Function becomes extensible | |
interface GenericFunction<R, X1, X2, X3> extends Function { | |
GenericFunction(R _(X1 x1, X2 x2, X3 x3)); | |
R operator () (X1 x1, X2 x2, X3 x3); | |
} | |
// Rest parameters | |
foo(first, ...restPositional, [key, ...: restNamed]) { | |
//... | |
} | |
// Splays (inverse of rest parameters) | |
// List | |
Iterable<T> middle, rest; | |
//... | |
List<T> all = <T> [first, ... middle, next, ...rest]; | |
// Map | |
Map<K, V> from, fromAgain; | |
K key; | |
V value; | |
//... | |
// if a key appears more than once, last occurrence wins | |
Map<K, V> to = <K, V> {key: value, ...: from, ...: fromAgain}; | |
// Arguments | |
// List syntax for positional arguments | |
// Map syntax for named arguments, keys are toString'ed | |
f(first, ... middle, next, ...rest, foo: value, ...: from, ...: fromAgain); | |
interface Receiver<T> { | |
receive(_(T value), Sender replyTo); | |
} | |
interface Sender<T> { | |
Sender(); | |
Receiver<T> get receiver(); | |
send(T value, [Receiver replyTo]); | |
} | |
interface Result<T, E> extends Receiver<T> { | |
receiveException(_(E exception)); | |
} | |
interface Call<T, E> extends Sender<T> { | |
sendException(_(E exception)); | |
} | |
interface DatePort extends Port<void> { | |
DatePort(Duration duration); | |
} | |
interface RepeatDurationFuture extends Future<int> { | |
RepeatDurationFuture(Duration duration); | |
} | |
interface DateFuture extends Future<void> { | |
DateFuture(Date date); | |
} | |
new Timer(new Duration(milliseconds: 500)) | |
interface Realizer<T> extends Trigger<T> { | |
Realizer(); | |
Event<T> get event(); | |
void fire(T event); | |
} | |
class Matrix<T> implements List<List<T>> { | |
} | |
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
/** | |
* Thrown if you attempt to normalize a zero length vector. | |
*/ | |
class ZeroLengthVectorException implements Exception { | |
ZeroLengthVectorException() {} | |
} | |
/** | |
* 3 dimensional vector. | |
*/ | |
class Vector3 { | |
final double x; | |
final double y; | |
final double z; | |
// TODO - need to figure out better syntax for things like this | |
Vector3([num x, num y, num z]) : | |
x = (x != null) ? x.toDouble() : 0.0, | |
y = (y != null) ? y.toDouble() : 0.0, | |
z = (z != null) ? z.toDouble() : 0.0 | |
{} | |
double magnitude() => Math.sqrt(x*x + y*y + z*z); | |
Vector3 normalize() { | |
double len = magnitude(); | |
if (len == 0.0) { | |
throw new ZeroLengthVectorException(); | |
} | |
return new Vector3(x/len, y/len, z/len); | |
} | |
String toString() { | |
return "Vector3($x,$y,$z)"; | |
} | |
} | |
/** | |
* A 4x4 transformation matrix (for use with webgl) | |
* | |
* We label the elements of the matrix as follows: | |
* | |
* m11 m12 m13 m14 | |
* m21 m22 m23 m24 | |
* m31 m32 m33 m34 | |
* m41 m42 m43 m44 | |
* | |
* These are stored in a 16 element array of doubles. | |
* | |
*/ | |
class Matrix4 { | |
final List<double> buf; | |
double get m11() => buf[rc(1, 1)]; | |
double get m12() => buf[rc(1, 2)]; | |
double get m13() => buf[rc(1, 3)]; | |
double get m14() => buf[rc(1, 4)]; | |
double get m21() => buf[rc(2, 1)]; | |
double get m22() => buf[rc(2, 2)]; | |
double get m23() => buf[rc(2, 3)]; | |
double get m24() => buf[rc(2, 4)]; | |
double get m31() => buf[rc(3, 1)]; | |
double get m32() => buf[rc(3, 2)]; | |
double get m33() => buf[rc(3, 3)]; | |
double get m34() => buf[rc(3, 4)]; | |
double get m41() => buf[rc(4, 1)]; | |
double get m42() => buf[rc(4, 2)]; | |
double get m43() => buf[rc(4, 3)]; | |
double get m44() => buf[rc(3, 4)]; | |
void set m11(double m) { buf[rc(1, 1)] = m; } | |
void set m12(double m) { buf[rc(1, 2)] = m; } | |
void set m13(double m) { buf[rc(1, 3)] = m; } | |
void set m14(double m) { buf[rc(1, 4)] = m; } | |
void set m21(double m) { buf[rc(2, 1)] = m; } | |
void set m22(double m) { buf[rc(2, 2)] = m; } | |
void set m23(double m) { buf[rc(2, 3)] = m; } | |
void set m24(double m) { buf[rc(2, 4)] = m; } | |
void set m31(double m) { buf[rc(3, 1)] = m; } | |
void set m32(double m) { buf[rc(3, 2)] = m; } | |
void set m33(double m) { buf[rc(3, 3)] = m; } | |
void set m34(double m) { buf[rc(3, 4)] = m; } | |
void set m41(double m) { buf[rc(4, 1)] = m; } | |
void set m42(double m) { buf[rc(4, 2)] = m; } | |
void set m43(double m) { buf[rc(4, 3)] = m; } | |
void set m44(double m) { buf[rc(3, 4)] = m; } | |
String toString() { | |
List<String> rows = new List(); | |
for (int row = 1; row <= rows; row++) { | |
List<String> items = new List(); | |
for (int col = 1; col <= cols; col++) { | |
items.add(buf[rc(row, col)].toString()); | |
} | |
rows.add("| ${Strings.join(items, ", ")} |"); | |
} | |
return "Matrix4:\n${Strings.join(rows, '\n')}"; | |
} | |
/** | |
* returns the index into the buf list for a given | |
* row and column. | |
*/ | |
static int rc(int row, int col) { | |
assert(row <= rows && row > 0); | |
assert(col <= cols && col > 0); | |
return (row - 1) * rows + (col - 1); | |
} | |
/** | |
* Constructs a new Matrix4 with all entries initialized | |
* to null. | |
*/ | |
Matrix4(int rows, int cols) : buf = new List<double>(rows * cols) { } | |
/** | |
* Cosntructs a new Matrix4 with all entries initialized to zero. | |
*/ | |
Matrix4.zero() : this() { | |
for (int i = 0; i < buf.length; i++) { | |
buf[i] = 0.0; | |
} | |
} | |
/** | |
* Cosntructs a new Matrix4 that represents the identity transformation. | |
* (which means the diagonal entries are 1, and everything else is zero). | |
*/ | |
Matrix4.identity() : this.zero() { | |
for (int i = 1; i <= 4; i++) { | |
buf[rc(i, i)] = 1.0; | |
} | |
} | |
/** | |
* Constructs a new Matrix4 that represents a rotation around an axis. | |
* | |
* [degrees] number of degrees to rotate | |
* [axis] direction of axis of rotation (must not be zero length) | |
*/ | |
Matrix4.rotation(double degrees, Vector3 axis) : this.identity() { | |
double radians = degrees / 180.0 * Math.PI; | |
axis = axis.normalize(); | |
double x = axis.x; | |
double y = axis.y; | |
double z = axis.z; | |
double s = Math.sin(radians); | |
double c = Math.cos(radians); | |
double t = 1 - c; | |
m11 = x*x*t + c; m12 = y*x*t + z*s; m13 = z*x*t - y*s; | |
m21 = x*y*t - z*s; m22 = y*y*t + c; m23 = z*y*t + x*s; | |
m31 = x*z*t + y*s; m32 = y*z*t - x; m33 = z*z*t + c; | |
} | |
/** | |
* Constructs a new Matrix4 that represents translation. | |
* | |
* [v] - 3d vector representing which direction and how much to move | |
* | |
*/ | |
Matrix4.translation(Vector3 v) : this.identity() { | |
m14 = v.x; | |
m24 = v.y; | |
m34 = v.z; | |
} | |
/** | |
* returns the transpose of this matrix | |
*/ | |
Matrix4 transpose() { | |
Matrix4 m = new Matrix4(); | |
List<double> destBuf = m.buf; | |
for (int row = 1; row <= size; row++) { | |
for (int col = 1; col <= size; col++) { | |
destBuf[rc(col, row)] = buf[rc(row, col)]; | |
} | |
} | |
return m; | |
} | |
/** | |
* Matrix multiplication where | |
* | |
* C = AB | |
* | |
* A is this matrix | |
* B is another matrix | |
* C is the result of multiplying A * B. | |
* | |
*/ | |
Matrix4 operator *(Matrix4 matrixB) { | |
Matrix4 matrixC = new Matrix4.zero(); | |
List<double> bufA = this.buf; | |
List<double> bufB = matrixB.buf; | |
List<double> bufC = matrixC.buf; | |
for (int row = 1; row <= 4; row++) { | |
for (int col = 1; col <= 4; col++) { | |
for (int i = 1; i <= 4; i++) { | |
bufC[rc(row, col)] += | |
bufA[rc(row, i)] * bufB[rc(i, col)]; | |
} | |
} | |
} | |
return matrixC; | |
} | |
Matrix4 rotate(double degrees, Vector3 axis) => this * new Matrix4.rotation(degrees, axis); | |
Matrix4 translate(Vector3 vector) => this * new Matrix4.translation(vector); | |
// TODO - add other matrix operations - determinant, perspective | |
// projection, etc. | |
} | |
[Iterable]^[Collection] | |
[Collection]^[ReadableList] | |
[Collection]^[ReadableSet] | |
[ReadableSet]^[ReadableMultiMap] | |
[ReadableMultiMap]^[ReadableMap] | |
[ReadableMultiMap]^[MultiMap] | |
[ReadableMap]^[ReadableBiMap] | |
[ReadableBiMap]^[BiMap] | |
[ReadableList]^[Queue] | |
[Queue]^[List] | |
[Queue]^[Deque] | |
[ReadableMap]^[Map] | |
[ReadableSet]^[Set] | |
[Map]^[HashMap] | |
[HashMap]^[LinkedHashMap] | |
[Set]^[HashSet] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment