Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package pl.edu.pg.eti.ksg.po.lab1.transformacje;
  7.  
  8. /**
  9. *
  10. * @author student
  11. */
  12. public class PlEduPgEtiKsgPoLab1Transformacje {
  13.  
  14.  
  15. public static void main(String[] args) {
  16. try{
  17. Punkt p1= new Punkt(2,2);
  18. System.out.println(p1);
  19.  
  20. Transformacja tr=new Translacja(5,6);
  21. System.out.println(tr);
  22.  
  23. p1=tr.transformuj(p1);
  24. System.out.println(p1);
  25.  
  26. Transformacja trs=new Skalowanie(5,2);
  27. System.out.println(trs);
  28.  
  29. p1=trs.transformuj(p1);
  30. System.out.println(p1);
  31.  
  32. Transformacja tro=new Obrot(3);
  33. System.out.println(tro);
  34.  
  35. p1=tro.transformuj(p1);
  36. System.out.println(p1);
  37.  
  38. Transformacja tr1 = tro.getTransformacjaOdwrotna();
  39. System.out.println(tr1);
  40.  
  41. p1=tr1.transformuj(p1);
  42. System.out.println(p1);
  43.  
  44. tr1=trs.getTransformacjaOdwrotna();
  45. System.out.println(tr1);
  46.  
  47. p1=tr1.transformuj(p1);
  48. System.out.println(p1);
  49.  
  50. tr1=tr.getTransformacjaOdwrotna();
  51. System.out.println(tr);
  52.  
  53. p1=tr1.transformuj(p1);
  54. System.out.println(p1);
  55.  
  56. }
  57. catch(BrakTransformacjiOdwrotnejException ex)
  58. {
  59. ex.printStackTrace();
  60. }
  61. System.out.println();
  62. }
  63.  
  64. }
  65.  
  66.  
  67. ////////////////////////////////////////////////////////////
  68. *
  69. * To change this license header, choose License Headers in Project Properties.
  70. * To change this template file, choose Tools | Templates
  71. * and open the template in the editor.
  72. */
  73. package pl.edu.pg.eti.ksg.po.lab1.transformacje;
  74.  
  75. /**
  76. *
  77. * @author student
  78. */
  79. public class Punkt {
  80. private final double x,y;
  81. public Punkt(double x, double y) {
  82. this.x=x;
  83. this.y=y;
  84. }
  85. public double getX() {
  86. return x;
  87. }
  88. public double getY() {
  89. return y;
  90. }
  91. @Override
  92. public boolean equals(Object obj) {
  93. Punkt other=(Punkt)obj;
  94. if(x==other.getX()&&y==other.getY())
  95. {
  96. return true;
  97. }
  98. return false;
  99. }
  100. @Override
  101. public int hashCode() {
  102.  
  103. return 59*Double.hashCode(x)+7+56*Double.hashCode(y)+7;
  104.  
  105. }
  106. @Override
  107. public String toString() {
  108. return "Instancja klasy Punkt o współrzędnych: "+x+","+y;
  109. }
  110. public static final Punkt O = new Punkt(0, 0);
  111. /**
  112. * Punkt (1,0) w układzie współrzędnych
  113. */
  114. public static final Punkt E_X = new Punkt(1, 0);
  115.  
  116. /**
  117. * Punkt (0,1) w układzie współrzędnych
  118. */
  119. public static final Punkt E_Y = new Punkt(0, 1);
  120. }
  121.  
  122. //////////////////////////////////////////////////////
  123. /*
  124. * To change this license header, choose License Headers in Project Properties.
  125. * To change this template file, choose Tools | Templates
  126. * and open the template in the editor.
  127. */
  128. package pl.edu.pg.eti.ksg.po.lab1.transformacje;
  129.  
  130. /**
  131. *
  132. * @author student
  133. */
  134. public class BrakTransformacjiOdwrotnejException extends Exception
  135. {
  136. public BrakTransformacjiOdwrotnejException()
  137. {
  138. //Niejawne wywołanie konstruktora klasy nadrzędnej
  139. //super();
  140. }
  141. /*
  142. * Konstruktor przyjmujący jako parametr tekst opisujący błąd
  143. */
  144. public BrakTransformacjiOdwrotnejException(String message) {
  145. super(message);
  146. }
  147. /*
  148. * Konstruktor przyjmujący jako parametr referencje do innego
  149. * wyjątku, który spowodował błąd
  150. */
  151. public BrakTransformacjiOdwrotnejException(Throwable cause) {
  152. super(cause);
  153. }
  154. /*
  155. * Konstruktor przyjmujący jako parametr tekst opisujący błąd
  156. * oraz wyjątek, który spowodował błąd.
  157. */
  158. public BrakTransformacjiOdwrotnejException(String message,
  159. Throwable cause) {
  160. super(message, cause);
  161. }
  162. }
  163. /////////////////////////////////////
  164. /*
  165. * To change this license header, choose License Headers in Project Properties.
  166. * To change this template file, choose Tools | Templates
  167. * and open the template in the editor.
  168. */
  169. package pl.edu.pg.eti.ksg.po.lab1.transformacje;
  170.  
  171. /**
  172. *
  173. * @author student
  174. */
  175. public interface Transformacja {
  176. Punkt transformuj(Punkt p);
  177. Transformacja getTransformacjaOdwrotna()
  178. throws BrakTransformacjiOdwrotnejException;
  179. }
  180. /////////////////////////////////////
  181. /*
  182. * To change this license header, choose License Headers in Project Properties.
  183. * To change this template file, choose Tools | Templates
  184. * and open the template in the editor.
  185. */
  186. package pl.edu.pg.eti.ksg.po.lab1.transformacje;
  187.  
  188. /**
  189. *
  190. * @author student
  191. */
  192. public class Translacja implements Transformacja{
  193. private final double dX,dY;
  194. public Translacja(double dX, double dY) {
  195. this.dX = dX;
  196. this.dY = dY;
  197. }
  198. @Override
  199. public Transformacja getTransformacjaOdwrotna() {
  200. return new Translacja(-dX, -dY);
  201. }
  202. @Override
  203. public Punkt transformuj(Punkt p) {
  204. return new Punkt(p.getX() + dX, p.getY() + dY);
  205. }
  206. public double getdX() {
  207. return dX;
  208. }
  209. public double getdY() {
  210. return dY;
  211. }
  212. @Override
  213. public String toString() {
  214. return "Translacja o wektor ("+dX+","+dY+")";
  215. }
  216. }
  217.  
  218. //////////////////////////////////////////////
  219. *
  220. * To change this license header, choose License Headers in Project Properties.
  221. * To change this template file, choose Tools | Templates
  222. * and open the template in the editor.
  223. */
  224. package pl.edu.pg.eti.ksg.po.lab1.transformacje;
  225.  
  226. /**
  227. *
  228. * @author student
  229. */
  230. public class Skalowanie implements Transformacja{
  231. private final double skalaX,skalaY;
  232. public Skalowanie(double skalaX, double skalaY) {
  233. this.skalaX = skalaX;
  234. this.skalaY = skalaY;
  235. }
  236. @Override
  237. public Transformacja getTransformacjaOdwrotna() throws
  238. BrakTransformacjiOdwrotnejException {
  239. if(skalaX == 0 || skalaY == 0)
  240. throw new BrakTransformacjiOdwrotnejException("Brak transformacji odwrotnej. Przynajmniej jeden z czynników skalowania jest równy 0.");
  241.  
  242. return new Skalowanie(1/skalaX,1/skalaY);
  243. }
  244. @Override
  245. public Punkt transformuj(Punkt p) {
  246. return new Punkt(skalaX * p.getX(), skalaY * p.getY());
  247. }
  248. public double getSkalaX() {
  249. return skalaX;
  250. }
  251. public double getSkalaY() {
  252. return skalaY;
  253. }
  254. @Override
  255. public String toString() {
  256. return "Skalowanie "+skalaX+"x oraz "+skalaY+"y";
  257. }
  258.  
  259. }
  260. ///////////////////////////////////////////////////////////
  261. /*
  262. * To change this license header, choose License Headers in Project Properties.
  263. * To change this template file, choose Tools | Templates
  264. * and open the template in the editor.
  265. */
  266. package pl.edu.pg.eti.ksg.po.lab1.transformacje;
  267.  
  268. /**
  269. *
  270. * @author student
  271. */
  272. public class Obrot implements Transformacja {
  273. private final double angle;
  274.  
  275. public Obrot(double angle)
  276. {
  277. this.angle=angle;
  278. }
  279. @Override
  280. public Transformacja getTransformacjaOdwrotna()
  281. {
  282. return new Obrot(-angle);
  283. }
  284.  
  285. @Override
  286. public Punkt transformuj(Punkt p)
  287. {
  288. double x=p.getX()*Math.cos(angle)-p.getY()*Math.sin(angle);
  289. double y=p.getX()*Math.sin(angle)+p.getY()*Math.cos(angle);
  290. return new Punkt(x,y);
  291. }
  292.  
  293. @Override
  294. public String toString()
  295. {
  296. return "Obrót("+angle+")";
  297. }
  298.  
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement