Intersect.java

Download source from Intersect.jar

no intersectionrectangles intersectrectangle contains another


Intersect.java

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
//import javafx.scene.Cursor;
import javafx.scene.*;
import javafx.event.EventHandler;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.collections.*;
import javafx.geometry.Point2D;
import javafx.geometry.Bounds;
import javafx.scene.input.* ;
import javafx.scene.layout.Pane;
import java.lang.Math;


import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;

import java.util.ArrayList;

public class Intersect extends Application {
    Group grp = new Group();
    double xs;
	double ys;
	double xe;
	double ye;
    Rectangle r;    
    Rectangle r1=null;
    Rectangle r2=null;
    Rectangle r3=null;
    myRect ra, rb;

    public void start(Stage stage) {
	// Create a border pane 
    	BorderPane pane = new BorderPane();
	Scene scene = new Scene(pane, 400, 400);

	Pane mypane = new Pane();
	pane.setCenter(mypane);
	mypane.getChildren().add(grp);



	grp.setTranslateX(scene.getWidth()/2);
	grp.setTranslateY(scene.getHeight()/2);
	double res = 96;
	grp.setScaleX(res);
	grp.setScaleY(res);

	int N = 2;
	Grid.draw(grp,N);
	
	stage.setTitle("Intersecting Rectangles");
	stage.setScene(scene);
	stage.show();

	grp.setOnMousePressed( e ->
	{
	    xs = e.getX();
	    ys = e.getY();
	    if (r1!=null) r1.setFill(Color.TRANSPARENT);

	    r = new Rectangle(xs,ys,0,0);
	    r.setStroke(Color.BLACK);
	    r.setStrokeWidth(1/res);
	    r.setStrokeLineCap(StrokeLineCap.ROUND);
	    r.setFill(Color.TRANSPARENT);
	    grp.getChildren().add(r);
	    grp.getChildren().remove(r2);
	    grp.getChildren().remove(r3);
	    r3 = null;	    
	}
	);

	grp.setOnMouseDragged( e ->
	{
		xe = e.getX();
		ye = e.getY();

		double width = xe - xs;
		if (width>0) r.setX(xs);
		else r.setX(xe);
		r.setWidth(Math.abs(width));
		double height = ye-ys;
		if (height>0) r.setY(ys);
		else r.setY(ye);
		r.setHeight(Math.abs(height));
	}
	);
	
    
	grp.setOnMouseReleased( e ->
	{
		if (r1==null) r1 = r;
		else r2 = r;
		if (r2==null) return;
		ra = new myRect(r1);
		rb = new myRect(r2);

		r3 = myRect.intersection(ra,rb);
		if (ra.contains(rb)) {
			r2.setFill(Color.RED);
		}
		else if (rb.contains(ra)) {
		r1.setFill(Color.RED);
		}
		else if (r3!=null) {
		    	r3.setStrokeWidth(1/res);
		    	r3.setStrokeLineCap(StrokeLineCap.ROUND);
			r3.setStroke(Color.BLACK);
			r3.setFill(Color.BLUE);
			grp.getChildren().add(r3);
		}
	}
	);

    }

public static void main(String[] args) {
	launch(args);
    }
}

class myRect {
	double xmin,ymin,xmax,ymax;
	myRect(Rectangle r) {
		xmin=r.getX();
		ymin=r.getY();
		xmax = xmin + r.getWidth();
		ymax = ymin + r.getHeight();
	}
	public boolean contains(myRect r) {
		if (r.xmin<xmin) return false;
		if (r.xmax>xmax) return false;
		if (r.ymin<ymin) return false;
		if (r.ymax>ymax) return false;
		return true;
	}

	static public Rectangle intersection(myRect a, myRect b) {
		if (a.xmin > b.xmax) return null;
		if (a.xmax < b.xmin) return null;
		if (a.ymin > b.ymax) return null;
		if (a.ymax < b.ymin) return null;
		double x1 = Math.max(a.xmin,b.xmin);
		double y1 = Math.max(a.ymin,b.ymin);
		double x2 = Math.min(a.xmax,b.xmax);
		double y2 = Math.min(a.ymax,b.ymax);
		return new Rectangle(x1,y1,x2-x1,y2-y1);
	}
		
}


Maintained by John Loomis, updated Sun Apr 08 15:04:55 2018