Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl.edu.pg.eti.ksg.po.lab1.transformacje;
- /**
- *
- * @author student
- */
- public class PlEduPgEtiKsgPoLab1Transformacje {
- public static void main(String[] args) {
- try{
- Punkt p1= new Punkt(2,2);
- System.out.println(p1);
- Transformacja tr=new Translacja(5,6);
- System.out.println(tr);
- p1=tr.transformuj(p1);
- System.out.println(p1);
- Transformacja trs=new Skalowanie(5,2);
- System.out.println(trs);
- p1=trs.transformuj(p1);
- System.out.println(p1);
- Transformacja tro=new Obrot(3);
- System.out.println(tro);
- p1=tro.transformuj(p1);
- System.out.println(p1);
- Transformacja tr1 = tro.getTransformacjaOdwrotna();
- System.out.println(tr1);
- p1=tr1.transformuj(p1);
- System.out.println(p1);
- tr1=trs.getTransformacjaOdwrotna();
- System.out.println(tr1);
- p1=tr1.transformuj(p1);
- System.out.println(p1);
- tr1=tr.getTransformacjaOdwrotna();
- System.out.println(tr);
- p1=tr1.transformuj(p1);
- System.out.println(p1);
- }
- catch(BrakTransformacjiOdwrotnejException ex)
- {
- ex.printStackTrace();
- }
- System.out.println();
- }
- }
- ////////////////////////////////////////////////////////////
- *
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl.edu.pg.eti.ksg.po.lab1.transformacje;
- /**
- *
- * @author student
- */
- public class Punkt {
- private final double x,y;
- public Punkt(double x, double y) {
- this.x=x;
- this.y=y;
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- @Override
- public boolean equals(Object obj) {
- Punkt other=(Punkt)obj;
- if(x==other.getX()&&y==other.getY())
- {
- return true;
- }
- return false;
- }
- @Override
- public int hashCode() {
- return 59*Double.hashCode(x)+7+56*Double.hashCode(y)+7;
- }
- @Override
- public String toString() {
- return "Instancja klasy Punkt o współrzędnych: "+x+","+y;
- }
- public static final Punkt O = new Punkt(0, 0);
- /**
- * Punkt (1,0) w układzie współrzędnych
- */
- public static final Punkt E_X = new Punkt(1, 0);
- /**
- * Punkt (0,1) w układzie współrzędnych
- */
- public static final Punkt E_Y = new Punkt(0, 1);
- }
- //////////////////////////////////////////////////////
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl.edu.pg.eti.ksg.po.lab1.transformacje;
- /**
- *
- * @author student
- */
- public class BrakTransformacjiOdwrotnejException extends Exception
- {
- public BrakTransformacjiOdwrotnejException()
- {
- //Niejawne wywołanie konstruktora klasy nadrzędnej
- //super();
- }
- /*
- * Konstruktor przyjmujący jako parametr tekst opisujący błąd
- */
- public BrakTransformacjiOdwrotnejException(String message) {
- super(message);
- }
- /*
- * Konstruktor przyjmujący jako parametr referencje do innego
- * wyjątku, który spowodował błąd
- */
- public BrakTransformacjiOdwrotnejException(Throwable cause) {
- super(cause);
- }
- /*
- * Konstruktor przyjmujący jako parametr tekst opisujący błąd
- * oraz wyjątek, który spowodował błąd.
- */
- public BrakTransformacjiOdwrotnejException(String message,
- Throwable cause) {
- super(message, cause);
- }
- }
- /////////////////////////////////////
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl.edu.pg.eti.ksg.po.lab1.transformacje;
- /**
- *
- * @author student
- */
- public interface Transformacja {
- Punkt transformuj(Punkt p);
- Transformacja getTransformacjaOdwrotna()
- throws BrakTransformacjiOdwrotnejException;
- }
- /////////////////////////////////////
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl.edu.pg.eti.ksg.po.lab1.transformacje;
- /**
- *
- * @author student
- */
- public class Translacja implements Transformacja{
- private final double dX,dY;
- public Translacja(double dX, double dY) {
- this.dX = dX;
- this.dY = dY;
- }
- @Override
- public Transformacja getTransformacjaOdwrotna() {
- return new Translacja(-dX, -dY);
- }
- @Override
- public Punkt transformuj(Punkt p) {
- return new Punkt(p.getX() + dX, p.getY() + dY);
- }
- public double getdX() {
- return dX;
- }
- public double getdY() {
- return dY;
- }
- @Override
- public String toString() {
- return "Translacja o wektor ("+dX+","+dY+")";
- }
- }
- //////////////////////////////////////////////
- *
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl.edu.pg.eti.ksg.po.lab1.transformacje;
- /**
- *
- * @author student
- */
- public class Skalowanie implements Transformacja{
- private final double skalaX,skalaY;
- public Skalowanie(double skalaX, double skalaY) {
- this.skalaX = skalaX;
- this.skalaY = skalaY;
- }
- @Override
- public Transformacja getTransformacjaOdwrotna() throws
- BrakTransformacjiOdwrotnejException {
- if(skalaX == 0 || skalaY == 0)
- throw new BrakTransformacjiOdwrotnejException("Brak transformacji odwrotnej. Przynajmniej jeden z czynników skalowania jest równy 0.");
- return new Skalowanie(1/skalaX,1/skalaY);
- }
- @Override
- public Punkt transformuj(Punkt p) {
- return new Punkt(skalaX * p.getX(), skalaY * p.getY());
- }
- public double getSkalaX() {
- return skalaX;
- }
- public double getSkalaY() {
- return skalaY;
- }
- @Override
- public String toString() {
- return "Skalowanie "+skalaX+"x oraz "+skalaY+"y";
- }
- }
- ///////////////////////////////////////////////////////////
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl.edu.pg.eti.ksg.po.lab1.transformacje;
- /**
- *
- * @author student
- */
- public class Obrot implements Transformacja {
- private final double angle;
- public Obrot(double angle)
- {
- this.angle=angle;
- }
- @Override
- public Transformacja getTransformacjaOdwrotna()
- {
- return new Obrot(-angle);
- }
- @Override
- public Punkt transformuj(Punkt p)
- {
- double x=p.getX()*Math.cos(angle)-p.getY()*Math.sin(angle);
- double y=p.getX()*Math.sin(angle)+p.getY()*Math.cos(angle);
- return new Punkt(x,y);
- }
- @Override
- public String toString()
- {
- return "Obrót("+angle+")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement