Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.*;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import static java.lang.Math.*;
  7.  
  8. public class Geom extends JFrame {
  9. private class PaintPanel extends JPanel {
  10. double minX = -5;
  11. double minY = -5;
  12. double maxX = 5;
  13. double maxY = 5;
  14. int state = 0;
  15. double[][] cord = new double[3][3];
  16.  
  17. public PaintPanel() {
  18. addKeyListener(new KeyListener() {
  19. public void keyPressed(KeyEvent arg0) {
  20. }
  21.  
  22. public void keyReleased(KeyEvent arg0) {
  23. }
  24.  
  25. public void keyTyped(KeyEvent key) {
  26. if (key.getKeyChar() == ' ') {
  27. state = (state + 1) % 7;
  28. repaint();
  29. }
  30. System.err.println(state);
  31. }
  32. });
  33. setFocusable(true);
  34. }
  35.  
  36. public void paintComponent(Graphics g) {
  37. super.paintComponents(g);
  38. Graphics2D g2 = (Graphics2D) g;
  39. g2.setColor(Color.black);
  40. g2.fill(new Rectangle(getSize()));
  41. paintAxis(g2);
  42. g2.setColor(Color.yellow);
  43. if (state == 0) {
  44. cord = createNewTriangle();
  45. } else if (state == 1) {
  46. double dx = cord[0][0];
  47. double dy = cord[0][1];
  48. for (int i = 0; i < cord.length; ++i) {
  49. cord[i] = move(cord[i], -dx, -dy);
  50. }
  51. } else if (state == 2) {
  52. double angle = Math.PI - Math.atan2(cord[1][1], cord[1][0]);
  53. for (int i = 0; i < cord.length; ++i) {
  54. cord[i] = rotate(cord[i], angle);
  55. }
  56. } else if (state == 3) {
  57. for (int i = 0; i < cord.length; ++i) {
  58. cord[i] = scale(cord[i], -1, -1);
  59. }
  60. } else if (state == 4) {
  61. double[][] ncord = new double[4][];
  62. ncord[0] = cord[1];
  63. ncord[1] = cord[2];
  64. ncord[2] = new double[] {-cord[1][0], -cord[1][1], 1};
  65. ncord[3] = new double[] {-cord[2][0], -cord[2][1], 1};
  66. cord = ncord;
  67. } else if (state == 5) {
  68. double kx = 1;
  69. double ky = Math.abs((cord[1][0] + cord[2][0]) / (cord[1][1] + cord[2][1]));
  70. for (int i = 0; i < cord.length; ++i) {
  71. cord[i] = scale(cord[i], kx, ky);
  72. }
  73. } else if (state == 6) {
  74. double dx = 0;
  75. for (int i = 0; i < cord.length; ++i) {
  76. dx = Math.max(dx, Math.abs(cord[i][0]));
  77. }
  78. for (int i = 0; i < cord.length; ++i) {
  79. if (Math.abs(cord[i][0]) < 1e-9) {
  80. if (cord[i][1] > 0) {
  81. cord[i] = move(cord[i], dx, 0);
  82. } else if (cord[i][1] < 0){
  83. cord[i] = move(cord[i], -dx, 0);
  84. }
  85. }
  86. }
  87. }
  88. drawPolygon(cord, g2);
  89. }
  90.  
  91. private double[] scale(double[] a, double kx, double ky) {
  92. double[][] m = new double[][] { {kx, 0, 0},
  93. {0, ky, 0},
  94. {0, 0, 1} };
  95. return multiply(a, m)[0];
  96. }
  97.  
  98. private void drawPolygon(double[][] cord, Graphics2D g2) {
  99. for (int i = 0; i < cord.length; ++i) {
  100. drawLine(cord[i][0], cord[i][1], cord[(i + 1) % cord.length][0], cord[(i + 1) % cord.length][1], g2);
  101. }
  102. }
  103.  
  104. private double[][] createNewTriangle() {
  105. double[][] cord = new double[3][];
  106. Random rm = new Random();
  107. double x = (rm.nextDouble() + 0.5) * maxX / 3;
  108. double y = (rm.nextDouble() + 0.5) * maxY / 3;
  109. cord[0] = new double[] {0, 0, 1};
  110. cord[1] = new double[] {0, y, 1};
  111. cord[2] = new double[] {x, 0, 1};
  112. double dx = (rm.nextDouble() + 0.0) * maxX / 3 + maxX / 5;
  113. double dy = (rm.nextDouble() + 0.0) * maxY / 3 + maxY / 5;
  114. for (int i = 0; i < 3; ++i) {
  115. cord[i] = move(cord[i], dx, dy);
  116. }
  117. double angle = Math.PI * rm.nextDouble();
  118. for (int i = 0; i < 3; ++i) {
  119. cord[i] = rotate(cord[i], angle);
  120. }
  121. return cord;
  122. }
  123.  
  124. private double[] rotate(double[] a, double angle) {
  125. double[][] m = new double[][] { {cos(angle), sin(angle), 0},
  126. {-sin(angle), cos(angle), 0},
  127. {0, 0, 1}};
  128. return multiply(a, m)[0];
  129. }
  130.  
  131. private double[] move(double[] a, double dx, double dy) {
  132. double[][] m = new double[][] { {1, 0, 0},
  133. {0, 1, 0},
  134. {dx, dy, 1}};
  135. return multiply(a, m)[0];
  136. }
  137.  
  138. private double[][] multiply(double[] cord, double[][] b) {
  139. double[][] a = new double[][] {cord};
  140. double[][] res = new double[a.length][b[0].length];
  141. for (int i = 0; i < res.length; ++i) {
  142. for (int j = 0; j < res[i].length; ++j) {
  143. for (int k = 0; k < a[i].length; ++k) {
  144. res[i][j] += a[i][k] * b[k][j];
  145. }
  146. }
  147. }
  148. return res;
  149. }
  150.  
  151. private void paintAxis(Graphics2D g2) {
  152. g2.setColor(Color.green);
  153. drawLine(minX, 0, maxX, 0, g2);
  154. drawLine(0, minY, 0, maxY, g2);
  155. }
  156.  
  157. private void drawLine(double x, double y, double x2, double y2, Graphics2D g2) {
  158. int ix = screenX(x);
  159. int iy = screenY(y);
  160. int ix2 = screenX(x2);
  161. int iy2 = screenY(y2);
  162. g2.drawLine(ix, iy, ix2, iy2);
  163. }
  164.  
  165. private int screenY(double y) {
  166. Dimension dim = getSize();
  167. return (int) (dim.height * (1 - (y - minX) / (maxX - minX)));
  168. }
  169.  
  170. private int screenX(double x) {
  171. Dimension dim = getSize();
  172. return (int) (dim.width * (x - minX) / (maxX - minX));
  173. }
  174. }
  175.  
  176. public Geom() {
  177. Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  178. setBounds(dim.width / 4, dim.height / 4, dim.width / 2, dim.height / 2);
  179. add(new PaintPanel());
  180. setDefaultCloseOperation(EXIT_ON_CLOSE);
  181. setVisible(true);
  182. }
  183.  
  184. public static void main(String[] args) {
  185. new Geom();
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement