/* * * Written By: Kenneth C. Baltz * Date Completed: 23 Apr 96 * Description: Graphical display applet of a scanline algorithm * to locate intersections among arbitrary lines * Usage Notes: This algorithm excludes some cases that are remotely * possible with arbitrary lines. These include: * 1) Multiple lines intersecting at the same point * 2) Vertical lines (Infinite Slope) * This was written for a class project, so * that's why it's not too detailed * Filename: Intersect.java * Related Files: SketchBoard.java // Handles the graphics * InterestingPoint.java // Data structure * LineList.java // Manages Scan line * Node.java // Data structure * point.java // Data structure * PQ.java // Priority Queue * line.java // Data structure Last change: KCB 2 May 96 10:23 am */ import java.applet.Applet; import java.awt.*; import java.util.*; import java.io.*; import java.lang.*; public class Intersect extends Applet { static final int NUM_LINES = 10; static final int MAX_X = 200; static final int MAX_Y = 200; static final int OFF_X = 10; static final int OFF_Y = 10; static final boolean rand=true; static final boolean DEBUG=false; private int RAND_SEED=0; private float scanx=0; private int lastx=0; private Line select_line=null; private InterestingPoint currentIP; public int NumIntersects=0; ///////////////////////////////////////////////////// // GUI stuff SketchBoard canvas; Button step_button; Button reset_button; Frame displayFrame; List list_of_lines; Label status_text; Label num_intersects; TextField rand_text; // ///////////////////////////////////////////////////// boolean step; private Line [] lines; public PQ xtree; public LineList ytree; public void init() { this.setLayout(new BorderLayout(15,15)); canvas = new SketchBoard(lines, scanx, currentIP,select_line, MAX_X, MAX_Y, OFF_X, OFF_Y, NUM_LINES); canvas.resize(MAX_X + 50,MAX_Y + 50); this.add("West",canvas); Panel Master_East =new Panel(); Master_East.setLayout(new BorderLayout(15,15)); this.add("East",Master_East); Panel button_panel = new Panel(); button_panel.setLayout(new FlowLayout(FlowLayout.CENTER,15,15)); step_button= new Button("Step"); button_panel.add( step_button ); reset_button= new Button("Reset"); button_panel.add( reset_button ); Panel list_panel = new Panel(); list_panel.setLayout(new BorderLayout(1,1)); Label list_label = new Label("Double Click to highlight lines"); list_panel.add("North",list_label); list_of_lines= new List(NUM_LINES,false); list_panel.add("South",list_of_lines ); this.add("South",button_panel); Panel text_panel = new Panel(); text_panel.setLayout(new BorderLayout(1,1)); Panel random_panel= new Panel(); random_panel.setLayout(new BorderLayout(1,1)); Label l=new Label("Random Seed"); random_panel.add("North",l); rand_text=new TextField("0",3); random_panel.add("South",rand_text); Panel intersect_panel = new Panel(); intersect_panel.setLayout(new BorderLayout(15,15)); num_intersects = new Label("Intersections: "+NumIntersects); intersect_panel.add("South",num_intersects); status_text = new Label(" "); intersect_panel.add("North",status_text); Master_East.add("North", button_panel ); Master_East.add("West", list_panel ); text_panel.add("South", random_panel ); text_panel.add("North", intersect_panel ); Master_East.add("East", text_panel ); this.resize(this.minimumSize()); step_button.disable(); step=false; paint(this.getGraphics()); } // init() private void Reset() { scanx=0; currentIP = null; NumIntersects=0; num_intersects.setText("Intersections: "+NumIntersects); xtree = new PQ(NUM_LINES*3); // list_of_lines.clear(); while(list_of_lines.countItems()>0) list_of_lines.delItem(0); ytree = new LineList(); lines=new Line[10]; if(rand){ int z=0; try { z=Integer.parseInt(rand_text.getText()); } catch(Exception ex) { if(ex instanceof NumberFormatException); rand_text.setText(Integer.toString(RAND_SEED)); z=0; } RAND_SEED=z; Random rand=new Random(RAND_SEED); int x, y; InterestingPoint ip; // list_of_lines.clear(); canvas.select_line=null; select_line=null; for ( int i=0;iscanx) xtree.insert(intersections[0]); } if(intersections[1]!=null) { if(DEBUG) System.out.println("Inserting an intesection at " + intersections[1].p.x); if(intersections[1].p.x>scanx) xtree.insert(intersections[1]); } } else if ( currentIP.left ) // is this IP a left endpoint? { status_text.setText("Left Endpoint"); scanx=currentIP.line.p1.x; if(DEBUG) System.out.println("scanx = "+scanx); if(DEBUG) System.out.println("Dealing with a left endpoint"); InterestingPoint [] intersections; intersections=ytree.Insert(currentIP.line,scanx); // could produce 0-2 intersections from inserting a point if(intersections[0]!=null) { if(DEBUG) System.out.println("Inserting an intesection at " + intersections[0].p.x); if(intersections[0].p.x>=scanx) xtree.insert(intersections[0]); } if(intersections[1]!=null) { if(DEBUG) System.out.println("Inserting an intesection at " + intersections[1].p.x); if(intersections[1].p.x>=scanx) xtree.insert(intersections[1]); } } else // if it's a right endpoint // we need to remove it { status_text.setText("Right Endpoint"); scanx=currentIP.line.p2.x; if(DEBUG) System.out.println("scanx = "+scanx); if(DEBUG) System.out.println("Dealing with a right endpoint"); InterestingPoint p; if(null!=(p=ytree.Delete(currentIP.line))) // don't want to insert any old intersects if(p.p.x>=scanx) xtree.insert(p); } } paint(this.getGraphics()); if(DEBUG) System.out.println("ytree ="+ytree); if(DEBUG) System.out.println("xtree: "+xtree); } // go public void paint(Graphics g) { canvas.scanx=scanx; canvas.lines=lines; canvas.currentIP=currentIP; canvas.select_line=select_line; canvas.paint(canvas.getGraphics()); } public boolean action(Event e, Object arg) { if(e.target == step_button) { step=true; if(DEBUG) System.out.println("Hit step_button"); go(); return true; } else if(e.target == reset_button) { Reset(); return true; } else if(e.target == list_of_lines && e.id==Event.ACTION_EVENT ) { int i; for( i=0;i