Advertisement
rivalcoba

acuarela

May 6th, 2020
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. using System;
  2. namespace Acuarela.Primitives
  3. {
  4.     // Defining a Pixel
  5.     public class iPixel
  6.     {
  7.         // Fields | Attirbutes | Caracteristicas
  8.         int x, y;
  9.         // Properties
  10.         public int X
  11.         {
  12.             // Get
  13.             get{
  14.              return this.x;  
  15.             }
  16.             // Set
  17.             set{
  18.                 if(value < 0)
  19.                 {
  20.                     throw new Exception("> Chico malo las coordenadas no pueden ser menores que cero");
  21.                 }
  22.                 this.x = value;
  23.             }
  24.         }
  25.         public int Y
  26.         {
  27.             // Get
  28.             get{
  29.              return this.y;  
  30.             }
  31.             // Set
  32.             set{
  33.                 if(value < 0)
  34.                 {
  35.                     throw new Exception("> Chico malo las coordenadas no pueden ser menores que cero");
  36.                 }
  37.                 this.y = value;
  38.             }
  39.         }          
  40.         // Constructores
  41.         public iPixel(){}
  42.         public iPixel(int x, int y){
  43.             this.X = x;
  44.             this.Y = y;
  45.         }
  46.     }
  47.  
  48.     // Defining a rectangle class
  49.     public class iRectangle
  50.     {
  51.         // Fields | Campos | Atributos
  52.         iPixel pixel1 = new iPixel(), pixel2 = new iPixel();
  53.         // Rectangle Properties
  54.         public iPixel Pixel1
  55.         {
  56.             get{
  57.                 return this.pixel1;
  58.             }
  59.             set{
  60.                 this.pixel1 = value;
  61.             }
  62.         }
  63.         public iPixel Pixel2
  64.         {
  65.             get{
  66.                 return this.pixel2;
  67.             }
  68.             set{
  69.                 this.pixel2 = value;
  70.             }
  71.         }
  72.  
  73.         // Metodos Auxiliares
  74.         private static int Max(int num1, int num2){
  75.             // Logica
  76.             return num1 > num2 ? num1 : num2;
  77.         }
  78.         private static int Min(int num1, int num2){
  79.             // Logica
  80.             return num1 < num2 ? num1 : num2;
  81.         }
  82.  
  83.         // Metodos que trabajan con los pixeles
  84.         private static iPixel GetMaxPix(iPixel pix1, iPixel pix2)
  85.         {
  86.             // Creando el pixel resultante
  87.             iPixel maxPixel = new iPixel();
  88.             maxPixel.X = Max(pix1.X, pix2.X);
  89.             maxPixel.Y = Max(pix1.Y, pix2.Y);
  90.             return maxPixel;
  91.         }
  92.         private static iPixel GetMinPix(iPixel pix1, iPixel pix2)
  93.         {
  94.             // Creando el pixel resultante
  95.             iPixel maxPixel = new iPixel();
  96.             maxPixel.X = Min(pix1.X, pix2.X);
  97.             maxPixel.Y = Min(pix1.Y, pix2.Y);
  98.             return maxPixel;
  99.         }
  100.  
  101.         private static bool IsIntersection(iRectangle rec)
  102.         {
  103.             bool cond1 = rec.Pixel2.X - rec.Pixel1.X > 0;
  104.             bool cond2 = rec.Pixel2.Y - rec.Pixel1.Y > 0;
  105.             return cond1 && cond2;
  106.         }
  107.  
  108.         public static iRectangle GetIntersection(iRectangle rec1, iRectangle rec2)
  109.         {
  110.             // Creo mi rectangulo respueta
  111.             iRectangle interRec = new iRectangle();
  112.             // Calculando la interseccion entre rec1 y rec2
  113.             interRec.Pixel1 = GetMaxPix(rec1.Pixel1, rec2.Pixel1);
  114.             interRec.Pixel2 = GetMinPix(rec1.Pixel2, rec2.Pixel2);
  115.             // Validando si la respuesta es una interseccion
  116.             if(!IsIntersection(interRec))
  117.             {
  118.                 return null;
  119.             }
  120.             // Retorno la interseccion
  121.             return interRec;
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement