package sk.paz1a.practicals.task2;
import java.awt.Color;
import sk.upjs.jpaz2.*;
public class SmartTurtle extends Turtle {
public void chain(int beadCount, double beadRadius) {
this.setFillColor(Color.BLUE);
this.penUp();
for (int i = 0; i < beadCount; i++) {
this.dot(beadRadius);
this.step(2 * beadRadius);
}
this.penDown();
}
public void ngon(int n, double sideLength) {
for (int i = 0; i < n; i++) {
this.step(sideLength);
this.turn(360.0 / n);
}
}
public void dashedLine(int n, double segmentLength) {
for (int i = 0; i < n; i++) {
this.step(segmentLength / 4);
this.penUp();
this.step(segmentLength / 2);
this.penDown();
this.step(segmentLength / 4);
}
}
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 rameno) {
this.setPenColor(Color.YELLOW);
for (int i = 0; i < n; i++) {
this.isosceles(rameno, 360.0 / (2 * n));
this.turn(360 / n);
}
}
public void rgbChain(int beadCount, double beadRadius) {
this.penUp();
for (int i = 0; i < beadCount; i++) {
if (i % 3 == 0) {
this.setFillColor(Color.blue);
}
if (i % 3 == 1) {
this.setFillColor(Color.red);
}
if (i % 3 == 2) {
this.setFillColor(Color.green);
}
this.dot(beadRadius);
this.step(2 * beadRadius);
}
this.penDown();
}
public void circleWalk(int stepCount, double radius) {
// 1.zapamatat suradnice
double startX = this.getX();
double startY = this.getY();
// double vzdialenost;
// 2.nahodna pochodzka
for (int i = 0; i < stepCount; i++) {
// nahodne otocenie medzi 0 az 360 stupnov
this.turn(Math.random() * 360);
JPAZUtilities.delay(1);
this.step(5);
/*
* vzdialenost = Math.sqrt((this.getX()-startX)*(this.getX()-startX)
* +(this.getY()-startY)*(this.getY()-startY));
*/
// ak je mimo definovanej oblasti, tak krok spat
if (this.distanceTo(startX, startY) > radius) {
this.step(-5);
JPAZUtilities.delay(1);
}
}
// 3.navrat na podvodne suradnice
this.setPosition(startX, startY);
JPAZUtilities.delay(1);
}
public void squareWalk(int stepCount, double sideLength) {
// 1.zapamatat suradnice
double startX = this.getX();
double startY = this.getY();
boolean vPaseX;
boolean vPaseY;
// 2.nahodna pochodzka
for (int i = 0; i < stepCount; i++) {
// nahodne otocenie medzi 0 az 360 stupnov
this.turn(Math.random() * 360);
this.step(5);
// ak je mimo definovanej oblasti stvorec, tak krok spat
vPaseX = (this.getX() <= startX + sideLength / 2) && (this.getX() >= startX - sideLength / 2);
vPaseY = (this.getY() <= startY + sideLength / 2) && (this.getY() >= startY - sideLength / 2);
if (!(vPaseX && vPaseY)) {
this.step(-5);
JPAZUtilities.delay(1);
}
}
// 3.navrat na podvodne suradnice
this.setPosition(startX, startY);
}
public void tripleCircleWalk(int stepCount, double radius) {
// 1.zapamatat suradnice
double startX = this.getX();
double startY = this.getY();
// 2.nahodna pochodzka
for (int i = 0; i < stepCount; i++) {
// nahodne otocenie medzi 0 az 360 stupnov
this.turn(Math.random() * 360);
// podmienky, kde sa nachadza a nastavenie farby pera
if (this.distanceTo(startX, startY) < radius / 3)
this.penUp();
if ((this.distanceTo(startX, startY) >= radius / 3) && (this.distanceTo(startX, startY) < 2 * radius / 3)) {
this.penDown();
this.setPenColor(Color.BLUE);
}
if ((this.distanceTo(startX, startY) >= 2 * radius / 3) && (this.distanceTo(startX, startY) < radius)) {
this.penDown();
this.setPenColor(Color.RED);
}
this.step(5);
// ak je mimo definovanej oblasti, tak krok spat
if (this.distanceTo(startX, startY) > radius) {
this.step(-5);
JPAZUtilities.delay(1);
}
}
// 3.navrat na podvodne suradnice
this.setPosition(startX, startY);
JPAZUtilities.delay(1);
}
}