ALUMNO: OSCAR ARTURO FLORES BALADEZ.
ESCUELA: UNIVERSIDAD AUTÓNOMA DEL CARMEN.
CURSO: REPRESENTACION VISUAL DE DATOS.
ACTIVIDAD 2: PROGRAMACIÓN DE TRANSFORMACIONES
BASICAS.
MATRICULA: 191786.
CIUDAD DEL CARMEN CAMPECHE 4 DE MAYO 2025.
import java.util.Scanner;
public class TransformacionesRectangulo {
// Clase interna para representar un punto en 2D
static class Punto {
double x, y;
Punto(double x, double y) {
this.x = x;
this.y = y;
}
}
// Clase para representar un rectangulo por sus 4 vertices
static class Rectangulo {
Punto[] vertices = new Punto[4];
// Constructor que crea un rectangulo a partir de un punto inicial,
ancho y alto
Rectangulo(double x, double y, double ancho, double alto) {
vertices[0] = new Punto(x, y); // superior izquierdo
vertices[1] = new Punto(x + ancho, y); // superior derecho
vertices[2] = new Punto(x + ancho, y + alto); // inferior derecho
vertices[3] = new Punto(x, y + alto); // inferior izquierdo
}
// Mostrar coordenadas del rectangulo
void mostrar() {
System.out.println("Coordenadas del rectangulo:");
for (int i = 0; i < 4; i++) {
System.out.printf("V%d: (%.2f, %.2f)%n", i + 1, vertices[i].x,
vertices[i].y);
}
}
// Metodo para trasladar el rectangulo
void trasladar(double dx, double dy) {
for (int i = 0; i < 4; i++) {
vertices[i].x += dx;
vertices[i].y += dy;
}
}
// Metodo para rotar el rectangulo respecto al origen (0,0)
void rotar(double anguloGrados) {
double anguloRadianes = Math.toRadians(anguloGrados);
double cos = Math.cos(anguloRadianes);
double sin = Math.sin(anguloRadianes);
for (int i = 0; i < 4; i++) {
double x = vertices[i].x;
double y = vertices[i].y;
double nuevoX = x * cos - y * sin;
double nuevoY = x * sin + y * cos;
vertices[i].x = nuevoX;
vertices[i].y = nuevoY;
}
}
// Metodo para escalar el rectangulo respecto al origen (0,0)
void escalar(double sx, double sy) {
for (int i = 0; i < 4; i++) {
vertices[i].x *= sx;
vertices[i].y *= sy;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Crear un rectangulo inicial
System.out.println("Ingrese la posicion (x, y), el ancho y el alto del
rectangulo:");
System.out.print("x: ");
double x = sc.nextDouble();
System.out.print("y: ");
double y = sc.nextDouble();
System.out.print("Ancho: ");
double ancho = sc.nextDouble();
System.out.print("Alto: ");
double alto = sc.nextDouble();
Rectangulo rect = new Rectangulo(x, y, ancho, alto);
int opcion;
do {
System.out.println("\n--- Menu de Transformaciones ---");
System.out.println("1. Traslacion");
System.out.println("2. Rotacion");
System.out.println("3. Escalamiento");
System.out.println("4. Mostrar coordenadas");
System.out.println("0. Salir");
System.out.print("Seleccione una opcion: ");
opcion = sc.nextInt();
switch (opcion) {
case 1:
System.out.print("Ingrese cuanto desea trasladar en X: ");
double dx = sc.nextDouble();
System.out.print("Ingrese cuanto desea trasladar en Y: ");
double dy = sc.nextDouble();
rect.trasladar(dx, dy);
System.out.println("Rectangulo trasladado.");
break;
case 2:
System.out.print("Ingrese el angulo de rotacion (en grados):
");
double angulo = sc.nextDouble();
rect.rotar(angulo);
System.out.println("Rectangulo rotado.");
break;
case 3:
System.out.print("Ingrese el factor de escala en X: ");
double sx = sc.nextDouble();
System.out.print("Ingrese el factor de escala en Y: ");
double sy = sc.nextDouble();
rect.escalar(sx, sy);
System.out.println("Rectangulo escalado.");
break;
case 4:
rect.mostrar();
break;
case 0:
System.out.println("Saliendo del programa...");
break;
default:
System.out.println("Opcion invalida.");
}
} while (opcion != 0);
sc.close();
}
}