Skip to content

Commit

Permalink
Merge pull request #118 from aya-lang/feature/points-graphics-instruc…
Browse files Browse the repository at this point in the history
…tion

Add graphics instruction for drawing a batch of points
  • Loading branch information
nick-paul authored Nov 28, 2024
2 parents 6734779 + 88ee351 commit d081d59
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/aya/ext/graphics/GraphicsInstructionStore.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
Expand Down
50 changes: 50 additions & 0 deletions src/aya/ext/graphics/instruction/PointsGraphicsInstruction.java
Original file line number Diff line number Diff line change
@@ -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();
}
}



10 changes: 10 additions & 0 deletions src/aya/util/BlockReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
4 changes: 4 additions & 0 deletions std/canvas.aya
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit d081d59

Please sign in to comment.