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

Praktické cvičenie 2.10.2012

Spustac.java

import sk.upjs.jpaz2.*;


public class Spustac {

        /**
         * @param args
         */

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                WinPane plocha = new WinPane();
                ChytraTurtle albert = new ChytraTurtle();
                plocha.add(albert);
                albert.center();

                //albert.retiazka(12, 8);
                //albert.nUholnik(5, 40);
                //albert.prerusovana(5, 30);
                //albert.mlyn(7, 80);
                albert.retiazkaRGB(10, 7);
        }

}

ChytraTurtle.java

import java.awt.Color;

import sk.upjs.jpaz2.Turtle;

public class ChytraTurtle extends Turtle {

        public void retiazka(int pocetKoralov, double polomerKoralu) {
                this.turn(90);
                this.setFillColor(Color.blue);
                this.setPenColor(Color.blue);

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

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

        public void nUholnik(int n, double dlzkaStrany) {
                for (int i = 0; i < n; i++) {
                        this.step(dlzkaStrany);
                        this.turn(360.0 / n);
                }
        }

        public void prerusovana(int n, double dlzkaUseku) {
                this.turn(90);
                for (int i = 0; i < n; i++) {
                        this.step(dlzkaUseku / 4);
                        this.penUp();
                        this.step(dlzkaUseku / 2);
                        this.penDown();
                        this.step(dlzkaUseku / 4);
                }
        }

        public void rovnoramenny(double uhol, double rameno) {
                double startX = this.getX();
                double startY = this.getY();
                double natocenie = this.getDirection();
                this.turn(-uhol / 2);
                this.step(rameno);
                double xPoziciaA = this.getX();
                double yPoziciaA = this.getY();
                this.step(-rameno);
                this.turn(uhol);
                this.step(rameno);
                this.moveTo(xPoziciaA, yPoziciaA);
                this.setPosition(startX, startY);
                this.setDirection(natocenie);
        }

        public void mlyn(int pocetLopatiek, double rameno) {
                // najprv si nastavim farbu
                this.setPenColor(Color.orange);
                // albert ma vykonat metodu rovnoramenny tolkokrat, kolko je pocet
                // lopatiek
                for (int i = 0; i < pocetLopatiek; i++) {
                        this.rovnoramenny(30, rameno);
                        // uhol musi byt realne cislo, preto musime zadat 360.0
                        this.turn(360.0 / pocetLopatiek);
                }
        }

        public void retiazkaRGB(int pocetKoralov, double polomerKoralu) {
                this.penUp();
                for (int i = 0; i < pocetKoralov; i++) {
                        if (i % 3 == 1)
                                this.setFillColor(Color.red);
                        if (i % 3 == 2)
                                this.setFillColor(Color.green);
                        if (i % 3 == 0)
                                this.setFillColor(Color.blue);

                        this.dot(polomerKoralu);
                        this.step(polomerKoralu * 2);

                }

        }
}

Teoretické cvičenie 5.10.2012

ChytraTurtle.java


import java.awt.Color;
import java.awt.Point;

import sk.upjs.jpaz2.Turtle;

public class ChytraTurtle extends Turtle {

        public void vystredenyStvorec(double dlzkaStrany) {

                this.penUp();
                this.turn(-90);
                this.step(dlzkaStrany / 2);
                this.turn(-90);
                this.step(dlzkaStrany / 2);
                this.turn(180);
                this.penDown();

                for (int i = 1; i <= 4; i++) {
                        this.step(dlzkaStrany);
                        this.turn(90);
                }

                this.penUp();
                this.step(dlzkaStrany / 2);
                this.turn(90);
                this.step(dlzkaStrany / 2);
                this.turn(-90);
                this.penDown();
        }

        public void ciarkaBodkaCiarka(double dlzka) {
                this.step(dlzka / 4);
                this.penUp();
                this.step(dlzka / 4);
                this.setFillColor(Color.orange);
                this.dot(dlzka / 8);
                this.step(dlzka / 4);
                this.penDown();
                this.step(dlzka / 4);
        }

        public void slniecko(double polomer) {

                this.setPenColor(Color.orange);
                for (int i = 0; i < 36; i++) {
                        this.step(2 * polomer);
                        this.step(-2 * polomer);
                        this.turn(10);

                }

                this.dot(polomer);

        }

        public void stvorec(double dlzkaStrany) {
                for (int i = 0; i < 4; i++) {
                        this.step(dlzkaStrany);
                        this.turn(90);
                }
        }

        public void obrazec(double dlzkaStrany) {
                this.setPenColor(Color.blue);
                this.setPenWidth(2);
                for (int i = 0; i < 10; i++) {
                        this.stvorec(dlzkaStrany);
                        this.turn(90);
                        this.step(dlzkaStrany / 2);
                        this.turn(36);
                        this.turn(-90);
                }
        }

        public void mriezka(double rozmer) {
                this.setPosition(new Point(10, 10));
                this.turn(90);
                for (int i = 0; i < 9; i++) {
                        this.step(rozmer * 8);
                        this.setY(10 + (i + 1) * rozmer);
                        this.setX(10);
                }
                this.setPosition(new Point(10, 10));
                this.turn(90);
                for (int i = 0; i < 9; i++) {
                        this.step(rozmer * 8);
                        this.setX(10 + (i + 1) * rozmer);
                        this.setY(10);

                }
        }

        public void vyplnenyStvorec(double strana) {
                this.openPolygon();
                for (int i = 0; i < 4; i++) {
                        this.step(strana);
                        this.turn(90);
                }
                this.closePolygon();
        }

        public void hviezda4(double dlzkaCipu, double uhol) {
                this.setFillColor(Color.yellow);
                this.openPolygon();
                this.setPenColor(Color.red);
                this.setPenWidth(4);
                for (int i = 0; i < 4; i++) {
                        this.step(dlzkaCipu);
                        this.turn(180 - uhol);
                        this.step(dlzkaCipu);
                        this.turn(-(90 - uhol));
                }
                this.closePolygon();
        }

        public void diamant(double uhol, double dlzkaStrany) {

                for (int i = 0; i < 3; i++) {
                        this.turn(-uhol);
                        this.step(dlzkaStrany);
                        this.turn(2 * uhol);
                        this.step(dlzkaStrany);
                        this.turn(180 - 2 * uhol);
                        this.step(dlzkaStrany);
                        this.turn(2 * uhol);
                        this.step(dlzkaStrany);
                        this.turn(300 - uhol);

                }
        }

        public void pochodzkaKruh(int pocetKrokov, double polomer) {
                double xStart = getX();
                double yStart = getY();
                for (int i = 0; i < pocetKrokov; i++) {
                        this.turn(Math.random() * 360);
                        this.step(5);

                        if (this.distanceTo(xStart, yStart) > polomer) {
                                this.step(-5);
                        }
                }
        }

        public void pochodzkaTrojKruh(int pocetKrokov, double polomer) {
                double xStart = getX();
                double yStart = getY();
                this.penUp();
                for (int i = 0; i < pocetKrokov; i++) {
                        this.turn(Math.random() * 360);
                        this.step(5);

                        if (this.distanceTo(xStart, yStart) > polomer) {
                                this.step(-5);
                        }
                        if (this.distanceTo(xStart, yStart) < polomer / 3) {
                                this.penUp();
                        } else {
                                penDown();
                        }
                        if ((this.distanceTo(xStart, yStart) < (2 * polomer / 3))
                                        && (this.distanceTo(xStart, yStart) > polomer / 3)) {
                                this.setPenColor(Color.blue);
                        }
                        if (this.distanceTo(xStart, yStart) > (2 * polomer / 3)) {
                                this.setPenColor(Color.red);
                        }
                }
        }

        public void pochodzkaStvorec(int pocetKrokov, double strana) {
                double xL = this.getX() - strana / 2;
                double xP = this.getX() + strana / 2;
                double yH = this.getY() - strana / 2;
                double yD = this.getY() + strana / 2;
                for (int i = 0; i < pocetKrokov; i++) {
                        this.turn(Math.random() * 360);
                        this.step(5);
                        this.setPenColor(new Color((int)(Math.random()*256),(int)( Math.random()*256),(int)(Math.random()*256)));

                        if (this.getX() < xL) {
                                this.step(-5);
                        }
                        if (this.getX() > xP) {
                                this.step(-5);
                        }
                        if (this.getY() < yH) {
                                this.step(-5);
                        }
                        if (this.getY() > yD) {
                                this.step(-5);
                        }
                }

        }
        public void vymenAB(int a, int b) {
                a = a+b;
                b = a-b;
                a = a-b;
                System.out.println(a);
                System.out.println(b);
        }
}