Una forma sencilla y rápida de aprender JAVA, observando y deduciendo cómo se comporta el lenguaje a través de ejemplos prácticos.

Archivo del blog

miércoles, 10 de agosto de 2022

Distancia entre 2 puntos en un espacio n-dimensional.


Fórmula:

 
d(P,Q) = SQRT[ (Q1-P1)^2 + (Q2-P2)^2 + (Q3-P3)^2 + ... + (Qn-Pn)^2 ]


Código java (Distancia2p.java)

package distancia2p;

import java.util.Arrays;

public class Distancia2p {

    public static void main(String[] args) {

        int n = 6;  //numero de dimensiones
        int[] A = new int[n];
        int[] B = new int[n];

        //posición cartesiana n coordenadas
        int maximo = 100;
        int minimo = 1;
        for (int i = 0; i < n; i++) {
            //numero aleatoria entre 1 (minimo) y 100 (maximo)
            A[i] = (int) Math.floor(Math.random() * (maximo - minimo + 1)) + minimo;
            B[i] = (int) Math.floor(Math.random() * (maximo - minimo + 1)) + minimo;
        }

        double aux = 0;
        double distancia;

        for (int i = 0; i < n; i++) {
            aux = aux + Math.pow((A[i] - B[i]), 2);
        }
        distancia = Math.sqrt(aux);

        //mostrar resultados
        System.out.println("*Distancia entre 2 puntos en un espacio de " + n + "-dimensiones:");
        System.out.println("coordenadas punto A: " + Arrays.toString(A));
        System.out.println("coordenadas punto B: " + Arrays.toString(B));
        System.out.println("Distancia entre punto A y B: " + distancia);

    }

}


Resultado:

run:
*Distancia entre 2 puntos en un espacio de 6-dimensiones:
coordenadas punto A: [29, 89, 69, 33, 18, 22]
coordenadas punto B: [76, 93, 41, 53, 43, 91]
Distancia entre punto A y B: 93.78166132032425
BUILD SUCCESSFUL (total time: 0 seconds)


miércoles, 3 de agosto de 2022

Gráficos 2D. Creación de un fractal 2 (árbol).

Código 1: (Blogspot_Fractal.java):

package blogspot_fractal;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Blogspot_Fractal {

    public static void main(String[] args) {

        int x = 300;
        int y = 256;
        int angulo = -90;
        int depth = 9;

        BufferedImage imagen = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        Dibujo.Dibujar((Graphics2D) imagen.getGraphics(), x / 2, y, angulo, depth);

        try {
            ImageIO.write(imagen, "png", new File("Arbol.png"));
        } catch (IOException e) {
            System.out.println("Error de escritura");
        }

    }

}


Código 2: (Dibujo.java):

package blogspot_fractal;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

class Dibujo {

    public static void Dibujar(Graphics2D g, int x1, int y1, double angle, int depth) {

        if (depth == 0) {
            return;
        }
        
        g.setColor(Color.GREEN);
        
        // grosor rama dependiendo de la profundidad
        g.setStroke(new BasicStroke((float) depth));

        //Filtro antialiasing       
        g.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 5.0);
        int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 6.0);
        g.drawLine(x1, y1, x2, y2);
        Dibujar(g, x2, y2, angle - 30, depth - 1);
        Dibujar(g, x2, y2, angle + 30, depth - 1);

    }

}


Resultado:


martes, 2 de agosto de 2022

Graficos 2D. Curva de Hilbert 2 (4K)

Código 1: (Blogspot_Curvahilbert.java)

package blogspot_curvahilbert;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Blogspot_CurvaHilbert {

    public static void main(String[] args) {

        // resolucion 4K
        int x = 2160;
        int y = 2160;
        int depth = 8;

        BufferedImage imagen = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);

        Dibujo.Dibujar((Graphics2D) imagen.getGraphics(), x / 2, y / 2, depth, y / 2);

        try {
            ImageIO.write(imagen, "png", new File("CurbaHilbert.png"));
        } catch (IOException e) {
            System.out.println("Error de escritura");
        }

    }

}


Código 2: (Dibujo.java):

package blogspot_curvahilbert;

import java.awt.Color;
import java.awt.Graphics2D;

class Dibujo {

    public static void Dibujar(Graphics2D g, int x, int y, int n, int size) {

        if (n == 0) {
            return;
        }

        int x0 = x - size / 2;
        int x1 = x + size / 2;
        int y0 = y - size / 2;
        int y1 = y + size / 2;
        g.setColor(Color.GREEN);
        g.drawLine(x0, y0, x0, y1);
        g.drawLine(x1, y0, x1, y1);
        g.drawLine(x0, y, x1, y);
        Dibujar(g, x0, y0, n - 1, size / 2);
        Dibujar(g, x0, y1, n - 1, size / 2);
        Dibujar(g, x1, y0, n - 1, size / 2);
        Dibujar(g, x1, y1, n - 1, size / 2);

    }

}


Resultado:





Con la tecnología de Blogger.