Riešenia (skupina C, 2. týždeň)

Streda

package sk.paz1a.practicals.task02;

import java.awt.Color;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        public void chain(int beadCount, double beadRadius) {
                this.penUp();
                this.setFillColor(Color.BLUE);

                for (int i = 0; i < beadCount; i++) {
                        this.dot(beadRadius);
                        this.step(beadRadius * 2);
                }

        }

        public void ngon(int n, double sideLength) {
                boolean jePolozene = this.isPenDown();
                this.penDown();


                for (int i = 0; i < n; i++) {
                        this.step(sideLength);
                        this.turn(360.0 / n);
                }


                if (jePolozene) {

                } else {
                        this.penUp();
                }

        }


        public void dashedLine(int n, double segmentLength) {
                boolean jePolozene = this.isPenDown();
                this.penDown();

                for (int i = 0; i < n; i++) {
                        this.step(segmentLength/4);
                        this.penUp();
                        this.step(segmentLength/2);
                        this.penDown();
                        this.step(segmentLength/4);
                }

                if (jePolozene) {

                } else {
                        this.penUp();
                }
        }

        public void isosceles(double legLength, double angle) {
        // Do premennych startX a startY si ulozime
        // poziciu korytnacky pri zavolani tejto metody
        double startX = this.getX();
        double startY = this.getY();
        // Do premennej natocenie si ulozime natocenie (smer)
        // korytnacky pri zavolani tejto metody
        double direction = this.getDirection();

        // Kreslenie
        this.turn(-angle / 2);
        this.step(legLength);

        // Sme v bode A, ulozime si teda x-ovu a y-ovu suradnicu
        // tohto bodu (pouzijeme ich neskor)

        // Vytvorime premennu xCoord na hodnoty typu double
        double xCoord;
        // Do premennej xCoord ulozime aktualnu x-ovu suradnicu
        // pozicie korytnacky - ziskame ju tak, ze sa "sami seba" spytame
        // na x-ovu suradnicu
        xCoord = this.getX();

        // Ako predosle, ale s y-ovou suradnicou. Kombinujeme vytvorenie
        // premennej aj inicializaciu hodnoty do jedneho prikazu
        double yCoord = this.getY();

        // Dalsie kreslenie
        this.step(-legLength);
        this.turn(angle);
        this.step(legLength);

        // Na zaver sa presunieme na ulozenu poziciu bodu A
        this.moveTo(xCoord, yCoord);

        // Korytnacku presunieme tam, kde bola na zaciatku, a natocime smerom,
        // akym bola natocena na zaciatku (pri volani tejto metody)
        this.setPosition(startX, startY);
        this.setDirection(direction);
}

        public void mill(int n, double length) {
                this.setPenColor(Color.YELLOW);
                double angle = 360/n;
                for(int i=0;i<n;i++) {
                        this.isosceles(length, angle/2);
                        this.turn(angle);
                }
        }


}
package sk.paz1a.practicals.task02;

import sk.upjs.jpaz2.*;

public class Launcher {

        public static void main(String[] args) {
                // create new "sandbox" - a place where turtles can live
                AnimatedWinPane sandbox = new AnimatedWinPane();

                // create new turtle and add it to the "sandbox"
                SmartTurtle franklin = new SmartTurtle();
                sandbox.add(franklin);

                // create new object inspector
                ObjectInspector oi = new ObjectInspector();
                // ask the inspector to inspect "franklin" and "sandbox"
                oi.inspect(franklin);
                oi.inspect(sandbox);

                // you can put other initialization commands here

        }
}

Štvrtok

package sk.paz1a.practicals.task02;

import java.awt.Color;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        public void chain(int beadCount, double beadRadius) {
                this.penUp();
                this.setFillColor(Color.BLUE);

                for (int i = 0; i < beadCount; i++) {
                        this.dot(beadRadius);
                        this.step(beadRadius * 2);
                }

        }

        public void ngon(int n, double sideLength) {
                boolean jePolozene = this.isPenDown();
                this.penDown();

                for (int i = 0; i < n; i++) {
                        this.step(sideLength);
                        this.turn(360.0 / n);
                }

                if (jePolozene) {

                } else {
                        this.penUp();
                }

        }

        public void dashedLine(int n, double segmentLength) {
                boolean jePolozene = this.isPenDown();
                this.penDown();

                for (int i = 0; i < n; i++) {
                        this.step(segmentLength / 4);
                        this.penUp();
                        this.step(segmentLength / 2);
                        this.penDown();
                        this.step(segmentLength / 4);
                }

                if (jePolozene) {

                } else {
                        this.penUp();
                }
        }

        public void isosceles(double legLength, double angle) {
                // Do premennych startX a startY si ulozime
                // poziciu korytnacky pri zavolani tejto metody
                double startX = this.getX();
                double startY = this.getY();
                // Do premennej natocenie si ulozime natocenie (smer)
                // korytnacky pri zavolani tejto metody
                double direction = this.getDirection();

                // Kreslenie
                this.turn(-angle / 2);
                this.step(legLength);

                // Sme v bode A, ulozime si teda x-ovu a y-ovu suradnicu
                // tohto bodu (pouzijeme ich neskor)

                // Vytvorime premennu xCoord na hodnoty typu double
                double xCoord;
                // Do premennej xCoord ulozime aktualnu x-ovu suradnicu
                // pozicie korytnacky - ziskame ju tak, ze sa "sami seba" spytame
                // na x-ovu suradnicu
                xCoord = this.getX();

                // Ako predosle, ale s y-ovou suradnicou. Kombinujeme vytvorenie
                // premennej aj inicializaciu hodnoty do jedneho prikazu
                double yCoord = this.getY();

                // Dalsie kreslenie
                this.step(-legLength);
                this.turn(angle);
                this.step(legLength);

                // Na zaver sa presunieme na ulozenu poziciu bodu A
                this.moveTo(xCoord, yCoord);

                // Korytnacku presunieme tam, kde bola na zaciatku, a natocime smerom,
                // akym bola natocena na zaciatku (pri volani tejto metody)
                this.setPosition(startX, startY);
                this.setDirection(direction);
        }

        public void mill(int n, double length) {
                this.setPenColor(Color.YELLOW);
                double angle = 360 / n;
                for (int i = 0; i < n; i++) {
                        this.isosceles(length, angle / 2);
                        this.turn(angle);
                }
        }

        public void RGBChain(int beadCount, double beadRadius) {
                this.penUp();

                int counter = 0;
                for (int i = 0; i < beadCount; i++) {
                        if (counter % 3 == 0) {
                                this.setFillColor(Color.RED);

                        }

                        if (counter % 3 == 1) {
                                this.setFillColor(Color.GREEN);

                        }

                        if (counter % 3 == 2) {
                                this.setFillColor(Color.BLUE);

                        }
                        counter++;

                        this.dot(beadRadius);
                        this.step(beadRadius * 2);
                }

        }

        public void circleWalk(int stepCount, double radius) {
                double startX = this.getX();
                double startY = this.getY();
                double startDir = this.getDirection();

                for (int i = 0; i < stepCount; i++) {
                        this.turn(Math.random() * 360);
                        this.step(5);

                        boolean jeMimoDefinovanejOblasti = this.distanceTo(startX, startY) > radius;
                        if (jeMimoDefinovanejOblasti) {
                                this.step(-5);
                        }
                }
                this.setPosition(startX, startY);
                this.setDirection(startDir);
        }

        public void tripleCircleWalk(int stepCount, double radius) {
                double startX = this.getX();
                double startY = this.getY();
                double startDir = this.getDirection();

                for (int i = 0; i < stepCount; i++) {

                        if (this.distanceTo(startX, startY) < radius / 3) {
                                this.penUp();
                        } else {
                                this.penDown();
                                if (this.distanceTo(startX, startY) < 2 * radius / 3) {
                                        this.setPenColor(Color.BLUE);

                                } else {
                                        this.setPenColor(Color.RED);
                                }
                        }

                        this.turn(Math.random() * 360);
                        this.step(5);

                        boolean jeMimoDefinovanejOblasti = this.distanceTo(startX, startY) > radius;
                        if (jeMimoDefinovanejOblasti) {
                                this.step(-5);
                        }
                }
                this.setPosition(startX, startY);
                this.setDirection(startDir);
        }

        public void squareWalk(int stepCount, double sideLength) {
                double startX = this.getX();
                double startY = this.getY();
                double startDir = this.getDirection();

                for (int i = 0; i < stepCount; i++) {
                        this.turn(Math.random() * 360);
                        this.step(5);
                        boolean jeMimoX = (this.getX()>startX+sideLength/2) || (this.getX()<startX-sideLength/2);
                        boolean jeMimoY = (this.getY()>startY+sideLength/2) || (this.getY()<startY-sideLength/2);
                        boolean jeMimoDefinovanejOblasti = jeMimoX || jeMimoY;
                        if (jeMimoDefinovanejOblasti) {
                                this.step(-5);
                        }
                }
                this.setPosition(startX, startY);
                this.setDirection(startDir);
        }

        public void directionWalk(int stepCount, double radius) {
                double startX = this.getX();
                double startY = this.getY();
                double startDir = this.getDirection();

                for (int i = 0; i < stepCount; i++) {
                        int direction= (int) (Math.random()*4) * 90;

                        this.turn(direction);
                        this.step(5);

                        boolean jeMimoDefinovanejOblasti = this.distanceTo(startX, startY) > radius;
                        if (jeMimoDefinovanejOblasti) {
                                this.step(-5);
                        }
                }
                this.setPosition(startX, startY);
                this.setDirection(startDir);
        }

        public void rainbowWalk(int stepCount, double radius) {
                double startX = this.getX();
                double startY = this.getY();
                double startDir = this.getDirection();

                for (int i = 0; i < stepCount; i++) {
                        int r= (int) (Math.random()*256);
                        int g = (int) (Math.random()*256);
                        int b = (int) (Math.random()*256);
                        this.setPenColor(new Color(r, g, b));
                        this.turn(Math.random() * 360);
                        this.step(5);

                        boolean jeMimoDefinovanejOblasti = this.distanceTo(startX, startY) > radius;
                        if (jeMimoDefinovanejOblasti) {
                                this.step(-5);
                        }
                }
                this.setPosition(startX, startY);
                this.setDirection(startDir);
        }

}
package sk.paz1a.practicals.task02;

import sk.upjs.jpaz2.*;

public class Launcher {

        public static void main(String[] args) {
                // create new "sandbox" - a place where turtles can live
                AnimatedWinPane sandbox = new AnimatedWinPane();

                // create new turtle and add it to the "sandbox"
                SmartTurtle franklin = new SmartTurtle();
                sandbox.add(franklin);

                // create new object inspector
                ObjectInspector oi = new ObjectInspector();
                // ask the inspector to inspect "franklin" and "sandbox"
                oi.inspect(franklin);
                oi.inspect(sandbox);

                // you can put other initialization commands here

        }
}