Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://sit.tu-varna.bg/wp-content/uploads/2018/04/OOP2/project02.pdf
- // I)
- package color;
- public class color implements Comparable<Object>
- {
- private static long BYTE_MASK = 255; //konstanti s glavni bukvi
- private long red,green,blue,rgb;
- // 24 bitovo chislo (xxxxxxxxR xxxxxxxxG xxxxxxxxB ---- 256*256*R + 256*G + 1*B
- // R,G,B e [0,255] rgb e [0 ~ 2(na 24-1)]
- public void rgb2color()
- { // 16 bita na lqvo << 8 bita na lqvo
- rgb = red << 16 | green << 8 | << blue;
- // ||-logicheski operacii |-po bitovi operacii
- } // &&-logicheski operacii &-po bitovi operacii
- public void color2rgb()
- {
- red = rgb >> 16;
- green = (rgb >> 8) & BYTE_MASK;
- blue = rgb & BYTE_MASK;
- }
- // 1)
- public color() {}
- // 2)
- public color(long c)
- {
- this.rgb=c;
- color2rgb();
- }
- // 4) nqma 3 po uslovie
- public long getRed()
- {
- return red;
- }
- public void setRed(long r)
- {
- red = r;
- rgb2color();
- }
- public long getGreen()
- {
- return green;
- }
- public void setGreen(long g)
- {
- green = g;
- rgb2color();
- }
- public long getBlue()
- {
- return blue;
- }
- public void setBlue(long b)
- {
- blue = b;
- rgb2color();
- }
- // 5)
- public String toString()
- {
- return "Red: " + red + ", Green: " + green + ", Blue: " + blue + ", RGB: " + rgb;
- }
- // 6)
- public boolean equals(Object r)
- {
- return this.rgb == ((color)r).rgb;
- }
- // 7)
- public int compareTo (Object c)
- {
- if (this.rgb < ((color)c).rgb) return -1;
- if (this.rgb > ((color)c).rgb) return 1;
- return 0; // <- sukratena forma na else. //else return 0;
- }
- }
- -----------------------------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------------------------
- // http://sit.tu-varna.bg/wp-content/uploads/2018/04/OOP2/project02.pdf
- // II) Izpolzvame Source koda ot uprajnenie 1 i pravim malki promeni po nego
- package color;
- public class ColorRec extends color implements Comparable<Object>
- {
- // http://sit.tu-varna.bg/wp-content/uploads/2018/04/OOP2/project01.pdf
- private int ix1, iy1, ix2, iy2;
- // 1)
- public ColorRec() {}
- // 2) //dobavqme long c
- public ColorRec (int x1, int y1, int x2, int y2, long c)
- {
- // dobavqme super(c);
- super(c);
- ix1 = x1;
- this.iy1 = y1;
- this.ix2=x2;
- this.iy2=y2;
- }
- // dobavqme void set-ove
- public void setix1(int x)
- {
- ix1=x;
- }
- public void setix2(int x)
- {
- ix2=x;
- }
- public void setiy1(int y)
- {
- ix1=y;
- }
- public void setiy2(int y)
- {
- ix2=y;
- }
- // 3)
- public int getix1()
- {
- return ix1;
- }
- public int getiy1()
- {
- return iy1;
- }
- public int getix2()
- {
- return ix2;
- }
- public int getiy2()
- {
- return iy2;
- }
- public int calcArea()
- {
- return Math.abs((ix2-ix1)*(iy2-iy1));
- }
- public int compareTo(Object r)
- {
- if (this.calcArea() > ((ColorRec)r).calcArea()) return 1;
- if (this.calcArea() < ((ColorRec)r).calcArea()) return -1;
- return 0; // if(this.calcArea() = ((ColorRec)r).calcArea()) return 0;
- }
- public String toString()
- { // 7) dobavqme super.toString();
- return "ix1: " + ix1 + ", iy1: " + iy1 + " ,ix2: " + ix2 + " , iy2: " + iy2 + super.toString();
- }
- // 8)
- public boolean equals(ColorRec r)
- {
- return this.calcArea() == r.calcArea() && super.equals(r);
- }
- public void translatex(int iPoints)
- {
- ix1 = ix1 + iPoints;
- // same as above -> ix2 += iPoints;
- }
- public void translatey(int iPoints)
- {
- iy1 = iy1 + iPoints;
- // same as above -> iy2 += iPoints;
- }
- public void translatexy(int iPoints)
- {
- translatex(iPoints);
- translatey(iPoints);
- }
- public boolean isInside(int ptx, int pty)
- {
- return (ix1 < ptx && ix2 > ptx && iy1 < pty && iy2 > pty);
- }
- // Dolen lqv ugul -> po-malki stoinosti // Goren desen ugul -> po-golemi stoinosti
- public ColorRec unionRect(ColorRec r)
- { // dobavqme long c, i v return-a dobavqme c
- int x1 = Math.min(ix1, r.ix1);
- int y1 = Math.min(iy1, r.iy1);
- int x2 = Math.max(ix2, r.ix2);
- int y2 = Math.max(iy2, r.iy2);
- long c = this.getRgb() | r.getRgb();
- return new ColorRec(x1,y1,x2,y2,c);
- // int x1 = ix1 < r.ix1 ? ix1 : r.ix1; // Ternalen Zapis ! // uslovie - istina - luja
- }
- public ColorRec intersectionRect(ColorRec r)
- { // dobavqme long c, i v return-a dobavqme c
- int x1 = Math.max(ix1, r.ix1);
- int y1 = Math.max(iy1, r.iy1);
- int x2 = Math.min(ix2, r.ix2);
- int y2 = Math.min(iy2, r.iy2);
- long c = this.getRgb() | r.getRgb();
- return new ColorRec(x1,y1,x2,y2,c);
- }
- // Main
- public static void main (String[] args)
- {
- // --------------- dobavqme 5-to chislo
- ColorRec r1 = new ColorRec(1,1,6,6,825896);
- System.out.println(r1.toString());
- ColorRec r2 = new ColorRec (5,5,12,12,15);
- System.out.println(r2.toString());
- // -------------------------------------------
- r1.translatexy(10);
- System.out.println(r1.toString());
- // --------------------------------------------
- if (r1.equals(r2))
- {
- System.out.println("Areas are equal");
- }
- else
- {
- System.out.println("Areas are different");
- }
- // ---------------------------------------------
- if(r1.compareTo(r2)==-1)
- {
- System.out.println("r1 < r2");
- }
- if(r1.compareTo(r2)==1)
- {
- System.out.println("r1 > r2");
- }
- else
- {
- System.out.println("r1 = r2");
- }
- // ----------------------------------------------
- ColorRec r3=r1.unionRect(r2);
- System.out.println(r3.toString());
- }
- // ----------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement