Coverage details for edu.uci.ics.jung.visualization.transform.shape.Intersector

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  */
9 package edu.uci.ics.jung.visualization.transform.shape;
10  
11 import java.awt.Rectangle;
12 import java.awt.geom.Line2D;
13 import java.awt.geom.Point2D;
14 import java.util.HashSet;
15 import java.util.Set;
16  
17 public class Intersector {
18     
19     protected Rectangle rectangle;
20     Line2D line;
210    Set points = new HashSet();
22     /**
23      * @param rectangle
24      */
250    public Intersector(Rectangle rectangle) {
260        this.rectangle = rectangle;
270    }
28     /**
29      * @param rectangle
30      * @param line
31      */
320    public Intersector(Rectangle rectangle, Line2D line) {
330        this.rectangle = rectangle;
340       intersectLine(line);
350    }
36     
37     public void intersectLine(Line2D line) {
380        this.line = line;
390        points.clear();
400        float rx0 = (float) rectangle.getMinX();
410        float ry0 = (float) rectangle.getMinY();
420        float rx1 = (float) rectangle.getMaxX();
430        float ry1 = (float) rectangle.getMaxY();
44         
450        float x1 = (float) line.getX1();
460        float y1 = (float) line.getY1();
470        float x2 = (float) line.getX2();
480        float y2 = (float) line.getY2();
49         
500        float dy = y2 - y1;
510        float dx = x2 - x1;
52         
530        if(dx != 0) {
540            float m = dy/dx;
550            float b = y1 - m*x1;
56             
57             // base of rect where y == ry0
580            float x = (ry0 - b) / m;
59             
600            if(rx0 <= x && x <= rx1) {
610                points.add(new Point2D.Float(x, ry0));
62             }
63             
64             // top where y == ry1
650            x = (ry1 - b) / m;
660            if(rx0 <= x && x <= rx1) {
670                points.add(new Point2D.Float(x, ry1));
68             }
69             
70             // left side, where x == rx0
710            float y = m * rx0 + b;
720            if(ry0 <= y && y <= ry1) {
730                points.add(new Point2D.Float(rx0, y));
74             }
75             
76             
77             // right side, where x == rx1
780            y = m * rx1 + b;
790            if(ry0 <= y && y <= ry1) {
800                points.add(new Point2D.Float(rx1, y));
81             }
82             
83         } else {
84             
85             // base, where y == ry0
860            float x = x1;
870            if(rx0 <= x && x <= rx1) {
880                points.add(new Point2D.Float(x, ry0));
89             }
90             
91             // top, where y == ry1
920            x = x2;
930            if(rx0 <= x && x <= rx1) {
940                points.add(new Point2D.Float(x, ry1));
95             }
96         }
970    }
98     public Line2D getLine() {
990        return line;
100     }
101     public Set getPoints() {
1020        return points;
103     }
104     public Rectangle getRectangle() {
1050        return rectangle;
106     }
107  
108     public String toString() {
1090        return "Rectangle: "+rectangle+", points:"+points;
110     }
111     
112     public static void main(String[] args) {
1130        Rectangle rectangle = new Rectangle(0,0,10,10);
1140        Line2D line = new Line2D.Float(4,4,5,5);
1150        System.err.println(""+new Intersector(rectangle, line));
1160        System.err.println(""+new Intersector(rectangle, new Line2D.Float(9,11,11,9)));
1170        System.err.println(""+new Intersector(rectangle, new Line2D.Float(1,1,3,2)));
1180        System.err.println(""+new Intersector(rectangle, new Line2D.Float(4,6,6,4)));
1190    }
120  
121 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.