# Intersect of rectangles efficiency

• 09-11-2013, 09:03 AM
jazzmasterkc
Intersect of rectangles efficiency
Hello,

I just finished a computer science assignment in my course. The goal was to use command line arguments, be able to parse the data and output various information. the sets of cordinates given were only (x,y) of bottom left, and (x,y) of top right of two squares (so 8 command line argument). Otherwise that was the the assignment object. But I'm curious if this can be done better, I feel like my program is sloppy :p

Code:

``` package rect; import java.awt.geom.Rectangle2D; import java.lang.reflect.Field; /* Assignment 01 *  Student: ***** ******* *  Rectangle Geometries */ public class Rectangles {         public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {                                 //check if args are correct                 if (args.length != 8) {                         System.out.println("Error: Program Requires 8 arguments!");                         System.exit(1);                 }                         //store parsed poitns         double x1 = Double.parseDouble(args);         double x2 = Double.parseDouble(args);         double x3 = Double.parseDouble(args);         double x4 = Double.parseDouble(args);         double y1 = Double.parseDouble(args);         double y2 = Double.parseDouble(args);         double y3 = Double.parseDouble(args);         double y4 = Double.parseDouble(args);                 //set up parameters for Rectangles function         double r1Len = x2 - x1;         double r1High = y2 - y1;         double r2Len = x4 - x3;         double r2High = y4 - y3;                 //create Rectangle objects         Rectangle2D r1 = new Rectangle2D.Double(x1, y1, r1Len, r1High);         Rectangle2D r2 = new Rectangle2D.Double(x3, y3, r2Len, r2High);         boolean flag = r1.intersects(r2);                 //check if there is no intersection.. no one confuses my programs! :p         if (flag == false) {                 System.out.println("empty intersection");                 System.exit(1);         }                 //fetch object fields         Rectangle2D r3 = r1.createIntersection(r2);         double r3h = getR3h(r3);         double r3w = getR3w(r3);         double r3x = getR3x(r3);         double r3y = getR3y(r3);                 double area = r3h * r3w;         double r3x2 = r3w + r3x;         double r3y2 = r3h + r3y;                 System.out.println("Intersecting rectangle: ("+r3x+", "+r3y+"), ("+r3x2+", "+r3y2+")");         System.out.println("Area: "+area);                 //function to get covering rectangle         double[] arrayX = new double;         double[] arrayY = new double;                 double maxX = 0;         double maxY = 0;         double minX = 9999;         double minY = 9999;         for (int i = 0; i < 4; i++) {                 if (i == 0) {                         arrayX = x1;                         arrayY = y1;                 } else if (i == 1) {                         arrayX = x2;                         arrayY = y2;                 } else if (i == 2) {                         arrayX = x3;                         arrayY = y3;                 } else if (i == 3) {                         arrayX = x4;                         arrayY = y4;                 }                                 if (maxX < arrayX[i]) {                         maxX = arrayX[i];                 }                                 if (maxY < arrayY[i]) {                         maxY = arrayY[i];                 }                                 if (minX > arrayX[i]) {                         minX = arrayX[i];                 }                                 if (minY > arrayY[i]) {                         minY = arrayY[i];                 }         }                 r3w = maxX - minX;         r3h = maxY - minY;         area = r3w * r3h;                 System.out.println("Covering Rectangle: ("+minX+", "+minY+"), ("+maxX+", "+maxY+")");         System.out.println("Area: "+area);         }                 //field functions         static double getR3h(Rectangle2D r3) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {                 Field hf;             Double num;             Class lol = r3.getClass();             hf = lol.getField("height");             num = (Double) hf.get(r3);                                 return num;                         }                 static double getR3w(Rectangle2D r3) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {                 Field wf;                 Double num;                 Class lol = r3.getClass();                 wf = lol.getField("width");                 num = (Double) wf.get(r3);                                 return num;         }                 static double getR3x(Rectangle2D r3) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {                 Field xf;                 Double num;                 Class lol = r3.getClass();                 xf = lol.getField("x");                 num = (Double) xf.get(r3);                                 return num;         }                 static double getR3y(Rectangle2D r3) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {                 Field yf;                 Double num;                 Class lol = r3.getClass();                 yf = lol.getField("y");                 num = (Double) yf.get(r3);                                 return num;         }         }```
• 09-11-2013, 09:37 AM
gimbal2
Re: Intersect of rectangles efficiency
Yes it can be done better, but then you first need to learn more about Java programming and be able to properly use multiple classes. For now don't focus on how pretty your code is, focus on actually understanding it all.