From 14d4fd0d0d84e3b8a52ecde790d878fcb909ddd0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 14 Feb 2018 11:41:53 +0100 Subject: [PATCH 1/1] Add the transformations stack code. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP2/Cercle.java | 13 +++++--- TP2/Main.java | 32 +++++++++++++++++++ TP2/Makefile | 1 + TP2/Piletransformations.java | 60 ++++++++++++++++++++++++++++++++++++ TP2/Point.java | 8 +++-- TP2/Segment.java | 13 +++++--- 6 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 TP2/Piletransformations.java diff --git a/TP2/Cercle.java b/TP2/Cercle.java index 4bf743f..fe17b44 100644 --- a/TP2/Cercle.java +++ b/TP2/Cercle.java @@ -8,12 +8,15 @@ class Cercle { rayon = r; } - void dessiner(Piletransformations pile) { - + public void dessiner(Piletransformations pile) { + String className = this.getClass().getSimpleName(); + Point pTrans = pile.getCurrentTransformation(); + Point pOriTrans = pOri.additionner(pTrans); + System.out.println(className + " " + pOri.toString() + "->" + pOriTrans.toString() + " " + rayon); } - void deplacer(Point p) { - + public void deplacer(Point p) { + pOri = pOri.additionner(p); } - + } diff --git a/TP2/Main.java b/TP2/Main.java index 0a031cd..e05593a 100644 --- a/TP2/Main.java +++ b/TP2/Main.java @@ -4,6 +4,38 @@ class Main { public static void main(String[] args) { + Point p1 = new Point(1, 2); + Point p2 = new Point(2, 7); + Point p3 = p1.additionner(p2); + + System.out.println(p1.toString()); + System.out.println(p2.toString()); + System.out.println(p3.toString()); + + Point p4 = new Point(4, 5); + Point p5 = new Point(2, 3); + Segment segment = new Segment(p4, p5); + Cercle cercle = new Cercle(p4, 5.2); + + Piletransformations trans = new Piletransformations(10); + trans.display(); + + segment.dessiner(trans); + cercle.dessiner(trans); + boolean rt = trans.empiler(p1); + trans.display(); + segment.dessiner(trans); + cercle.dessiner(trans); + rt = trans.empiler(p2); + trans.display(); + segment.dessiner(trans); + cercle.dessiner(trans); + + segment.deplacer(p1); + cercle.deplacer(p1); + segment.dessiner(trans); + cercle.dessiner(trans); + } } diff --git a/TP2/Makefile b/TP2/Makefile index b8e3c32..40b372f 100644 --- a/TP2/Makefile +++ b/TP2/Makefile @@ -49,6 +49,7 @@ CLASSES = \ Point.java \ Segment.java \ Cercle.java \ + Piletransformations.java \ Main.java # diff --git a/TP2/Piletransformations.java b/TP2/Piletransformations.java new file mode 100644 index 0000000..6d25095 --- /dev/null +++ b/TP2/Piletransformations.java @@ -0,0 +1,60 @@ + +class Piletransformations { + private Point[] transformations; + private int currentTransformation; + + Piletransformations(int size) { + transformations = new Point[size]; + for (int i = 0; i < transformations.length; i++) { + transformations[i] = new Point(0, 0); + } + currentTransformation = 0; + } + + public Point getCurrentTransformation() { + if (isEmpty()) { + return transformations[currentTransformation]; + } else { + return transformations[currentTransformation - 1]; + } + } + + private boolean isEmpty() { + return (currentTransformation == 0); + } + + private boolean isFull() { + return (currentTransformation >= transformations.length); + } + + public boolean empiler(Point p) { + boolean rtVal = false; + if (isEmpty()) { + transformations[currentTransformation] = p; + currentTransformation++; + rtVal = true; + } else if (!isFull()) { + transformations[currentTransformation] = transformations[currentTransformation - 1].additionner(p); + currentTransformation++; + rtVal = true; + } + return rtVal; + } + + public Point depiler() { + if(!isEmpty()) { + currentTransformation--; + return transformations[currentTransformation]; + } else { + return transformations[currentTransformation]; + } + } + + public void display() { + System.out.println("----"); + for (int i = 0; i < transformations.length; i++) { + System.out.println(transformations[i].toString()); + } + } + +} diff --git a/TP2/Point.java b/TP2/Point.java index e949acc..049c6a2 100644 --- a/TP2/Point.java +++ b/TP2/Point.java @@ -4,6 +4,9 @@ class Point { private double y; Point() { + /* + * FIXME: init to (0,0)? + */ } Point (double x, double y) { @@ -27,8 +30,9 @@ class Point { return y; } - public Point additionner(Point p1) { - return new Point(p1.getX() + getX(), p1.getY() + getX()); + public Point additionner(Point p) { + Point pNew = new Point(p.getX() + getX(), p.getY() + getY()); + return pNew; } public String toString() { diff --git a/TP2/Segment.java b/TP2/Segment.java index 941820c..e169172 100644 --- a/TP2/Segment.java +++ b/TP2/Segment.java @@ -13,12 +13,17 @@ class Segment { pDest = pD; } - void dessiner(Piletransformations pile) { - + public void dessiner(Piletransformations pile) { + String className = this.getClass().getSimpleName(); + Point pTrans = pile.getCurrentTransformation(); + Point pOriTrans = pOri.additionner(pTrans); + Point pDestTrans = pDest.additionner(pTrans); + System.out.println(className + " " + pOri.toString()+ "->" + pOriTrans.toString() + " " + pDest.toString() + "->" + pDestTrans.toString()); } - void deplacer(Point p) { - + public void deplacer(Point p) { + pOri = pOri.additionner(p); + pDest = pDest.additionner(p); } } -- 2.34.1