Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ugr;
- import org.lwjgl.glfw.*;
- import org.lwjgl.opengl.GL;
- import static org.lwjgl.glfw.GLFW.*;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.system.MemoryUtil.*;
- import java.lang.reflect.Array;
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- import java.util.Arrays;
- import org.lwjgl.BufferUtils;
- public class cv02 {
- private int sirka = 800;
- private int vyska = 600;
- private int rozmer = Math.min(sirka, vyska) / 2; // max. polomer kruhu
- private int sx = sirka / 2, sy = vyska / 2; // stred obrazovky
- private ByteBuffer buffer = BufferUtils.createByteBuffer(sirka * vyska * 4); // 4 bpp (bytes per pixel), RGBA
- private int rgba_WHITE = 0xFFFFFFFF; // -1
- private int rgba_BLACK = 0x00000000;
- void floodFill(int x, int y) {
- if (zistiFarbu(x, y) == rgba_BLACK) {
- Bod(x, y);
- floodFill(x - 1, y);
- floodFill(x + 1, y);
- floodFill(x, y - 1);
- floodFill(x, y + 1);
- }
- }
- private void vypln(int x, int y) {
- // floodFill(x, y);
- seedFill(x, y);
- glColor3d(0, 0, 255);
- seedFill((int) (x * 0.15), y);
- glColor3d(0, 255, 0);
- seedFill((int) (x + 100), y);
- }
- void Bod(int x, int y) {
- glBegin(GL_POINTS);
- glVertex2i(x, y);
- glEnd();
- }
- private void seedFill(int x, int y) {
- if (zistiFarbu(x, y) == rgba_BLACK) {
- int xL = x;
- int xR = x;
- while (xL >= 0 && zistiFarbu(xL, y) == rgba_BLACK) {
- xL--;
- }
- while (xR <= sirka - 1 && zistiFarbu(xR, y) == rgba_BLACK) {
- xR++;
- }
- riadok(y, xL + 1, xR - 1);
- for (int i = xL + 1; i < xR; i++) {
- if (y - 1 >= 0) {
- seedFill(i, y - 1);
- }
- if (y + 1 < vyska) {
- seedFill(i, y + 1);
- }
- }
- }
- }
- ArrayList<Bod> Bresenham2(int x1, int y1, int x2, int y2) {
- boolean zapamataj = false;
- ArrayList<Bod> listBodov = new ArrayList<>();
- if (Math.abs(x2 - x1) < Math.abs(y2 - y1)) {
- int pom;
- pom = x1;
- x1 = y1;
- y1 = pom;
- pom = x2;
- x2 = y2;
- y2 = pom;
- zapamataj = true;
- }
- if (x2 < x1) {
- int pom;
- pom = x1;
- x1 = x2;
- x2 = pom;
- pom = y1;
- y1 = y2;
- y2 = pom;
- }
- int krok = y2 < y1 ? -1 : 1;
- int dx = x2 - x1;
- int dy = Math.abs(y2 - y1);
- int y = y1;
- int k1 = 2 * dy;
- int k2 = 2 * dy - 2 * dx;
- int d = 2 * dy - dx;
- for (int x = x1; x <= x2; x++) {
- if (zapamataj) {
- Bod(y, x);
- listBodov.add(new Bod(y, x));
- } else {
- Bod(x, y);
- listBodov.add(new Bod(x, y));
- }
- if (d < 0) {
- d = d + k1;
- } else {
- d = d + k2;
- y = y + krok;
- }
- }
- return listBodov;
- }
- void Bresenham(int x1, int y1, int x2, int y2, boolean thick) {
- boolean zapamataj = false;
- if (Math.abs(x2 - x1) < Math.abs(y2 - y1)) {
- int pom;
- pom = x1;
- x1 = y1;
- y1 = pom;
- pom = x2;
- x2 = y2;
- y2 = pom;
- zapamataj = true;
- }
- if (x2 < x1) {
- int pom;
- pom = x1;
- x1 = x2;
- x2 = pom;
- pom = y1;
- y1 = y2;
- y2 = pom;
- }
- int krok = y2 < y1 ? -1 : 1;
- int dx = x2 - x1;
- int dy = Math.abs(y2 - y1);
- int y = y1;
- int k1 = 2 * dy;
- int k2 = 2 * dy - 2 * dx;
- int d = 2 * dy - dx;
- for (int x = x1; x <= x2; x++) {
- if (zapamataj) {
- Bod(y, x);
- if (thick) {
- Bod(y + 1, x);
- Bod(y, x + 1);
- Bod(y - 1, x);
- Bod(y, x - 1);
- }
- } else {
- Bod(x, y);
- if (thick) {
- Bod(x + 1, y);
- Bod(x, y + 1);
- Bod(x - 1, y);
- Bod(x, y - 1);
- }
- }
- if (d < 0) {
- d = d + k1;
- } else {
- d = d + k2;
- y = y + krok;
- }
- }
- }
- void vyklesliTrojuholnik(int x1, int y1, int x2, int y2, int x3, int y3) {
- byte r = (byte)(Math.random() * Byte.MAX_VALUE);
- byte g = (byte)(Math.random() * Byte.MAX_VALUE);
- byte b = (byte)(Math.random() * Byte.MAX_VALUE);
- glColor3b(r, g, b);
- ArrayList<Bod> body = new ArrayList<>();
- body.addAll(Bresenham2(x1, y1, x2, y2));
- body.addAll(Bresenham2(x1, y1, x3, y3));
- body.addAll(Bresenham2(x3, y3, x2, y2));
- //System.out.println(body);
- body.sort(new ComparatorBodov());
- //System.out.println(body);
- for (int i = 0; i < body.size() - 1; i++) {
- Bod bod1 = body.get(i);
- Bod bod2 = body.get(i + 1);
- if (bod1.getX() == bod2.getX() || bod1.getY() != bod2.getY()) {
- continue;
- }
- int x = bod1.getX();
- int y = bod1.getY();
- int x0 = bod2.getX();
- while (x != x0) {
- Bod(x, y);
- x++;
- }
- }
- }
- void vykresliGL() {
- // Clear the screen and depth buffer
- // glLoadIdentity();
- //
- // // vonkajsia lomena ciara
- // glBegin(GL_LINE_LOOP);
- // glColor3f(1, 1, 1);
- // lomenaCiaraKruh(0.75, 0.25);
- // glEnd();
- //
- // // vnutorna lomena ciara
- // glBegin(GL_LINE_LOOP);
- // glColor3f(0, 0, 1);
- // lomenaCiaraKruh(0.2, 0.18);
- // glEnd();
- //
- // takeScreenShot();
- //
- // glBegin(GL_POINTS);
- // glColor3f(1, 0, 0);
- // vypln(sx, sy);
- // glEnd();
- for (int i = 0; i < 100; i++) {
- vyklesliTrojuholnik((int) (Math.random() * 800), (int) (Math.random() * 600), (int) (Math.random() * 800),
- (int) (Math.random() * 600), (int) (Math.random() * 800), (int) (Math.random() * 600));
- }
- }
- long window;
- GLFWErrorCallback errorCallback;
- GLFWKeyCallback keyCallback;
- void spusti() {
- try {
- init();
- loop();
- glfwDestroyWindow(window);
- keyCallback.free();
- } finally {
- glfwTerminate();
- errorCallback.free();
- }
- }
- void init() {
- glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
- if (!glfwInit())
- throw new IllegalStateException("Chyba pri inicializacii GLFW!!!");
- window = glfwCreateWindow(sirka, vyska, "UGR1", NULL, NULL);
- if (window == NULL)
- throw new RuntimeException("Chyba pri vytvoreni GLFW okna!!!");
- glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
- @Override
- public void invoke(long window, int key, int scancode, int action, int mods) {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
- glfwSetWindowShouldClose(window, true);
- }
- });
- GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
- glfwSetWindowPos(window, (vidmode.width() - sirka) / 2, (vidmode.height() - vyska) / 2);
- glfwMakeContextCurrent(window);
- glfwSwapInterval(0);
- glfwShowWindow(window);
- GL.createCapabilities();
- glReadBuffer(GL_BACK); // select a color buffer source for pixels
- }
- private void takeScreenShot() {
- glReadPixels(0, 0, sirka, vyska, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
- }
- private void riadok(int y, int x1, int x2) {
- for (int x = x1; x <= x2; x++) {
- bod(x, y);
- }
- }
- private void bod(int x, int y) {
- glVertex2i(x, y);
- y = vyska - 1 - y;
- buffer.asIntBuffer().put(y * sirka + x, rgba_WHITE);
- }
- private int zistiFarbu(int x, int y) {
- y = vyska - 1 - y;
- return buffer.asIntBuffer().get(sirka * y + x);
- }
- private void lomenaCiaraKruh(double stred, double odchylka) {
- double r, znam = -1;
- for (double uhol = 0; uhol < 1.999 * Math.PI; uhol += 20 * Math.PI / 180) {
- r = stred * rozmer + znam * odchylka * Math.random() * rozmer;
- glVertex2d(sx + r * Math.cos(uhol), sy + r * Math.sin(uhol));
- znam = -znam;
- }
- }
- void loop() {
- glViewport(0, 0, sirka, vyska);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-0.5, sirka - 0.5, vyska - 0.5, -0.5, 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClearColor(0.f, 0.f, 0.f, 1.f); // Initialize clear color
- vykresliGL();
- glfwSwapBuffers(window);
- while (!glfwWindowShouldClose(window)) {
- glfwPollEvents();
- }
- }
- public static void main(String[] args) {
- new cv02().spusti();
- }
- }
- package ugr;
- public class Bod {
- private int x;
- private int y;
- public Bod(int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public String toString() {
- return "Bod [x=" + x + ", y=" + y + "]";
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- }
- package ugr;
- import java.util.Comparator;
- public class ComparatorBodov implements Comparator<Bod> {
- @Override
- public int compare(Bod o1, Bod o2) {
- if (o1.getY() < o2.getY()) {
- return -1;
- } else if (o1.getY() == o2.getY()) {
- if (o1.getX() < o2.getX()) {
- return -1;
- } else if (o1.getX() == o2.getX()) {
- return 0;
- } else {
- return 1;
- }
- } else {
- return 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement