Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Acuarela.Primitives
- {
- // Defining a Pixel
- public class iPixel
- {
- // Fields | Attirbutes | Caracteristicas
- int x, y;
- // Properties
- public int X
- {
- // Get
- get{
- return this.x;
- }
- // Set
- set{
- if(value < 0)
- {
- throw new Exception("> Chico malo las coordenadas no pueden ser menores que cero");
- }
- this.x = value;
- }
- }
- public int Y
- {
- // Get
- get{
- return this.y;
- }
- // Set
- set{
- if(value < 0)
- {
- throw new Exception("> Chico malo las coordenadas no pueden ser menores que cero");
- }
- this.y = value;
- }
- }
- // Constructores
- public iPixel(){}
- public iPixel(int x, int y){
- this.X = x;
- this.Y = y;
- }
- }
- // Defining a rectangle class
- public class iRectangle
- {
- // Fields | Campos | Atributos
- iPixel pixel1 = new iPixel(), pixel2 = new iPixel();
- // Rectangle Properties
- public iPixel Pixel1
- {
- get{
- return this.pixel1;
- }
- set{
- this.pixel1 = value;
- }
- }
- public iPixel Pixel2
- {
- get{
- return this.pixel2;
- }
- set{
- this.pixel2 = value;
- }
- }
- // Metodos Auxiliares
- private static int Max(int num1, int num2){
- // Logica
- return num1 > num2 ? num1 : num2;
- }
- private static int Min(int num1, int num2){
- // Logica
- return num1 < num2 ? num1 : num2;
- }
- // Metodos que trabajan con los pixeles
- private static iPixel GetMaxPix(iPixel pix1, iPixel pix2)
- {
- // Creando el pixel resultante
- iPixel maxPixel = new iPixel();
- maxPixel.X = Max(pix1.X, pix2.X);
- maxPixel.Y = Max(pix1.Y, pix2.Y);
- return maxPixel;
- }
- private static iPixel GetMinPix(iPixel pix1, iPixel pix2)
- {
- // Creando el pixel resultante
- iPixel maxPixel = new iPixel();
- maxPixel.X = Min(pix1.X, pix2.X);
- maxPixel.Y = Min(pix1.Y, pix2.Y);
- return maxPixel;
- }
- private static bool IsIntersection(iRectangle rec)
- {
- bool cond1 = rec.Pixel2.X - rec.Pixel1.X > 0;
- bool cond2 = rec.Pixel2.Y - rec.Pixel1.Y > 0;
- return cond1 && cond2;
- }
- public static iRectangle GetIntersection(iRectangle rec1, iRectangle rec2)
- {
- // Creo mi rectangulo respueta
- iRectangle interRec = new iRectangle();
- // Calculando la interseccion entre rec1 y rec2
- interRec.Pixel1 = GetMaxPix(rec1.Pixel1, rec2.Pixel1);
- interRec.Pixel2 = GetMinPix(rec1.Pixel2, rec2.Pixel2);
- // Validando si la respuesta es una interseccion
- if(!IsIntersection(interRec))
- {
- return null;
- }
- // Retorno la interseccion
- return interRec;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement