Add the transformations stack code.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 14 Feb 2018 10:41:53 +0000 (11:41 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 14 Feb 2018 10:41:53 +0000 (11:41 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TP2/Cercle.java
TP2/Main.java
TP2/Makefile
TP2/Piletransformations.java [new file with mode: 0644]
TP2/Point.java
TP2/Segment.java

index 4bf743ffdee026a698b25de263e8df57eaaaf3b6..fe17b443d7d1d937ad322234b9cbc63a25030588 100644 (file)
@@ -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);
     }
-    
+
 }
index 0a031cd8c810aef34fb787e95a5c13ec9da94afe..e05593a846aa509c54e65de24431230b355212ea 100644 (file)
@@ -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);
+
     }
 
 }
index b8e3c3238c7635bd6e4036744b1356ff4406e32e..40b372fe5e8960cc597bab14cfd4a7b70b23a0ac 100644 (file)
@@ -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 (file)
index 0000000..6d25095
--- /dev/null
@@ -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());
+        }
+    }
+
+}
index e949acc413fbe8f4947a04bd1fc3fdc3dc75df9b..049c6a20cb6b116fedc524cfee81843ae1e7f9ee 100644 (file)
@@ -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() {
index 941820c4ac728add80b4eaff85613f4c70a51221..e1691723aa7895e0d67ba8b834d4c3fa8f6af373 100644 (file)
@@ -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);
     }
 
 }