From c26c37cc82d16f4388c063f72e7f36166a21e97e Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Thu, 1 Jul 2021 23:34:44 +0200 Subject: [PATCH] Scale and translate + round instead of floor --- .../java/de/oshgnacknak/gruphi/Canvas.java | 32 +++++++++++++++---- .../java/de/oshgnacknak/gruphi/Drawable.java | 6 +++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/oshgnacknak/gruphi/Canvas.java b/src/main/java/de/oshgnacknak/gruphi/Canvas.java index 8089910..482e958 100644 --- a/src/main/java/de/oshgnacknak/gruphi/Canvas.java +++ b/src/main/java/de/oshgnacknak/gruphi/Canvas.java @@ -30,17 +30,24 @@ public class Canvas extends JPanel { @Override public void rect(double x, double y, double w, double h) { - g.fillRect((int) x, (int) y, (int) w, (int) h); + g.fillRect(round(x), round(y), round(w), round(h)); } @Override public void ellipse(double x, double y, double w, double h) { - g.fillOval((int) (x - w/2), (int) (y - h/2), (int) w, (int) h); + g.fillOval(round(x - w/2), round(y - h/2), round(w), round(h)); + } + + @Override + public void triangle(double x1, double y1, double x2, double y2, double x3, double y3) { + int[] xs = {round(x1), round(x2), round(x3)}; + int[] ys = {round(y1), round(y2), round(y3)}; + g.fillPolygon(xs, ys, 3); } @Override public void line(double x1, double y1, double x2, double y2) { - g.drawLine((int) x1, (int) y1, (int) x2, (int) y2); + g.drawLine(round(x1), round(y1), round(x2), round(y2)); } @Override @@ -51,11 +58,22 @@ public class Canvas extends JPanel { } @Override - public void triangle(double x1, double y1, double x2, double y2, double x3, double y3) { - int[] xs = {(int) x1, (int) x2, (int) x3}; - int[] ys = {(int) y1, (int) y2, (int) y3}; - g.fillPolygon(xs, ys, 3); + public void translated(double x, double y, Runnable r) { + g.translate(x, y); + r.run(); + g.translate(-x, -y); + } + + @Override + public void scaled(double scale, Runnable r) { + g.scale(scale, scale); + r.run(); + g.scale(1/scale, 1/scale); } }); } + + private int round(double d) { + return (int) Math.round(d); + } } diff --git a/src/main/java/de/oshgnacknak/gruphi/Drawable.java b/src/main/java/de/oshgnacknak/gruphi/Drawable.java index c588cde..47f0369 100644 --- a/src/main/java/de/oshgnacknak/gruphi/Drawable.java +++ b/src/main/java/de/oshgnacknak/gruphi/Drawable.java @@ -12,9 +12,13 @@ public interface Drawable { void ellipse(double x, double y, double w, double h); + void triangle(double x1, double y1, double x2, double y2, double x3, double y3); + void line(double x1, double y1, double x2, double y2); void rotated(double theta, double x, double y, Runnable r); - void triangle(double x1, double y1, double x2, double y2, double x3, double y3); + void translated(double x, double y, Runnable r); + + void scaled(double scale, Runnable r); }