Line | Hits | Source |
---|---|---|
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; | |
21 | 0 | Set points = new HashSet(); |
22 | /** | |
23 | * @param rectangle | |
24 | */ | |
25 | 0 | public Intersector(Rectangle rectangle) { |
26 | 0 | this.rectangle = rectangle; |
27 | 0 | } |
28 | /** | |
29 | * @param rectangle | |
30 | * @param line | |
31 | */ | |
32 | 0 | public Intersector(Rectangle rectangle, Line2D line) { |
33 | 0 | this.rectangle = rectangle; |
34 | 0 | intersectLine(line); |
35 | 0 | } |
36 | ||
37 | public void intersectLine(Line2D line) { | |
38 | 0 | this.line = line; |
39 | 0 | points.clear(); |
40 | 0 | float rx0 = (float) rectangle.getMinX(); |
41 | 0 | float ry0 = (float) rectangle.getMinY(); |
42 | 0 | float rx1 = (float) rectangle.getMaxX(); |
43 | 0 | float ry1 = (float) rectangle.getMaxY(); |
44 | ||
45 | 0 | float x1 = (float) line.getX1(); |
46 | 0 | float y1 = (float) line.getY1(); |
47 | 0 | float x2 = (float) line.getX2(); |
48 | 0 | float y2 = (float) line.getY2(); |
49 | ||
50 | 0 | float dy = y2 - y1; |
51 | 0 | float dx = x2 - x1; |
52 | ||
53 | 0 | if(dx != 0) { |
54 | 0 | float m = dy/dx; |
55 | 0 | float b = y1 - m*x1; |
56 | ||
57 | // base of rect where y == ry0 | |
58 | 0 | float x = (ry0 - b) / m; |
59 | ||
60 | 0 | if(rx0 <= x && x <= rx1) { |
61 | 0 | points.add(new Point2D.Float(x, ry0)); |
62 | } | |
63 | ||
64 | // top where y == ry1 | |
65 | 0 | x = (ry1 - b) / m; |
66 | 0 | if(rx0 <= x && x <= rx1) { |
67 | 0 | points.add(new Point2D.Float(x, ry1)); |
68 | } | |
69 | ||
70 | // left side, where x == rx0 | |
71 | 0 | float y = m * rx0 + b; |
72 | 0 | if(ry0 <= y && y <= ry1) { |
73 | 0 | points.add(new Point2D.Float(rx0, y)); |
74 | } | |
75 | ||
76 | ||
77 | // right side, where x == rx1 | |
78 | 0 | y = m * rx1 + b; |
79 | 0 | if(ry0 <= y && y <= ry1) { |
80 | 0 | points.add(new Point2D.Float(rx1, y)); |
81 | } | |
82 | ||
83 | } else { | |
84 | ||
85 | // base, where y == ry0 | |
86 | 0 | float x = x1; |
87 | 0 | if(rx0 <= x && x <= rx1) { |
88 | 0 | points.add(new Point2D.Float(x, ry0)); |
89 | } | |
90 | ||
91 | // top, where y == ry1 | |
92 | 0 | x = x2; |
93 | 0 | if(rx0 <= x && x <= rx1) { |
94 | 0 | points.add(new Point2D.Float(x, ry1)); |
95 | } | |
96 | } | |
97 | 0 | } |
98 | public Line2D getLine() { | |
99 | 0 | return line; |
100 | } | |
101 | public Set getPoints() { | |
102 | 0 | return points; |
103 | } | |
104 | public Rectangle getRectangle() { | |
105 | 0 | return rectangle; |
106 | } | |
107 | ||
108 | public String toString() { | |
109 | 0 | return "Rectangle: "+rectangle+", points:"+points; |
110 | } | |
111 | ||
112 | public static void main(String[] args) { | |
113 | 0 | Rectangle rectangle = new Rectangle(0,0,10,10); |
114 | 0 | Line2D line = new Line2D.Float(4,4,5,5); |
115 | 0 | System.err.println(""+new Intersector(rectangle, line)); |
116 | 0 | System.err.println(""+new Intersector(rectangle, new Line2D.Float(9,11,11,9))); |
117 | 0 | System.err.println(""+new Intersector(rectangle, new Line2D.Float(1,1,3,2))); |
118 | 0 | System.err.println(""+new Intersector(rectangle, new Line2D.Float(4,6,6,4))); |
119 | 0 | } |
120 | ||
121 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |