Template:E 2D utilities: Difference between revisions
Content added Content deleted
(avoid extra whitespace) |
No edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
<noinclude>Some [[Category:E]] library code for the [[Spiral]] and [[Zig Zag]] tasks. Perhaps ought to be put into generally available E data structure/math libraries. |
<noinclude>Some [[Category:E]] library code for the [[Spiral]] and [[Zig Zag]] tasks. Perhaps ought to be put into generally available E data structure/math libraries. |
||
</noinclude>< |
{{template}}</noinclude><syntaxhighlight lang="e">/** Missing scalar multiplication, but we don't need it. */ |
||
def makeVector2(x, y) { |
def makeVector2(x, y) { |
||
return def vector { |
return def vector { |
||
Line 26: | Line 26: | ||
to put(r, c, v) { storage[r * cols + c] := v } |
to put(r, c, v) { storage[r * cols + c] := v } |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
Latest revision as of 02:22, 31 July 2023
Some library code for the Spiral and Zig Zag tasks. Perhaps ought to be put into generally available E data structure/math libraries.
/** Missing scalar multiplication, but we don't need it. */
def makeVector2(x, y) {
return def vector {
to x() { return x }
to y() { return y }
to add(other) { return makeVector2(x + other.x(), y + other.y()) }
to clockwise() { return makeVector2(-y, x) }
}
}
/** Bugs: (1) The printing is specialized. (2) No bounds check on the column. */
def makeFlex2DArray(rows, cols) {
def storage := ([null] * (rows * cols)).diverge()
return def flex2DArray {
to __printOn(out) {
for y in 0..!rows {
for x in 0..!cols {
out.print(<import:java.lang.makeString>.format("%3d", [flex2DArray[y, x]]))
}
out.println()
}
}
to get(r, c) { return storage[r * cols + c] }
to put(r, c, v) { storage[r * cols + c] := v }
}
}