Results 1 to 2 of 2
  1. #1
    jazzmasterkc is offline Member
    Join Date
    Sep 2013
    Location
    Lincoln, NE
    Posts
    5
    Rep Power
    0

    Default 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

    Java 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[0]);
    	double x2 = Double.parseDouble(args[2]);
    	double x3 = Double.parseDouble(args[4]);
    	double x4 = Double.parseDouble(args[6]);
    	double y1 = Double.parseDouble(args[1]);
    	double y2 = Double.parseDouble(args[3]);
    	double y3 = Double.parseDouble(args[5]);
    	double y4 = Double.parseDouble(args[7]);
    	
    	//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[4];
    	double[] arrayY = new double[4];
    	
    	double maxX = 0;
    	double maxY = 0;
    	double minX = 9999;
    	double minY = 9999;
    	for (int i = 0; i < 4; i++) {
    		if (i == 0) {
    			arrayX[0] = x1;
    			arrayY[0] = y1;
    		} else if (i == 1) {
    			arrayX[1] = x2;
    			arrayY[1] = y2;
    		} else if (i == 2) {
    			arrayX[2] = x3;
    			arrayY[2] = y3;
    		} else if (i == 3) {
    			arrayX[3] = x4;
    			arrayY[3] = 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;
    	}
    	
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,870
    Rep Power
    5

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 0
    Last Post: 02-06-2013, 08:18 PM
  2. Help With Rectangles
    By CG9892 in forum New To Java
    Replies: 16
    Last Post: 11-27-2012, 08:24 PM
  3. Replies: 2
    Last Post: 12-09-2010, 01:40 PM
  4. Area Intersect
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:07 PM
  5. Replies: 0
    Last Post: 06-22-2008, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •