Back to index

moin  1.9.0~rc2
PolyLineConnector.java
Go to the documentation of this file.
00001 /*
00002  * @(#)PolyLineConnector.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.figures;
00007 
00008 import java.awt.*;
00009 import java.io.IOException;
00010 import CH.ifa.draw.framework.*;
00011 import CH.ifa.draw.standard.*;
00012 import CH.ifa.draw.util.*;
00013 
00020 public class PolyLineConnector extends ChopBoxConnector {
00021 
00022     /*
00023      * Serialization support.
00024      */
00025     private static final long serialVersionUID = 6018435940519102865L;
00026 
00027     public PolyLineConnector() {
00028         super();
00029     }
00030 
00034     public PolyLineConnector(Figure owner) {
00035         super(owner);
00036     }
00037 
00038     protected Point chop(Figure target, Point from) {
00039         PolyLineFigure p = (PolyLineFigure)owner();
00040         // *** based on PolygonFigure's heuristic
00041         Point ctr = p.center();
00042         int cx = -1;
00043         int cy = -1;
00044         long len = Long.MAX_VALUE;
00045 
00046         // Try for points along edge
00047 
00048         for (int i = 0; i < p.pointCount()-1; i++) {
00049             Point p1 = p.pointAt(i);
00050             Point p2 = p.pointAt(i+1);
00051             Point chop = Geom.intersect(p1.x,
00052                                  p1.y,
00053                                  p2.x,
00054                                  p2.y,
00055                                  from.x,
00056                                  from.y,
00057                                  ctr.x,
00058                                  ctr.y);
00059             if (chop != null) {
00060                 long cl = Geom.length2(chop.x, chop.y, from.x, from.y);
00061                 if (cl < len) {
00062                     len = cl;
00063                     cx = chop.x;
00064                     cy = chop.y;
00065                 }
00066             }
00067         }
00068         // if none found, pick closest vertex
00069         //if (len ==  Long.MAX_VALUE) {
00070         { // try anyway
00071             for (int i = 0; i < p.pointCount(); i++) {
00072                 Point pp = p.pointAt(i);
00073                 long l = Geom.length2(pp.x, pp.y, from.x, from.y);
00074                 if (l < len) {
00075                     len = l;
00076                     cx = pp.x;
00077                     cy = pp.y;
00078                 }
00079             }
00080         }
00081         return new Point(cx, cy);
00082     }
00083 }
00084