Advertisement
LukacikPavel

trojuholniky

Oct 7th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.44 KB | None | 0 0
  1. package ugr;
  2.  
  3. import org.lwjgl.glfw.*;
  4. import org.lwjgl.opengl.GL;
  5.  
  6. import static org.lwjgl.glfw.GLFW.*;
  7. import static org.lwjgl.opengl.GL11.*;
  8. import static org.lwjgl.system.MemoryUtil.*;
  9.  
  10. import java.lang.reflect.Array;
  11. import java.nio.ByteBuffer;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14.  
  15. import org.lwjgl.BufferUtils;
  16.  
  17. public class cv02 {
  18.     private int sirka = 800;
  19.     private int vyska = 600;
  20.     private int rozmer = Math.min(sirka, vyska) / 2; // max. polomer kruhu
  21.     private int sx = sirka / 2, sy = vyska / 2; // stred obrazovky
  22.     private ByteBuffer buffer = BufferUtils.createByteBuffer(sirka * vyska * 4); // 4 bpp (bytes per pixel), RGBA
  23.     private int rgba_WHITE = 0xFFFFFFFF; // -1
  24.     private int rgba_BLACK = 0x00000000;
  25.  
  26.     void floodFill(int x, int y) {
  27.         if (zistiFarbu(x, y) == rgba_BLACK) {
  28.             Bod(x, y);
  29.             floodFill(x - 1, y);
  30.             floodFill(x + 1, y);
  31.             floodFill(x, y - 1);
  32.             floodFill(x, y + 1);
  33.         }
  34.     }
  35.  
  36.     private void vypln(int x, int y) {
  37.         // floodFill(x, y);
  38.         seedFill(x, y);
  39.         glColor3d(0, 0, 255);
  40.         seedFill((int) (x * 0.15), y);
  41.         glColor3d(0, 255, 0);
  42.         seedFill((int) (x + 100), y);
  43.     }
  44.  
  45.     void Bod(int x, int y) {
  46.         glBegin(GL_POINTS);
  47.         glVertex2i(x, y);
  48.         glEnd();
  49.     }
  50.  
  51.     private void seedFill(int x, int y) {
  52.  
  53.         if (zistiFarbu(x, y) == rgba_BLACK) {
  54.             int xL = x;
  55.             int xR = x;
  56.             while (xL >= 0 && zistiFarbu(xL, y) == rgba_BLACK) {
  57.                 xL--;
  58.             }
  59.             while (xR <= sirka - 1 && zistiFarbu(xR, y) == rgba_BLACK) {
  60.                 xR++;
  61.             }
  62.             riadok(y, xL + 1, xR - 1);
  63.  
  64.             for (int i = xL + 1; i < xR; i++) {
  65.                 if (y - 1 >= 0) {
  66.                     seedFill(i, y - 1);
  67.                 }
  68.                 if (y + 1 < vyska) {
  69.                     seedFill(i, y + 1);
  70.                 }
  71.             }
  72.  
  73.         }
  74.     }
  75.    
  76.     ArrayList<Bod> Bresenham2(int x1, int y1, int x2, int y2) {
  77.         boolean zapamataj = false;
  78.         ArrayList<Bod> listBodov = new ArrayList<>();
  79.  
  80.         if (Math.abs(x2 - x1) < Math.abs(y2 - y1)) {
  81.             int pom;
  82.             pom = x1;
  83.             x1 = y1;
  84.             y1 = pom;
  85.             pom = x2;
  86.             x2 = y2;
  87.             y2 = pom;
  88.             zapamataj = true;
  89.         }
  90.  
  91.         if (x2 < x1) {
  92.             int pom;
  93.             pom = x1;
  94.             x1 = x2;
  95.             x2 = pom;
  96.             pom = y1;
  97.             y1 = y2;
  98.             y2 = pom;
  99.         }
  100.  
  101.         int krok = y2 < y1 ? -1 : 1;
  102.         int dx = x2 - x1;
  103.         int dy = Math.abs(y2 - y1);
  104.         int y = y1;
  105.         int k1 = 2 * dy;
  106.         int k2 = 2 * dy - 2 * dx;
  107.         int d = 2 * dy - dx;
  108.  
  109.         for (int x = x1; x <= x2; x++) {
  110.             if (zapamataj) {
  111.                 Bod(y, x);
  112.                 listBodov.add(new Bod(y, x));
  113.             } else {
  114.                 Bod(x, y);
  115.                 listBodov.add(new Bod(x, y));
  116.             }
  117.             if (d < 0) {
  118.                 d = d + k1;
  119.             } else {
  120.                 d = d + k2;
  121.                 y = y + krok;
  122.             }
  123.         }
  124.         return listBodov;
  125.     }
  126.  
  127.     void Bresenham(int x1, int y1, int x2, int y2, boolean thick) {
  128.         boolean zapamataj = false;
  129.  
  130.         if (Math.abs(x2 - x1) < Math.abs(y2 - y1)) {
  131.             int pom;
  132.             pom = x1;
  133.             x1 = y1;
  134.             y1 = pom;
  135.             pom = x2;
  136.             x2 = y2;
  137.             y2 = pom;
  138.             zapamataj = true;
  139.         }
  140.  
  141.         if (x2 < x1) {
  142.             int pom;
  143.             pom = x1;
  144.             x1 = x2;
  145.             x2 = pom;
  146.             pom = y1;
  147.             y1 = y2;
  148.             y2 = pom;
  149.         }
  150.  
  151.         int krok = y2 < y1 ? -1 : 1;
  152.         int dx = x2 - x1;
  153.         int dy = Math.abs(y2 - y1);
  154.         int y = y1;
  155.         int k1 = 2 * dy;
  156.         int k2 = 2 * dy - 2 * dx;
  157.         int d = 2 * dy - dx;
  158.  
  159.         for (int x = x1; x <= x2; x++) {
  160.             if (zapamataj) {
  161.                 Bod(y, x);
  162.                 if (thick) {
  163.                     Bod(y + 1, x);
  164.                     Bod(y, x + 1);
  165.                     Bod(y - 1, x);
  166.                     Bod(y, x - 1);
  167.                 }
  168.             } else {
  169.                 Bod(x, y);
  170.                 if (thick) {
  171.                     Bod(x + 1, y);
  172.                     Bod(x, y + 1);
  173.                     Bod(x - 1, y);
  174.                     Bod(x, y - 1);
  175.                 }
  176.             }
  177.             if (d < 0) {
  178.                 d = d + k1;
  179.             } else {
  180.                 d = d + k2;
  181.                 y = y + krok;
  182.             }
  183.         }
  184.     }
  185.  
  186.     void vyklesliTrojuholnik(int x1, int y1, int x2, int y2, int x3, int y3) {
  187.         byte r = (byte)(Math.random() * Byte.MAX_VALUE);
  188.         byte g = (byte)(Math.random() * Byte.MAX_VALUE);
  189.         byte b = (byte)(Math.random() * Byte.MAX_VALUE);
  190.         glColor3b(r, g, b);
  191.         ArrayList<Bod> body = new ArrayList<>();
  192.         body.addAll(Bresenham2(x1, y1, x2, y2));
  193.         body.addAll(Bresenham2(x1, y1, x3, y3));
  194.         body.addAll(Bresenham2(x3, y3, x2, y2));
  195.         //System.out.println(body);
  196.         body.sort(new ComparatorBodov());
  197.         //System.out.println(body);
  198.        
  199.         for (int i = 0; i < body.size() - 1; i++) {
  200.             Bod bod1 = body.get(i);
  201.             Bod bod2 = body.get(i + 1);
  202.            
  203.             if (bod1.getX() == bod2.getX() || bod1.getY() != bod2.getY()) {
  204.                 continue;
  205.             }
  206.            
  207.             int x = bod1.getX();
  208.             int y = bod1.getY();
  209.             int x0 = bod2.getX();
  210.            
  211.             while (x != x0) {
  212.                 Bod(x, y);
  213.                 x++;
  214.             }
  215.         }
  216.        
  217.     }
  218.  
  219.     void vykresliGL() {
  220.         // Clear the screen and depth buffer
  221. //      glLoadIdentity();
  222. //
  223. //      // vonkajsia lomena ciara
  224. //      glBegin(GL_LINE_LOOP);
  225. //      glColor3f(1, 1, 1);
  226. //      lomenaCiaraKruh(0.75, 0.25);
  227. //      glEnd();
  228. //
  229. //      // vnutorna lomena ciara
  230. //      glBegin(GL_LINE_LOOP);
  231. //      glColor3f(0, 0, 1);
  232. //      lomenaCiaraKruh(0.2, 0.18);
  233. //      glEnd();
  234. //
  235. //      takeScreenShot();
  236. //
  237. //      glBegin(GL_POINTS);
  238. //      glColor3f(1, 0, 0);
  239. //      vypln(sx, sy);
  240. //      glEnd();
  241.  
  242.         for (int i = 0; i < 100; i++) {
  243.             vyklesliTrojuholnik((int) (Math.random() * 800), (int) (Math.random() * 600), (int) (Math.random() * 800),
  244.                     (int) (Math.random() * 600), (int) (Math.random() * 800), (int) (Math.random() * 600));
  245.         }
  246.  
  247.     }
  248.  
  249.     long window;
  250.     GLFWErrorCallback errorCallback;
  251.     GLFWKeyCallback keyCallback;
  252.  
  253.     void spusti() {
  254.         try {
  255.             init();
  256.             loop();
  257.  
  258.             glfwDestroyWindow(window);
  259.             keyCallback.free();
  260.         } finally {
  261.             glfwTerminate();
  262.             errorCallback.free();
  263.         }
  264.     }
  265.  
  266.     void init() {
  267.         glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
  268.         if (!glfwInit())
  269.             throw new IllegalStateException("Chyba pri inicializacii GLFW!!!");
  270.  
  271.         window = glfwCreateWindow(sirka, vyska, "UGR1", NULL, NULL);
  272.         if (window == NULL)
  273.             throw new RuntimeException("Chyba pri vytvoreni GLFW okna!!!");
  274.  
  275.         glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
  276.             @Override
  277.             public void invoke(long window, int key, int scancode, int action, int mods) {
  278.                 if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
  279.                     glfwSetWindowShouldClose(window, true);
  280.             }
  281.         });
  282.  
  283.         GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  284.         glfwSetWindowPos(window, (vidmode.width() - sirka) / 2, (vidmode.height() - vyska) / 2);
  285.  
  286.         glfwMakeContextCurrent(window);
  287.         glfwSwapInterval(0);
  288.         glfwShowWindow(window);
  289.  
  290.         GL.createCapabilities();
  291.         glReadBuffer(GL_BACK); // select a color buffer source for pixels
  292.     }
  293.  
  294.     private void takeScreenShot() {
  295.         glReadPixels(0, 0, sirka, vyska, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  296.     }
  297.  
  298.     private void riadok(int y, int x1, int x2) {
  299.         for (int x = x1; x <= x2; x++) {
  300.             bod(x, y);
  301.         }
  302.     }
  303.  
  304.     private void bod(int x, int y) {
  305.         glVertex2i(x, y);
  306.         y = vyska - 1 - y;
  307.         buffer.asIntBuffer().put(y * sirka + x, rgba_WHITE);
  308.     }
  309.  
  310.     private int zistiFarbu(int x, int y) {
  311.         y = vyska - 1 - y;
  312.         return buffer.asIntBuffer().get(sirka * y + x);
  313.     }
  314.  
  315.     private void lomenaCiaraKruh(double stred, double odchylka) {
  316.         double r, znam = -1;
  317.         for (double uhol = 0; uhol < 1.999 * Math.PI; uhol += 20 * Math.PI / 180) {
  318.             r = stred * rozmer + znam * odchylka * Math.random() * rozmer;
  319.             glVertex2d(sx + r * Math.cos(uhol), sy + r * Math.sin(uhol));
  320.             znam = -znam;
  321.         }
  322.     }
  323.  
  324.     void loop() {
  325.         glViewport(0, 0, sirka, vyska);
  326.  
  327.         glMatrixMode(GL_PROJECTION);
  328.         glLoadIdentity();
  329.         glOrtho(-0.5, sirka - 0.5, vyska - 0.5, -0.5, 0, 1);
  330.  
  331.         glMatrixMode(GL_MODELVIEW);
  332.         glLoadIdentity();
  333.  
  334.         glClearColor(0.f, 0.f, 0.f, 1.f); // Initialize clear color
  335.  
  336.         vykresliGL();
  337.  
  338.         glfwSwapBuffers(window);
  339.  
  340.         while (!glfwWindowShouldClose(window)) {
  341.             glfwPollEvents();
  342.         }
  343.     }
  344.  
  345.     public static void main(String[] args) {
  346.         new cv02().spusti();
  347.     }
  348.    
  349. }
  350.  
  351. package ugr;
  352.  
  353. public class Bod {
  354.    
  355.     private int x;
  356.     private int y;
  357.    
  358.     public Bod(int x, int y) {
  359.         this.x = x;
  360.         this.y = y;
  361.     }
  362.  
  363.    
  364.  
  365.     @Override
  366.     public String toString() {
  367.         return "Bod [x=" + x + ", y=" + y + "]";
  368.     }
  369.  
  370.     public int getX() {
  371.         return x;
  372.     }
  373.  
  374.     public void setX(int x) {
  375.         this.x = x;
  376.     }
  377.  
  378.     public int getY() {
  379.         return y;
  380.     }
  381.  
  382.     public void setY(int y) {
  383.         this.y = y;
  384.     }
  385.    
  386. }
  387. package ugr;
  388.  
  389. import java.util.Comparator;
  390.  
  391. public class ComparatorBodov implements Comparator<Bod> {
  392.  
  393.     @Override
  394.     public int compare(Bod o1, Bod o2) {
  395.         if (o1.getY() < o2.getY()) {
  396.             return -1;
  397.         } else if (o1.getY() == o2.getY()) {
  398.             if (o1.getX() < o2.getX()) {
  399.                 return -1;
  400.             } else if (o1.getX() == o2.getX()) {
  401.                 return 0;
  402.             } else {
  403.                 return 1;
  404.             }
  405.         } else {
  406.             return 1;
  407.         }
  408.     }
  409.  
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement