A2

Streda

package PAZ1a.task02;

import java.awt.Color;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        public void chain(int beadCount, double beadRadius) {
                this.turn(90);
                this.penUp();
                this.setFillColor(Color.blue);
                for (int i = 0; i < beadCount; i++) {
                        this.dot(beadRadius);
                        this.step(2 * beadRadius);
                }
                this.penDown();

        }

        public void dashedLine(int n, double segmentLength) {
                for (int i = 0; i < n; i++) {
                        this.step(1 / 4 * segmentLength);
                        // this.step(segmentLength/4);
                        this.penUp();
                        this.step(segmentLength / 2);
                        this.penDown();
                        this.step(segmentLength / 4);
                }
        }

        /**
         * Nakresli rovnoramenny trojuholnik
         *
         * @param legLength dlzka ramena
         * @param angle     uhol, ktory zvieraju ramena
         */

        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 count, double size) {
                for (int i = 0; i < count; i++) {
                        // kreslim trojuholnik
                        this.isosceles(size, 360 / (2 * count));
                        // otocim sa o medzeru
                        this.turn(360 / count);
                }
        }

        public void rgbChain(int beadCount, double beadRadius) {
                this.turn(90);
                this.penUp();
                int count = 0;

                // this.setFillColor(Color.blue);
                for (int i = 0; i < beadCount; i++) {
                        int farba = count % 3;
                        if (farba == 0) {
                                this.setFillColor(Color.red);
                        }
                        if (farba == 1) {
                                this.setFillColor(Color.green);
                        }
                        if (farba == 2) {
                                this.setFillColor(Color.blue);
                        }
                        this.dot(beadRadius);
                        count = count + 1;
                        this.step(2 * beadRadius);
                }
                this.penDown();
        }

        public void randomWalk(int stepCount) {
                // ulozim suradnice a natocenie
                double startX = this.getX();
                double startY = this.getY();
                double angle = this.getDirection();

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

                boolean jeMimoDefinovanejOblasti = false;
                if (jeMimoDefinovanejOblasti) {
                        this.step(-5);
                }
        }

                // vratim korytnacku do stavu ako bola na zaciatku
        }

}

Štvrtok

package paz1a.task02;

import java.awt.Color;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        public void circleWalk(int stepCount, double radius) {

                double startX = this.getX();
                double startY = this.getY();
                double startDirection = this.getDirection();

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

                        // otocime sa o nahodny uhol <0,360)
                        this.turn(Math.random() * 360);

                        // spravime krok o velkost 5
                        this.step(5);

                        // do premennej si ulozime odpoved na otazku ci
                        // vzdialenost od pociatocnej pozicie korytnacky (startX,
                        // startY) je viac ako zadany polomer
                        boolean jeMimoDefinovanejOblasti = this.distanceTo(startX, startY) > radius;

                        // ak ano, korytnacka chod spat o velkost -5
                        if (jeMimoDefinovanejOblasti) {
                                this.step(-5);
                        }
                }

                this.setPosition(startX, startY);
                this.setDirection(startDirection);

        }

        public void tripleCircleWalk(int stepCount, double radius) {
                // ulozime si startovaciu poziciu korytnacky
                double startX = this.getX();
                double startY = this.getY();
                double startDirection = this.getDirection();

                for (int i = 0; i < stepCount; i++) {
                        // otocime sa o nahodny uhol z intervalu <0, 360)
                        this.turn(Math.random() * 360);

                        // je vzdialenost od pociatocnej pozicie <= radius/3
                        boolean bielaCast = this.distanceTo(startX, startY) <= (radius / 3);

                        // ak ano, zdvihni pero - nechceme kreslit
                        if (bielaCast) {
                                this.penUp();
                        } else {
                                // ak nie, daj dole - kreslime
                                this.penDown();
                        }

                        boolean modraCast = this.distanceTo(startX, startY) < (2 * radius / 3);

                        if (modraCast) {
                                this.setPenColor(Color.blue);
                        } else {
                                this.setPenColor(Color.red);
                        }

                        this.step(5);

                        // do premennej si ulozime odpoved na otazku ci
                        // vzdialenost od pociatocnej pozicie korytnacky (startX,
                        // startY) je viac ako zadany polomer
                        boolean jeMimoDefinovanejOblasti = this.distanceTo(startX, startY) > radius;

                        // ak ano, korytnacka chod spat o velkost -5
                        if (jeMimoDefinovanejOblasti) {
                                this.step(-5);
                        }

                }

                this.setPosition(startX, startY);
                this.setDirection(startDirection);

        }

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

                for (int i = 0; i < stepCount; i++) {
                        // otocime sa o nahodny uhol z intervalu <0, 360)
                        this.turn(Math.random() * 360);
                        this.step(5);

                        boolean somVlavo = this.getX() < (startX - (sideLength / 2));
                        boolean somVpravo = this.getX() > (startX + (sideLength / 2));
                        boolean somNad = this.getY() < (startY - (sideLength / 2));
                        boolean somPod = this.getY() > (startY + (sideLength / 2));

                        boolean jeMimoDefinovanejOblasti = ((somVlavo || somVpravo) || (somNad || somPod));
                        if (jeMimoDefinovanejOblasti) {
                                this.step(-5);
                        }
                }
                this.setPosition(startX, startY);
                this.setDirection(startDirection);

        }

}
package paz1a.task02;

import java.awt.Color;

import sk.upjs.jpaz2.*;

public class Launcher {

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

                // 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
//              franklin.tripleCircleWalk(1000, 100);
//              franklin.squareWalk(10000, 50);

//              int a = 5;
//              int b = 8;
//              int c = 0;
//              c = a;
//              a = b;
//              b = c;
//
//              double realne = 3.14;
//              int cele = (int) realne;
//              // cele by bolo 3
//              int cele2 = (int) (realne * 8.3);
//              // cele2
//              int cele3 = 3 + (int) (realne / 8.3);

                int cervenaZlozka = (int) (Math.random() * 256);
                int zelenaZlozka = (int) (Math.random() * 256);
                int modraZlozka = (int) (Math.random() * 256);

                franklin.setPenColor(new Color(cervenaZlozka, zelenaZlozka, modraZlozka));
                franklin.squareWalk(10000, 100);

        }
}