diff --git a/pom.xml b/pom.xml index 659b3df0..b583d3fb 100644 --- a/pom.xml +++ b/pom.xml @@ -109,7 +109,7 @@ org.json json - 20190722 + 20231013 diff --git a/src/aya/ext/graphics/GraphicsInstructionStore.java b/src/aya/ext/graphics/GraphicsInstructionStore.java index 5593b2e2..32cd8d06 100644 --- a/src/aya/ext/graphics/GraphicsInstructionStore.java +++ b/src/aya/ext/graphics/GraphicsInstructionStore.java @@ -1,6 +1,7 @@ package aya.ext.graphics; import aya.ext.graphics.instruction.ArcGraphicsInstruction; +import aya.ext.graphics.instruction.PointsGraphicsInstruction; import aya.ext.graphics.instruction.ClearGraphicsInstruction; import aya.ext.graphics.instruction.ClearRectGraphicsInstruction; import aya.ext.graphics.instruction.CloseGraphicsInstruction; @@ -50,6 +51,7 @@ protected void init() { addInstruction(new ClickEventsInstruction(canvas_table)); addInstruction(new CloseGraphicsInstruction(canvas_table)); addInstruction(new CopyRectGraphicsInstruction(canvas_table)); + addInstruction(new PointsGraphicsInstruction(canvas_table)); addInstruction(new EllipseGraphicsInstruction(canvas_table)); addInstruction(new GetPixelsGraphicsInstruction(canvas_table)); addInstruction(new IsOpenGraphicsInstruction(canvas_table)); diff --git a/src/aya/ext/graphics/instruction/PointsGraphicsInstruction.java b/src/aya/ext/graphics/instruction/PointsGraphicsInstruction.java new file mode 100644 index 00000000..df0cedf0 --- /dev/null +++ b/src/aya/ext/graphics/instruction/PointsGraphicsInstruction.java @@ -0,0 +1,50 @@ +package aya.ext.graphics.instruction; + +import aya.eval.BlockEvaluator; +import aya.exceptions.runtime.IndexError; +import aya.ext.graphics.Canvas; +import aya.ext.graphics.CanvasTable; +import aya.ext.graphics.GraphicsInstruction; +import aya.obj.Obj; +import aya.obj.list.List; +import aya.obj.list.numberlist.NumberList; +import aya.util.Casting; + +public class PointsGraphicsInstruction extends GraphicsInstruction { + + public PointsGraphicsInstruction(CanvasTable canvas_table) { + super(canvas_table, "points", "LNNN"); + _doc = "points width height canvas_id: draw a batch of points"; + } + + + @Override + protected void doCanvasCommand(Canvas cvs, BlockEvaluator blockEvaluator) { + final int h = _reader.popInt(); + final int w = _reader.popInt(); + List points = _reader.popList(); + + final int half_h = h/2; + final int half_w = w/2; + + try { + for (int i = 0; i < points.length(); i++) { + Obj point_obj = points.getExact(i); + if (point_obj.isa(Obj.LIST)) { + NumberList point = Casting.asList(points.getExact(i)).toNumberList(); + int x = point.get(0).toInt(); + int y = point.get(1).toInt(); + + cvs.getG2D().fillRect(x-half_w, y-half_h, w, h); + } + } + } catch (IndexOutOfBoundsException e) { + throw new IndexError("All points must have length 2"); + } + + cvs.refresh(); + } +} + + + diff --git a/src/aya/util/BlockReader.java b/src/aya/util/BlockReader.java index 62ee0d1b..fba92008 100644 --- a/src/aya/util/BlockReader.java +++ b/src/aya/util/BlockReader.java @@ -4,6 +4,7 @@ import aya.exceptions.runtime.TypeError; import aya.instruction.named.NamedOperator; import aya.obj.Obj; +import aya.obj.list.List; import aya.obj.list.numberlist.NumberList; import aya.obj.symbol.Symbol; @@ -69,4 +70,13 @@ public Symbol popSymbol() { throw new TypeError(_inst, "J", o); } } + + public List popList() { + final Obj o = _block.pop(); + if (o.isa(Obj.LIST)) { + return Casting.asList(o); + } else { + throw new TypeError(_inst, "L", o); + } + } } diff --git a/src/aya/util/VectorizedFunctions.java b/src/aya/util/VectorizedFunctions.java index 817b21b3..8f1a65fc 100644 --- a/src/aya/util/VectorizedFunctions.java +++ b/src/aya/util/VectorizedFunctions.java @@ -72,8 +72,18 @@ private static List vectorizeObjList(ExecutionContext context, Operator op, Obj // private static List vectorizeListList(ExecutionContext context, Operator op, List a, List b, NumberListOp nlop) { - if (a.isa(NUMBERLIST) && b.isa(NUMBERLIST) && a.length() == b.length()) { - return new List(nlop.ll(asNumberList(a), asNumberList(b))); + if (a.isa(NUMBERLIST) && b.isa(NUMBERLIST)) { + final int a_len = a.length(); + final int b_len = b.length(); + if (a_len == b_len) { + return new List(nlop.ll(asNumberList(a), asNumberList(b))); + } else if (a_len == 1) { + return new List(nlop.nl(asNumberList(a).get(0), asNumberList(b))); + } else if (b_len == 1) { + return new List(nlop.ln(asNumberList(a), asNumberList(b).get(0))); + } else { + return vectorizeListList(context, op, a, b); + } } else { return vectorizeListList(context, op, a, b); } diff --git a/std/canvas.aya b/std/canvas.aya index 5525871e..3539b2eb 100644 --- a/std/canvas.aya +++ b/std/canvas.aya @@ -61,6 +61,10 @@ def canvas::fillcircle {x y r self, x y r2* $ 1 self.id :{graphics.ellipse} } +def canvas::points {points r self, + points r2* $ self.id :{graphics.points} +} + def canvas::set_color {color self, .# ::set_color {, color.r:r color.g:g color.b:b } self.id :{graphics.MG} ; color.r color.g color.b self.id :{graphics.set_color}